Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 31

Project 1

Java CLI Rhythm Game

“Never Gonna Give You Up” -- Rick Astley

2SE1 Group 6:
Muhammad Fajrul Falah

Muhammad Yusuf Ilham Abdulhadi

Rivan Anjung Sinar Permana

Faculty:
Muhammad Riza Iqbal Latief, S.T.

Continuing Education Center for Computing and Information Technology


Faculty of Engineering, University of Indonesia
2023
PROJECT ON

Rhythm Game

Developed by:

1. Muhammad Fajrul Falah


2. Muhammad Yusuf Ilham Abdulhadi
3. Rivan Anjung Sinar Permana
Java CLI Rhythm Game

Batch Code : 2SE1

Start Date : Tuesday, March 21, 2023

End Date : Tuesday, April 11, 2023

Name of Faculty : Muhammad Riza Iqbal Latief, S.T.

Names of Developer :

1. Muhammad Fajrul Falah


2. Muhammad Yusuf Ilham Abdulhadi
3. Rivan Anjung Sinar Permana

Date of Submission : Tuesday, April 11, 2023


CERTIFICATE

This is to certify that this report entitled “Java CLI Rhythm Game” embodies the original
work done by Muhammad Fajrul Falah, Muhammad Yusuf Ilham Abdulhadi, and Rivan
Anjung Sinar Permana. Project in partial fulfilment of their course requirements at CEP-
CCIT FTUI which has relationship with NIIT.

Coordinator: Muhammad Riza Iqbal Latief, S.T.


SYSTEM ANALYSIS
System Summary:

Java CLI Rhythm Game that imitates Piano Tiles Interface is a simple or mini college
project that aims to recreate the mainstream mobile game, Piano Tiles, in a command-
line interface using Java programming language.

The game involves a series of black and white tiles that move down the screen, and
the player must hit the corresponding keys on their keyboard in time with the rhythm of
the music. The gameplay is similar to the original Piano Tiles game, but with a text-
based interface.

The game is implemented using Java, which is a popular programming language used
for creating desktop applications, mobile apps, and web services. The CLI interface
allows the game to be played on any computer that has a command-line interface,
without requiring any additional software or plugins.

The game may use various libraries or frameworks to handle audio playback, visual
rendering, and user input. Some popular libraries for audio processing in Java include
JavaSound and jAudio, while libraries such as JLine or JCurses can be used to create
command-line interfaces with more advanced features.

To create the game's interface, the project may use ASCII characters or other visual
representations to simulate the appearance of the Piano Tiles game. The game may also
include different difficulty levels, various music tracks, and high-score tracking.

The implementation of the project may involve various programming concepts,


including object-oriented programming, event-driven programming, and multithreading.
The project may also use external data sources, such as music files, to generate the
gameplay experience.

Overall, a Java CLI Rhythm Game that imitates Piano Tiles Interface is a challenging
and engaging college project that tests the student's knowledge and skills in Java
programming language and command-line interface design. The project also allows the
student to demonstrate their creativity in adapting a popular mobile game into a text-
based environment.
SYSTEM ANALYSIS
System Diagram
SYSTEM ANALYSIS

System Application

1. Game.java file

(The above code is a Java program that deals with file I/O, date/time formatting, audio
playback, and timers. It imports several libraries and defines a package called "project". The
program reads and writes files, formats date/time using DateTimeFormatter, plays audio
using Clip and AudioInputStream, and sets up a Timer and TimerTask for executing actions
at specific times. It appears to be designed for a time-based project or application that
involves file management and audio playback)

(The above code defines a class called "Game" in Java. It includes several static variables and
constants, such as a boolean flag for the game state, an integer value for a reach, and two File
objects for storing score and leaderboard data. The class also has a static Clip object for
playing audio. The "Game" class does not have a constructor, so it cannot be instantiated.
Instead, its static methods and variables can be accessed from other classes.)

(same as below)
(same as below)
(The above code is the main method of the "Game" class in Java. It starts by checking if the
"score.txt" and "leader.txt" files exist, and creates them if they do not. The main method then
enters a while loop that runs as long as the "running" flag is false. Inside the loop, it calls the
"mainmenu" method of the "Menu" class to display a menu of options to the user. It then uses
a switch statement to handle the user's choice. If the user chooses to play the game, it calls
one of the methods "easy()", "medium()", or "hard()" based on the user's difficulty selection.
After completing the game, it stops the audio clip and returns to the main menu. Other
options in the menu include displaying instructions, viewing previous game history, showing
the leaderboard, resetting the score data, or quitting the program. If an invalid choice is made,
the program prompts the user to try again.)
(The above code defines a static method called "clrscr()" in Java. This method is used to clear
the console screen. It does this by printing a series of special characters to the console. The
characters "\033[H" and "\033[2J" represent ANSI escape sequences that move the cursor to
the top left corner of the screen and clear the screen, respectively. Finally, the
"System.out.flush()" method is called to flush any remaining output to the console. This
results in a clean and empty console screen.)

