Description
Write an object-oriented program using the C++ Programming language, which allows the user to play the game of 2048, with special rules, in the command line.
The game of 2048 is a simple single player sliding block puzzle game. The board is a 4 by 4 grid, with each cell either being empty or containing a tile with a number. The board starts with only 2 tiles, with a new tile appearing in a random position after each player move. Two adjacent tiles of the same number can be combined into one tile by sliding, doubling the value of the tile in the direction of the move. The objective of the game is to reach the value 2048.
For a full understanding of the game play a few games here: https:
play2048.co
In your implementation you are expected to provide a text-based (ASCII) version of the game for 1 human player. Figure 1 illustrates what your board should look like.
Figure 1: ASCII 2048 board with one mine
Each cell has space for 4 characters. Make sure you use the right padding function to put spaces in front of the number if it has less than 4 digits, so that the cells remain aligned. Empty cells are marked with just 4 spaces. The tile with a ‘*’ is a special mine tile. More on that below.
Your program should prompt the player to enter his move as one character as follows:
• ‘W’ or ‘w’ – slide up
• ‘X’ or ‘x’ – slide down
• ‘A’ or ‘a’ – slide left
• ‘D’ or ‘d’ – slide right
(
CCE2111 Assignment Brief
) (
1
6
) (
Josef Bajada
)
When the player wants to slide the blocks to the right, any tiles which have empty tiles on their right will move to the right, leaving no empty tiles on the right in the process. After a move to the right there should be no empty tile that has a populated tile with a number on the left of it. (So be careful from where you commence the loop in each case to make sure you move all tiles accordingly such that they compact the space in the direction chosen by the user).
Similarly, the same process will take place if the user chooses to slide tiles to the left, up or down.
If when sliding in the direction chosen by the user a tile becomes directly adjacent to another tile in the same direction, that has the same number, the value of the two tiles is combined into one tile with a new number (the two added together).
So, if the user slides the board in Figure 1 to the right, the bottom right tile will become XXXXXXXXXXThe other tiles do not match, so they will just slide to the right. The only exception to this is the mine tile, marked with the ‘*’, which will combine with any tile and destroy it, leaving an empty tile free for the next tile to slide into.
Figure 2 shows the result of sliding the board shown in Figure 1 to the right.
Figure 2: ASCII 2048 board after slide right
Precedence
If 3 adjacent tiles in the same row or column have the same value, and a move to slide the board in the direction of the row or column is done by the player, the tiles closest to the edge in the direction of the move will be combined first. For example, if there is a row with tiles 2, 4, 4, 4 and the player chooses to slide to the right, the row should result in
, 2, 4, 8.
Similarly, a mine tile should combine with (and destroy) the tile in the direction of the move, unless there are no tiles in that direction, in which case the tile on the other side will combine with it (and get destroyed).
For example, if there is a row with tiles 2, *, , 8, and the player chooses to slide to the right, the tile with 8 will be destroyed, resulting in the row , , , 2. On the other hand, if the row had 2, 8, *, , and the player also chooses to slide to the right, the tile with 8 will also be destroyed, since there are no tiles on the right of the mine tile, resulting in the row
empty>, , , 2.
A tile can only combine with another tile once in the same move. So, if there is a row with tiles
empty>, 8, 4, 4, a slide to the right would result in the row: , , 8, 8. The same
applies for the mine tile. If the tile adjacent to it has just been combined from two other tiles, the mine tile will not combine with it to destroy it. (If there is another tile on the other side which has not been combined it will still combine with the mine tile).
Score
Each time two adjacent tiles are combined, the user will gain points. This should amount to the total of both tiles (the resultant value of the new tile).
Each time a tile is destroyed by a mine tile, the user will lose points. This should amount to the value of the destroyed tile. If a mine tile is combined with another mine tile, the player should lose no points.
Remember that one move could combine multiple tile pairs. Display the score underneath the board after each move.
Game Initialisation
The board starts empty, with two tiles generated randomly, each in a random position with the same probabilities above.
Gameplay
After each move, the game will randomly add a new tile at a random empty position. This new tile
must have either a 2, or a 4, or a mine tile indicated by an ‘*’.
The probability of generating a 2 must be 40%, the probability of generating a 4 must be 40% and the probability of generating a mine tile must be 20%.
If the player chooses a slide direction which does not move any tiles, this will be considered invalid and the user must be prompted again to enter his move. The user cannot pass or skip his turn without moving at least one tile.
Winning and Losing
• The player will win if at least one tile has the value 2048.
• The player will lose if after his move there is are no more empty tiles.
Functional Requirements
Entering Moves
Each user must be prompted to enter his move as one character (case insensitive). The program must then perform the following tasks:
1. Validate the user’s command.
2. Loop through the rows or columns depending on the slide direction, starting from the opposite direction, and slide and/or combine tiles together.
• If no tiles were moved or combined display a message and skip to step 5.
3. Update the player’s score if the move resulted in any tile combinations or destruction.
• If any tile has the value 2048 the player has won, display a message and skip to step 5.
• If there are no free cells left, the player has lost, display a message and skip to step 5.
4. Generate a new random tile and put it in a random empty cell.
5. Display the new board, with the updated tile values and new score.
Validations
If the user enters an invalid move, the program must display the appropriate e
or message, and prompt the same player to enter the move again.
Checks in Between Turns
After each turn the game must check whether the opponent still has empty tiles or if any tile has the value 2048, to determine whether the game ended, or whether the game is still going.
Additional Commands
Apart from entering the moves, the user should have the option to:
• ‘N’ or ‘n’ – Start a new game
• ‘S’ or ‘s’ – Save the game
• ‘L’ or ‘l’ – Load a game
• ‘H’ or ‘h’ – Display the hall of fame
• ‘Q’ or ‘q’ – Quit
If the user chooses to start a new game, load another game or quit, he should be prompted with a
message ‘Are you sure?’ to which he must respond ‘Y’/’y’ or ‘N’/’n’.
Saving the game
If a user opts to save the game, it must be saved in a text file in the following format:
Score:256
0:2
1:32
5:*
The first line indicates the score of the player so far, while the rest of the lines indicate the tile index and the value in the tile. If the value is a ‘*’ it indicates a mine tile. Tiles are numbered from 0 (the topmost leftmost tile) to 15 (the bottommost rightmost tile).
The name of the file must be chosen by the user upon saving.
Loading the Game
The user must be able to enter the filename of the game he wants to load. If the user enters the wrong filename (file does not exist or is invalid) an appropriate e
or must be displayed and the user must return to the previous game.
Hall of Fame
If the player wins the game, he should be prompted to write his name. This is saved to a file named
‘HallOfFame.txt’. It should also be in text format containing the player name, and the score:
Mario Cachia:3028
Joseph Fa
ugia:4096
David Agius:3082
When writing the file make sure you do not erase the previous winners.
The hall of fame should be displayed at the end of each game, after the user loses, or after he wins and enters his name. It should also be displayed upon request, if the user chooses option ‘H’ (after which the game resumes).
When displaying the hall of fame, make sure you sort it with the top scorer first, in descending score order. (They do not need to be sorted in the file.)
Non-Functional Requirements
You must demonstrate a good understanding of C++ and Object Oriented Programming. Your program must be organised in classes, using the right concepts, such as composition, inheritance and polymorphism.
Make sure you follow proper coding conventions and your code is tidy, consistently indented and classes and variables appropriately named. You can choose any convention you wish, such as: https:
google.github.io/styleguide/cppguide.html
Deliverables
You need to submit the source code. That is, the .cpp and .h files of your program. Organise them properly in a folder and make sure they compile in Microsoft Visual C++. (Try your program at the university lab to make sure it works)