Tut7 1540
Tut7 1540
Using C++
Tutorial 7 : Assignment 4
1
About TAs
• LI Yunlun
yunlun.li@link.cuhk.edu.hk
2
Basic Information
• Assignment 4: Wild Tic-tac-toe
• Due: Sat 2 Nov 2024.
• Objective: (1) defining functions (2) calling functions (3) representing special kind of data.
• Basic requirements:
• Filename: wtttgrid.cpp, wildttt.cpp, and wtttgrid.h. Submit the three files in one submission
attempt.
• Insert your name, student ID, and e-mail address as comments at the beginning.
• Free of compilation errors and warnings
• Suitable comments : help us to understand your program
• You cannot declare any global variables.
• You cannot use any functions in the <cmath> library.
• (Note: <math.h> is the same as <cmath>)
• You cannot use any arrays or vectors in this assignment.
• Do NOT plagiarize!!
• Important: The required functions will be graded separately with designed main codes,
not just the game flow. 3
Create a Visual Studio Project with multiple
files
• (1). As all three files (wildttt.cpp, wtttgrid.cpp and wtttgrid.h) are already provided
for you, you are recommended to download them to your local storage before you
start.
• (2). Create the empty wildttt project in Visual Studio (remember the saving location
of the project).
• (3). Add provided files (this part will be introduced in detail with screenshots) to the
project
• Open the Solution Explorer (View -> Solution Explorer).
• In Solution Explorer -> Source Files (or Header Files) -> Add -> Add Existing Item
4
Open Solution Explorer
5
Add an Existing Item
•The way to add the .cpp file
and the .h file is a bit
different.
•The .h file should be added
through Solution Explorer ->
Header Files -> Add ->
Existing Item
6
Add an Existing Item
•The way to add the .cpp file
and the .h file is a bit
different.
•The .cpp file should be added
through Solution Explorer ->
Source Files -> Add ->
Existing Item
7
Add an Existing Item
8
Check the Added Files and Open them
•You can find the added files in the
"Source Files” and ”Header Files”
•Click file to open it in the editor
•You can then edit the files and start
coding
9
Introduction to Assignment 4
• Implement a two-player grid game, called Wild Tic-tac-toe.
• 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.
10
Win and Lose of the Game
• 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.
• Game example
11
Game Grid Representation
• In this assignment, we use integers 1 • To encode the whole grid and the players’ position in
to 9 to denote these grid positions. a game, we use a 9-digit integer:
Grid: 0, 1, or 2, 0 means
empty, 1 means X mark, 2
means O mark.
𝑑1 𝑑2 𝑑3 𝑑4 𝑑5 𝑑6 𝑑7 𝑑8
𝑑9
12
Game Grid Representation
Game network:
20010000
• The data type int is typically 32-bit, which is big enough to store a 9-digit integer.
13
Functions to Implement
• In total three functions are required:
int gridState(int grid, int pos)
void printGrid(int grid)
bool updateGrid(int &grid, int pos, char mark)
• The three functions must be written in wtttgrid.cpp, and they shall be called
somewhere in your program.
• You must not modify the prototypes of all these functions.
14
Function void printGrid(int grid)
• The function is already given, DO NOT modify.
• Input: int grid , which prints the game grid to the screen.
• Functionality: print the game grid according to the input.
• No return value.
15
Function int gridState(int grid, int pos)
• Input: int grid and int pos, representing the game grid and a position on the
grid, respectively.
• Functionality: Determine the state (0, 1, or 2) of the specified position pos on the
game grid.
• Return: The state (0, 1, or 2) of the given position pos.
gridState(102102102, 4) gridState(120120210, 7)
Return: 1 Return: 2
• Hints:
• Example: • Divide and Mod Calculation.
• Do not use the pow() function in <cmath>, write a
1. grid: 102102102; pos: 4 loop to do the power calculation instead.
• the function should remain effective even when
2. 102102102 / 10(9-4) =1021 there are leading zeros (i.e., when the integer has
less than 9 digits). E.g., gridState(120, 4) returns 0
3. 1021 % 10 = 1 16
Function bool updateGrid(int &grid, int pos,
char mark)
• Input: int grid , int, char mark, representing the game grid, the target position and the
player mark, respectively.
• Functionality: Determine whether a line of three same marks is formed horizontally,
vertically, or diagonally after the move.
• Return: A Boolean value indicating a line is formed or otherwise (True/False)
• Hints:
• You shall not print anything with “cout” in this function.
• calling the gridState() function would be useful.
17
Update Game Grid
• Grid is a 9-digit integer; thus, you can use subtraction
and addition to update the integer.
• 110202020 -> 110222020 : player 1 marks pos 5 as
“O”.
• 110202020 + 2*(10(9-5)) = 110222020
18
Check Whether Line formed
19
Check Whether Line formed
20
Pseudo Code for Method 1
// Check rows
for i from 1 to 9 step 3:
if gridState(grid, i) NOT EQUAL 0 AND gridState(grid, i) EQUAL gridState(grid,
i + 1) AND gridState(grid, i + 1) EQUAL gridState(grid, i + 2):
return true
// Check columns
for i from 1 to 3:
if /*formed a column*/:
return true
# Check diagonals
if /*formed a line from top left to bottom right*/:
return true
if /*formed a line from top right to bottom left*/:
return true
return false
21
Pseudo Code for Method 2
22
Add New Function
• You can add extra functions in wtttgrid.cpp if you find necessary.
• If you add extra functions in wtttgrid.cpp , you must add corresponding function
prototypes in the header file wtttgrid.h.
Add here
23
Function Testing
• You are recommended to write and test your functions one by one. We will also
mark your functions one by one and finally go to program flow.
• Write a "driver" main to call your functions individually.
// wtttgrid.cpp
/* Returns the state of position pos of the game grid. */
int gridState(int grid, int pos) {…}
// wtttgrid.cpp
/* Code for testing int gridState(int grid, int pos); */
// write a main() in wtttgrid.cpp for debugging purpose(MUST be removed or
commented before submitted)
int main() {
int grid = 2111022; // try different grids
int pos = 5; // try different positions
printGrid(grid); //print the game grid
cout << gridState(grid, pos) << endl;
return 0;
}
• Expected output is 1. You may use the printGrid() function to help testing.
• Try different grids and different positions to test and be familiar with the usage of this function. 24
Function Testing
// wtttgrid.cpp
/* Perform the task of player i mark the position pos*/
bool updateGrid (int &grid, int pos, char mark){…}
// wtttgrid.cpp
/* Code for testing bool updateGrid(int &grid, int pos, char mark); */
int main(){
int grid = 2111022; // try different grids
int pos = 7, mark = “O”; // try different positions and players
if (updateGrid(grid, pos, mark)){
cout << “Line formed" << endl;
}
else {
cout << “Continue Playing" << endl;
}
printGrid(grid); //print the game grid
return 0;
}
• Expected output: Line formed. You may use the function printGrid() to help testing.
• Try different Grids, positions and players to ensure the function is correct.
25
Start
Program Flow
Game Initialization
Enter Prerequisite
Positions
No
• The program flow must be implemented in
wildttt.cpp.
Valid
Input? • Remember to add #include wtttgrid.h in
wildttt.cpp.
Yes
Swap
Player
Update Grid Game Initialization
The program starts the game with an initial grid
Game Display (0).
Yes
Over? Message
Player 1 takes the first turn.
No
End
26
Start
Program Flow
Game Initialization
Yes
Swap
Player
Update Grid
Game Display
Yes
Over? Message
No
End
27
Start
Program Flow
Game Initialization
Game Display
Yes
Over? Message
No
End
29
Start
Program Flow
Game Initialization
Game Display
Yes
Over? Message
No
End
31
Program Flow Example
Wrong mark
32
Assignment Submission
• Your program file name should 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.
• Due for assignment 4: 23:59, Sat 2 Nov 2024.
• Do NOT plagiarize. Sending your work to others is subjected to the same penalty as
the copier.
33
Q&A