(The above code defines a static method called "gameResetter()" in Java. This method is used
to reset the game state to its initial values. When called, the "running" variable is set to true to
indicate that the game is currently running. Then, a nested loop is used to iterate over each
element in the 2D "notes" array and set its value to a default string " ". Next, the "Score" and
"Combo" variables are reset to 0. Finally, the "resetButtonPressed()" method is called with a
parameter value of 0 to reset the game's button presses. This method essentially resets the
game to its starting state, allowing the user to play again from the beginning.)
(The code defines a method named "Music" that plays a specified audio file. The method
takes in a float parameter "volume" to control the volume of the audio file being played. The
audio file is loaded into the program using an AudioInputStream object, and then played
using the Clip object. The volume of the clip is controlled using the FloatControl object, and
the volume is set using the setValue method. If any errors occur during the loading or playing
of the audio file, the method prints out the stack trace of the error.)

(The code above defines a method named readScore() that reads the contents of a file named
score.txt and stores them as a string variable scoreList. The method first initializes an empty
string to store the contents of the file. Then it creates a new Scanner object to read the file. It
loops through the lines of the file and appends each line to the scoreList variable followed by
a newline character. Finally, the method returns the concatenated string scoreList that
contains the contents of the file. The method throws a FileNotFoundException if the file
specified is not found.)
(The code defines a method called "writeScore" that writes the player's score and information
to a file called "score.txt". The method takes in the player's name, score, combo, and the
difficulty level as parameters. It first creates a FileWriter object to append data to the end of
the file, then formats the current date and time using the LocalDateTime class. It then creates
a string containing the player's name, date, score, combo, and difficulty level. Finally, it
writes this string to the file and closes the FileWriter object.)

