Assg 4
Assg 4
Department of Computer Science and Engineering, The Chinese University of Hong Kong
Introduction
The objective of this assignment is to practice (1) defining functions (being a callee), (2) calling
functions (being a caller), and (3) representing special kind of data.
You will write a program to play a paper-and-pencil game called Wild Tic-tac-toe, which is a variant
of the classic paper-and-pencil game Tic-tac-toe. In the game, two players 1 and 2 take turns to mark
the spaces in a 3 × 3 grid with either marks X or O of their choice. A player wins by completing three
same marks in a horizontal, vertical, or diagonal line in the grid. The game is a draw when the grid is
full but no player wins. Figure 1 shows an example move by Player 1, who puts an O to the center
and forms a horizontal line of three O’s in the second row to win the game.
X | X | X | X |
---+---+--- Player 1 puts an ---+---+---
⋯→ O | | O O to the center. O | O | O Player 1 wins.
---+---+--- → ---+---+---
| O | | O |
Figure 1: An Example Game Move Making Player 1 the Winner
Program Specification
This section describes the requirements, grid representation, some necessary functions, and
program flow.
Basic Requirements
• You cannot declare any global variables (variables declared outside any functions) in all files.
• You cannot use any functions in the <cmath> library.
• You cannot use any arrays nor vectors.
Grid Representation
There are nine possible positions in a 3 × 3 grid. Therefore, we use integers 1–9 to denote these
positions, where 1–3 denote the spaces in the top row of a grid left-to-right, 4–6 to denote the
spaces in the second row, and 7–9 to denote the spaces in the third row. The positions are basically
ordered top-to-bottom, left-to-right, as illustrated in Figure 2.
X | X |
Position 1 ---+---+---
O | O | O
---+---+--- Position 9
| O |
Figure 2: Grid Space Positions
To encode the whole grid of a game, we use a 9-digit integer 𝑑1 𝑑2 𝑑3 𝑑4 𝑑5 𝑑6 𝑑7 𝑑8 𝑑9 . Each digit 𝑑𝑖
stores the state of position 𝑖, which can be either 0 or 1 or 2. Value 0 denotes an empty space in the
position. Value 1 denotes that the space is filled with an X mark. Value 2 denotes that the space is
filled with an O mark. E.g., the grid in Figure 2 is encoded by the integer 110222020. Using this
representation, an empty grid (with no marks) is simply encoded as the integer 0 (all positions being
0). A full grid is encoded as an integer which contains only digits 1 and 2 but no 0. E.g., 122211112 is
a draw game. Recall that the int data type is typically 32-bit and has a range
−2147483648 … 2147483647. Therefore, a 9-digit integer fits into an int without overflow
problem, and we can simply use one int variable to store the grid. While this design is memory
compact, it may not be the most efficient in terms of programming. Nonetheless, you must still
follow this design in this assignment.
In the functions below, you can assume that (a) parameter grid is always a proper encoding of a
wild tic-tac-toe grid; (b) the parameter pos is always 1–9; and (c) the parameter mark is always
either ‘X’ or ‘O’.
Besides, all the required functions below will be graded individually. That is, we shall connect your
wtttgrid.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.
At the end of this function, the value of grid becomes the new grid configuration after a player
move. Note that you do not have to check in this function whether the player move is valid or not.
(You shall validate elsewhere. See step 3 in the next section.) Besides, you shall not print anything
using cout in this function.
1. The game starts with an empty grid. Player 1 takes the first turn.
2. Prompt the current player to enter a mark and a position for a game move. You can assume that
the inputs always consist of a character for the mark followed by an integer for the position. E.g.,
entering X 6 means putting mark X to position 6 of the grid.
3. The above user inputs are valid only if (a) the mark is either ‘X’ or ‘O’, and (b) the position is 1–9,
and (c) the position is empty (not filled). In case the above inputs are not valid, display a warning
message and go back to step 2.
4. Update the grid by putting the input mark to the corresponding position.
5. Swap the other player to become the current player.
6. Repeat steps 2 to 5 until there is a winner or the grid is full. (That is, until game is over.)
7. Determine the winner, if any, and display the message “Player 1 wins!”, “Player 2 wins!”, or
“Draw game!” accordingly.
Sample Run
In the following sample run, the blue text is user input and the other text is the program printout.
You can try the provided sample program for other inputs. Your program output shall be exactly the
same as the sample program (same text, symbols, letter case, spacings, etc.). Note that there is a
space after ‘:’ in the input prompts.
| |
---+---+---
| |
---+---+---
| |
X | O | O
---+---+---
O | X | X
---+---+---
| X | O
Player 1's move: O 7↵
X | O | O
---+---+---
O | X | X
---+---+---
O | X | O
Draw game!