0% found this document useful (0 votes)
22 views5 pages

Assg 4

Uploaded by

Mamat Rahmat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views5 pages

Assg 4

Uploaded by

Mamat Rahmat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CSCI1540 Fundamental Computing with C++, Fall 2024/25

Department of Computer Science and Engineering, The Chinese University of Hong Kong

Assignment 4: Wild Tic-tac-toe


Due: 23:59, Sat 2 Nov 2024 File name: wtttgrid.cpp Full marks: 100
wildttt.cpp
wtttgrid.h

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

Copyright © 2024 CSE, CUHK Page 1 of 5


CSCI1540 Fundamental Computing with C++, Fall 2024/25
Department of Computer Science and Engineering, The Chinese University of Hong Kong

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.

Provided and Required Functions (wtttgrid.cpp and wtttgrid.h)


Your program must contain the following functions. One of them is written for you already (Provided)
and you shall not modify its contents. The others shall be written by you (Required). These functions
shall be implemented in the source file wtttgrid.cpp with the function prototypes in the header
file wtttgrid.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.

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.

(Provided) void printGrid(int grid)


This function prints grid to the screen using the format in Figure 1.

(Required) int gridState(int grid, int pos)


Returns 0 if position pos of grid is unfilled; 1 if position pos is filled with an X mark; 2 if position
pos is filled with an O mark.

(Required) bool updateGrid(int &grid, int pos, char mark)


This function updates the posth digit of the reference parameter grid to 1 if mark is ‘X’, or to 2 if
mark is ‘O’. The other digits of grid remain unchanged. This function models the game move from a
player putting a mark in position pos in grid. It shall return true if a line of three same marks is
formed horizontally, vertically, or diagonally after the move; and shall return false otherwise. The
following shows some sample function calls and the expected results.

Parameter Parameter Parameter Calling updateGrid(grid, pos, mark)


grid pos mark Value of grid after the call Return value
110202020 5 ‘O’ 110222020 true
210102021 3 ‘X’ 211102021 false
201010000 7 ‘X’ 201010100 true
0 8 ‘O’ 20 false

Copyright © 2024 CSE, CUHK Page 2 of 5


CSCI1540 Fundamental Computing with C++, Fall 2024/25
Department of Computer Science and Engineering, The Chinese University of Hong Kong

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.

Program Flow (wildttt.cpp)


The program flow of the game is described as follows and shall be implemented in the source file
wildttt.cpp. You shall call the functions above to aid your implementation.

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.

Functions and Files Organization


• The file wtttgrid.cpp must contain the implementation of the provided and required
functions. There must be no main() function in the file. (You may write a main() in this .cpp
file during your development for debugging purpose, but it must be removed or commented in
your submitted version.) You may write extra functions in this file if you find necessary.
• The header file wtttgrid.h must contain the prototypes of the provided and required
functions. You may put in extra function prototypes if you have written extra functions in
wtttgrid.cpp. You must add #include "wtttgrid.h" at the top of wtttgrid.cpp and
wildttt.cpp.
• The file wildttt.cpp must contain the main() function for the program flow. You may write
extra functions in this file if you find necessary. You must add #include "wtttgrid.h" in
this .cpp file (but not #include "wtttgrid.cpp").

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.

| |
---+---+---
| |
---+---+---
| |

Copyright © 2024 CSE, CUHK Page 3 of 5


CSCI1540 Fundamental Computing with C++, Fall 2024/25
Department of Computer Science and Engineering, The Chinese University of Hong Kong

Player 1's move: X 5↵


| |
---+---+---
| X |
---+---+---
| |
Player 2's move: X 0 ↵
Invalid! Try again.
Player 2's move: Q 10↵
Invalid! Try again.
Player 2's move: @ -7↵
Invalid! Try again.
Player 2's move: O 5↵
Invalid! Try again.
Player 2's move: O 2↵
| O |
---+---+---
| X |
---+---+---
| |
Player 1's move: O 9↵
| O |
---+---+---
| X |
---+---+---
| | O
Player 2's move: X 1↵
X | O |
---+---+---
| X |
---+---+---
| | O
Player 1's move: X 8↵
X | O |
---+---+---
| X |
---+---+---
| X | O
Player 2's move: O 4↵
X | O |
---+---+---
O | X |
---+---+---
| X | O
Player 1's move: X 6↵
X | O |
---+---+---
O | X | X
---+---+---
| X | O
Player 2's move: O 3↵

Copyright © 2024 CSE, CUHK Page 4 of 5


CSCI1540 Fundamental Computing with C++, Fall 2024/25
Department of Computer Science and Engineering, The Chinese University of Hong Kong

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!

Submission and Marking


➢ Your program file names shall be wtttgrid.cpp, wildttt.cpp, and wtttgrid.h. Submit the
three files in Blackboard (https://blackboard.cuhk.edu.hk/). If you do not submit the .h, we shall
assume that it is the same as the provided one.
➢ Insert your name, student ID, and e-mail as comments at the beginning of all your files.
➢ You can submit your assignment multiple times. Only the latest submission counts.
➢ Your program should be free of compilation errors and warnings.
➢ Your program should include suitable comments as documentation.
➢ Do NOT share your work to others and do NOT plagiarize. Both senders and plagiarists shall be
penalized.

Copyright © 2024 CSE, CUHK Page 5 of 5

You might also like