Hello, if you have any need, please feel free to consult us, this is my wechat: wx91due
CSCI1120 Introduction to Computing Using C++, Fall 2024/25
Introduction
The objective of this assignment is to practice (1) the use of 1-D and 2-D arrays, and (2) the use of pointers for passing 1-D arrays as arguments between functions.
You will write a program to simulate a board game called Mathable, which is commonly described as being like playing Scrabble but using numbers instead of letters. Its detailed game rules can be found on websites like [1] [2] [3]. An iPad or iPhone app of this game is available on App Store if you have interest to try it for better ideas on how to play the game. Here we give a brief introduction.
The board game consists of:
- Game board: a square game board of N × N cells or squares where players place their tiles.
- Tokens (or tiles): each tile or numbered token displays a 1-digit or 2-digit number.
- Racks (or token holders): each player owns a rack for privately holding a handful (R) of tokens.
- Bag: a bag contains all the rest of the tokens. A player can draw tokens from it to refill their rack after a turn or can swap (some of) the tokens on their rack with the ones in the bag randomly if they find it hard to form an equation on the board using their current tokens.
- Score pad: used to track players' scores throughout the game.
Note that a variant of the game tailored to younger children, known as Mathable Junior, consists of a smaller game board and fewer tokens. See Table 1 below for their differences.
Table 1: Game configuration parameters of Mathable and Mathable Junior
|
|
Mathable |
Mathable Junior |
|
Number of players (P) |
2 to 4 |
2 to 4 |
|
Board size (N) |
14 ×14 |
10 ×10 |
|
Rack size (R) |
7 |
5 |
|
Number of tokens (T) |
106 |
60 |
|
Token list |
|
|
Game Board Representation
Each cell in the grid is addressed by an Excel-like addressing scheme, e.g., the cell located at row 2 and column 3 is referred to by a cell address C2. The initial configuration of the game board has fixed four tokens, valued 1, 2, 3, 4, in the center of the board. Token 1 is located at G7, token 2 at H7, token 3 at G8, and token 4 at H8. In the program, we use a 2-D int array called board to store the tokens placed on the game board. Corresponding to these initial 4 tokens, we have: board[6][6] = 1; board[6][7] = 2; board[7][6] = 3; board[7][7] = 4;
Game Logic
where ? is one of the four arithmetic operators (+, −, ×, or ÷).
Recap the official game instruction: “A math equation is completed by adding, subtracting, multiplying or dividing two adjacent numbers putting a token with the result on the next empty square, be it to the right or left, below or above the two original tokens. Equations may be made horizontally or vertically, but never diagonally.”
For example, for the initial game board, if your rack has token 7, you can play it at cell F8 or cell I8 (refer to Figure 1) to form the equation 3 + 4 = 7. If you don’t have token 7 but token 1, you can also put it at cell F8 or cell I8 because this can form the equation 4 – 3 = 1. You can put it next to token 4 or token 3, on either side of the two tokens, it doesn't matter. Similarly, you may also put token 12 at these two cells to form the equation 3 × 4 = 12. An equation can also be formed vertically. See Figure 2 for more examples of valid positions for placing a token onto the board.
Remember that the token you are going to place is always the result of the math operation. It is illegal to place it between the two adjacent tokens (operands) as we can see in Figure 3. Equations are also never formed diagonally. A token placement is valid as long as it can form at least one equation with two consecutively adjacent tokens in any of the 4 directions (left, right, above, below). See Figure 4. Now, placing token 7 between 3 and 4 is valid because it can form an equation with 2 and 5 vertically. For the example on the right, although 7 cannot form an equation with 1 and 2 vertically, it does with 3 and 4 horizontally. So, it is valid to place token 7 there.
Figure 3: Example invalid token placements
Figure 4: More examples of valid token placements
Bonus for multiple equations: Another strategy to score higher is to gain bonus points due to simultaneously forming multiple equations by one move. If a token, when being placed, completes more than one equation, the points are gained for each equation completed. See an example in Figure 5, which is actually the first move (placing token 4 at cell H4) in Round 13 Player 1’s turn in the Mathable Junior sample run 1 logged on macOS (mathable-jr-run1-mac.txt).
Figure 5: One move completing 3 equations, scoring 12 points (3 times 4 points)
Total Score: The total amount of points earned per turn is the total sum of the points scored by all moves made (i.e., all tokens placed plus bonuses) in that turn.
Game-over Conditions: The game continues until (1) there are no tokens left in the bag, and one of the players has used up all the tokens on his rack, or until (2) no more equations can be made. Since detecting condition (2) is a bit clunky, our program design will give players an option (T) to terminate the game anytime they want to. When players see no more possible moves, one of them will select this option to determine the game winner. A draw game can happen if more than one players attain the same highest score.
Simplifying the Game
• “Extra Bonus. If a player manages to place all the tokens he has on his holder in one turn, he earns an extra bonus of 50 points.”
Program Specification
Basic Requirements
- Apart from our given global constants in the mathable.h header file, you cannot declare any global variables in all files.
- Your program is scalable to board size (8 ≤ N ≤ 14, even number only), player size (2 ≤ P ≤ 4), rack size (4 ≤ R ≤ 8), token amount (either T = 60 or T = 106), which could be changed at compile time (not hardcoded in the program code).
- You cannot use any vectors or self-defined classes (they will be used in later assignments).
Starter Code
- To help you get started, you are provided with all the required source files:
|
File name |
What’s inside |
|
mathable.h |
Several global constants defining the board size, rack size, player count, total number of tokens and the function prototypes of the functions required by the client’s main() function. |
|
mathable.cpp |
The source file implementing all the functions needed by the game, e.g., printing the game board, rack, drawing tokens from bag, validating and making moves on the board. |
|
mathable-game.cpp |
The client source file implementing the main() function which contains the main game loop for alternating turns across players. It includes the header file and calls the functions provided by the above source file. |
- Basically, you need not change the header file unless you need to add extra functions to mathable.cpp and their function prototypes to the header file.
- To achieve the second requirement mentioned above, the header file has defined some global constant integers for scaling board size, player count, rack size and token count easily.
- We may grade your program with them changed to a different value within their valid range specified. When you test your program against smaller or bigger boards, you can change N to other values but remember to reset it to its original value before your submission.
- To make the program scalable, whenever you have need to use the board size, say, you should use the named constant N instead of hardcoded values throughout every source file. For simulating empty cells on the board, we can’t use 0 for the board array elements since token 0 may be placed on the board. A possible choice is to initialize the elements to -1. But instead of hardcoding -1 throughout the program, a more scalable implementation should use the global constant BLANK which is set to -1 in mathable.h.
- You may change the preprocessor flag JUNIOR on line 7 in mathable.h from 0 to 1 if you want to compile the program as the Mathable Junior variant, which uses a smaller board size and rack size, etc. This can already help test if your program is scalable.
- The source files have organized the entire program into different functions with well-defined parameter lists. Your main task in this assignment is to search for all “TODO” comments and fill in the missing parts of code.
Player Actions:
Your program is to prompt the user to enter one of the four letters: P, S, E, or T (case-insensitive) to perform one of the above actions for the current turn.
Playing a Token: If the player chooses P, the program prompts the player to enter a move, which consists of the target cell address, followed by the value of a token on his rack. The following shows an example where the player places token 4 at cell G9, and then places token 8 at cell H6. He may enter as many valid moves as possible based on the available tokens on his rack. The player then enters E to end the current turn and pass the turn to the next player.
Swapping Tokens: Note that the program implements the option S (Swap) is to support this feature in the game rules: “A player may make use of his turn to exchange one or more tokens on his holder for new ones … To play, he must await his next turn.” So, a player who wants to swap for new tokens from the bag must choose S at the beginning of the turn. If the player has chosen P (Play), he cannot choose S anymore, and this option should be hidden throughout the current turn. See below for an example prompt for swapping tokens. It first asks for how many tokens on the rack are to be swapped out. Then the program loops that many times to prompt for each token and replace it on the rack with a randomly drawn token from the bag.
Your program must contain the following functions. Most of them have been written for you already (Provided) and you shall not modify their contents. The others shall be written by you (Required).
These functions shall be implemented in the source file mathable.cpp with their prototypes in the header file mathable.h. These functions shall be called somewhere in your program. You must not modify the prototypes of all these functions. You can design extra functions if you find necessary.
Besides, all the required functions below will be graded individually. That is, we shall connect your mathable.cpp with another source file with our testing code to call the functions one by one for grading. So, your code for each function shall implement the description of that function only. You shall not write any code in a function that is beyond that function’s description.
The function also accumulates the points gained by possible multiple equations formed by this move and passes it back to the caller via the points reference parameter.
For example, for a move prompt, the program expects one character and one integer like A1 for the cell address, followed by some whitespace and then an integer for the token. The user won’t enter something else like “AB C”, “@#$”, “1A 2”, etc. But the values should still be validated, e.g., the user may enter “Z1 3” and the program should tell it is an invalid input because the maximum board is up to column N only and won’t have column Z. Also, inputs should be assumed case-insensitive, location “A1” and “a1” or action “p” and “P” should both be acceptable.