(The code above defines a method named readLeaderboard(), which reads the contents of a
file named "leader.txt" and returns a list of strings. The method first creates an empty
ArrayList named leaderboard. Then, it uses a Scanner to read the contents of the "leader.txt"
file line by line and add each line to the leaderboard list using the add() method. Finally, the
method closes the Scanner and returns the leaderboard list.)
(The code above is a method called updateLeaderboard() that takes in three parameters:
name, score, and combo. The method first sets the name parameter to the value of the
Menu.username. Then, it reads the current leaderboard by calling the readLeaderboard()
method which returns a list of strings.

Next, the method loops through the leaderboard and removes any entry that has the same
name as the name parameter. This is done so that the leaderboard only shows the most recent
score for each player. The loop splits each line in the leaderboard by the comma delimiter and
checks if the first element (which is the player's name) matches the name parameter. If it
does, the entry is removed from the leaderboard list and the loop is broken.)

(The code above is adding a new entry to the leaderboard with the player's name, score, and
combo if the player's score and combo are higher than the scores of the players currently on
the leaderboard.

It first removes the previous entry for the player (if it exists) from the leaderboard. It then
iterates through the current leaderboard to find the correct position for the new entry based on
the player's score and combo.

If the player's score and combo are higher than a player currently on the leaderboard, it adds
the new entry at that position and sets the updated variable to true. The updated variable is
used later to determine whether the leaderboard file needs to be rewritten with the new entry.)
(The program above updates the leaderboard by adding a new player's name, score, and
combo if they have a better score than an existing player or if they have the same score but a
higher combo. It first reads the current leaderboard from a text file and removes the old
record of the current player if it exists. It then checks if the player's score and combo are
higher than any existing player's score and combo in the leaderboard. If so, it adds the new
record at the appropriate position in the leaderboard list. If not, it adds the new record at the
end of the list. Finally, the updated leaderboard is written to the text file.)

(The code above contains a method called "update" which updates the user's score and
leaderboard data. It takes in two parameters, the difficulty level and the player's name. It
retrieves the player's score and combo from a previously defined method called
"returnScore". The method then calls two other methods, "writeScore" and
"updateLeaderboard", which respectively write the player's score data to a file called
"score.txt" and update the leaderboard with the player's new data.)
(The code above contains a method named "clearScore" that deletes the existing score and
leaderboard files and then creates new empty ones. The method throws an IOException,
which means that it may encounter an input/output error during the file creation process.)

(The code above is a method called "easy" that sets up and runs a game loop for the "easy"
difficulty level. It first resets the game, creates a timer, and schedules a TimerTask that runs
the game system every 1062 milliseconds. It checks if the final score is greater than or equal
to the target score "reach" times one, and if so, cancels the timer, updates the leaderboard
with the current player's name, score, and combo, and prints out a victory message. If the
final score is less than or equal to negative "reach" times one, the timer is canceled and a loss
message is printed out. After ten seconds, the music is played and the method returns to the
main menu.)
(The above code is a method for playing the game in medium difficulty mode. It resets the
game, sets up a timer task to run the game system and check the score at regular intervals,
and updates the score if the player wins. If the player reaches the winning score, the timer
task is cancelled, the game ends, and the final score and combo are printed. If the player
loses, the timer task is cancelled and the game ends. Finally, there is a short delay before
returning to the main menu and playing the background music.)
(The code above is a method for the "hard" difficulty level in a game. It starts by resetting the
game and creating a new timer. The timer runs a task repeatedly at a fixed interval of 531
milliseconds, which is faster than the "medium" and "easy" levels. The task includes running
the game system, getting the final score and combo, checking if the player has reached the
target score (which is ten times the "reach" variable), and updating the leaderboard and
printing a message accordingly. If the player's score goes below a certain threshold, the timer
is cancelled and the game ends. After the timer runs for a certain amount of time, the game
music volume is set to 0.5f.)
2. Menu.java file

(The above code is a Java package declaration for a project. It imports the necessary classes
for the project, including FileNotFoundException, IOException, List, and Scanner. These
classes are used to handle input/output operations and collections of data.)

(The code defines a class called Menu which extends the Game class. It includes a
static integer variable called mainchoice and a static string variable called username.
The mainmenu() method displays a menu with options for the user to select from
using a Scanner to take input. The menu options are: selecting difficulty, accessing
instructions, viewing game history, accessing the leaderboard, resetting the score, and
exiting the game.)
(The above code defines a static method nameEnter() that prompts the user to enter a
username and stores the input in the username variable. After the username is entered,
the method calls the diffmenu() method, which displays the difficulty menu for the
user to select a game difficulty.)

(The code above defines a method called diffmenu(), which displays a menu to
choose the difficulty level of the game. The menu is printed to the console, with
options for Easy, Medium, and Hard difficulty levels. The user is prompted to enter
their choice using the keyboard, and the input is read and stored in the mainchoice
variable. The method does not return any values, but updates the value of the
mainchoice variable which is later used to control the flow of the program.)

(The code above is a method named returnValue() which returns the value of the
static variable mainchoice. The mainchoice variable is an integer value that is set by
user input in various methods of the program, such as mainmenu() and diffmenu().
The returnValue() method allows other parts of the program to access and use the
value of mainchoice.)
(The above code defines a method "instruction" which displays the game instructions
on the console and prompts the user to confirm whether they understand the
instructions or not. If the user confirms understanding, the method sets the
"mainchoice" variable to 0, otherwise it sets it to 2. The "mainchoice" variable is used
to determine the user's choice in the game menu.)
(The code above defines a method called History() that displays the game's score
history. It first calls the readScore() method to retrieve the scores from a file, and then
clears the console screen using the clrscr() method. It then prints the title "HISTORY"
and displays the scores that have been read from the file, which are stored in the
scoreList variable. Finally, it waits for the user to input something by using the
Scanner class, which is stored in the placeholder variable.)

(The code above defines a method named Leaderboard which is used to display the
leaderboard of the game. It reads the leaderboard data from a file using the
readLeaderboard() method, clears the screen, and then prints the leaderboard in a
formatted table using the printf() method. The leaderboard displays the player's name,
score, and combo. After displaying the leaderboard, the program waits for the user to
input any text before returning to the main menu. If the leaderboard file is not found,
it will print the stack trace of the exception.)
(The program above shows a method called resetScore(), which prompts the user if
they want to reset their score. If the user inputs "Y" for yes, then the program will call
the clearScore() method to clear the user's score. If the user inputs anything else, then
nothing will happen. The method displays a series of messages to the user to confirm
their choice before taking any action.)
3. Setup.java file

(The code defines a 2D array of Strings called "notes" which is used to represent the falling
notes in the game. The array has 13 rows, each representing a different note, and 4 columns,
each representing a different lane. The default value for each element in the array is a string
of empty spaces. The array is used to display the notes falling down the screen during
gameplay.)

(The above code defines four variables: Score and Combo are integers that are set to 0
initially. buttonPressed is a private integer variable, and scanner is a static Scanner object that
is used to read user input from the command line.)
(The above code defines two methods: resetButtonPressed and gameSystem.

resetButtonPressed takes an integer value and sets the buttonPressed variable to that value.

gameSystem is a method that updates the game system. It first clears the console screen. It
then shifts the notes array elements down by one index, and creates a new note at the top of
the array with a random index.

Next, it checks the value of the buttonPressed variable. If the value is 1, it resets the combo to
0 and deducts 5 points from the score. If the value is 2, it increments the combo by 1, adds 5
points to the score, and sets the buttonPressed variable to 1.)
(The above code prints out the current state of the rhythm game. It first displays a header with
the game title and artist. It then prints out the current score and combo, and displays the notes
on the screen using a 2D String array called notes.

Next, it displays the keyboard controls that correspond to each note by printing out the
characters "w", "a", "s", and "d" in brackets. Finally, it prints out a footer to complete the
layout of the game interface.)
(The above code defines two methods: returnScore and keyInput.

returnScore returns an integer array containing the current values of the Score and Combo
variables.

keyInput is a method that runs continuously while the game is still running. It reads input
from the user using the scanner object. It then checks if the input corresponds to any of the
notes on the screen. If the input matches the position of the current note being played, the
buttonPressed variable is set to 2, which will increment the combo and score the next time the
gameSystem method is called. The method uses a for loop to iterate through each note
column to check for a match.)
CONFIGURATION

Unit : Laptop Acer Swift X

Hardware : Nvidia RTX 3050, AMD Ryzen 5 5600U, 16 GB RAM, 512 GB SSD

Operating System : Windows 11 Home

Software : Eclipse IDE & Visual Studio Code

You might also like