0% found this document useful (0 votes)
9 views34 pages

Tut7 1540

This document outlines the details for Assignment 4 of the CSCI 1540 course, which involves creating a two-player Wild Tic-tac-toe game using C++. It specifies the due date, basic requirements for submission, and the functions that need to be implemented, along with guidelines for coding and testing. Additionally, it includes instructions for setting up the project in Visual Studio and the program flow for the game logic.

Uploaded by

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

Tut7 1540

This document outlines the details for Assignment 4 of the CSCI 1540 course, which involves creating a two-player Wild Tic-tac-toe game using C++. It specifies the due date, basic requirements for submission, and the functions that need to be implemented, along with guidelines for coding and testing. Additionally, it includes instructions for setting up the project in Visual Studio and the program flow for the game logic.

Uploaded by

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

CSCI 1540 Introduction to Computing

Using C++

Tutorial 7 : Assignment 4

1
About TAs
• LI Yunlun
 yunlun.li@link.cuhk.edu.hk

 Consultation Hour: Wednesday, 14:00 - 16:00


 Office: SINO 527

• I will be responsible for Tutorial 7

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

•Open the Solution


Explorer
•In View -> 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

•Select the provided file in the same


directory with the
"shisimagame.cpp" file
•Click "Add" to add the file
•For Source Files, select
“wildttt.cpp” and “wtttgrid.cpp”
•Similarly, for Header Files, select
“wtttgrid.h”

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

• 1–3 denote the spaces in the top row


• 4–6 denote the spaces in the second row
• Order: left to right, top to bottom. • 7–9 denote the spaces in the third row

12
Game Grid Representation

Game network:

representation: 120210012 020010000

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.

int printGrid(int grid)  void printGrid(int grid, int pos) 


• You can design extra functions if you find necessary, but these three functions
must be used.

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.

grid: 102102102 grid: 2112102

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

• Winning Situations for Tic-Tac-Toe:


• No Zeros in Line:
• To win, a line must contain no empty cells (0). The values should all belong to one player (1 or 2).
• Row line:
• start at positions 1, 4, 7 and have a difference of 1 between cells within the same row.
• Column line:
• start at positions 1, 2, 3 and have a difference of 3 between cells within the same column.
• Diagonal line:
• Case 1: Positions 1, 5, 9 (top-left to bottom-right).
• Case 2: Positions 3, 5, 7 (top-right to bottom-left).

19
Check Whether Line formed

• Method 1: Leading Position Check


• Check the Leading Position:
• For each row, column, or diagonal, start by checking the leading position.
• If the leading position is marked with a player's value (not equal to 0), proceed to check the rest of the line.
• Verify the Rest of the Line:
• After identifying the leading position (starting cell), check if the following two positions have the same value.
• Method 2: Loop Through All Winning Lines
• Predefined Winning Lines:
• Since the Tic-Tac-Toe grid is only a 3x3 grid, there are only 8 possible winning lines (3 rows, 3 columns, and 2
diagonals).
• Loop through each of these winning combinations and check if all the cells have the same value and are not
empty (0).

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

if gridState(grid, 1) NOT EQUAL 0 AND gridState(grid, 1) EQUAL gridState(grid, 2)


AND gridState(grid, 2) EQUAL gridState(grid, 3):
return true
if gridState(grid, 4) NOT EQUAL 0 AND gridState(grid, 4) EQUAL gridState(grid, 5)
AND gridState(grid, 5) EQUAL gridState(grid, 6):
return true
……
/*List all 8 valid Lines*/
return false

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

Enter Prompt for Next Move


Positions
No
Prompt the current player to enter mark and
position integer to mark a target position.
Valid
Input?

Yes
Swap
Player
Update Grid

Game Display
Yes
Over? Message
No
End
27
Start
Program Flow
Game Initialization

Enter Valid Input Check


Positions
No
Check whether the input positions are valid
for the current player. The input is valid only
Valid if:
Input?
1. Pos are 1-9.
Yes
Swap 2. The state of pos is 0, i.e., empty.
Player
Update Grid 3. The mark is either “X” or “O”(NOTE: marks
are UPPERCASE)
Game Display
Yes
Over? Message In case the input positions are invalid, display
No a warning message “Invalid! Try again.” and go
End back to Enter Position.
28
Start
Program Flow
Game Initialization

Enter Update Grid


Positions
No
Update the grid by mark the target position.
Valid
Input? Hint
Swap
Yes Call the function updateGrid() here.
Player
Update Grid

Game Display
Yes
Over? Message
No
End
29
Start
Program Flow
Game Initialization

Enter Game Over Check


Positions
No
Before next player move, check whether the
current game is over,
Valid
Input? • A player has formed a line of three piece.
Yes
• No empty space in the grid.
Swap
Player
Update Grid
Hint
The first situation is the output of
Game Display updateGrid() function.
Yes
Over? Message
No
End
30
Start
Program Flow
Game Initialization

Enter Game Over Information Print


Positions
No
Once the game is finished, display the
message “Player 1 wins!” or “Player 2 wins!”
Valid
Input?
accordingly.
Note that there is draw game; the two
Swap
Yes players occupied all the space in the grid, but
Player no one have form a line, display the message
Update Grid “Draw game! ”

Game Display
Yes
Over? Message
No
End
31
Program Flow Example

State of pos is not 0

Wrong mark

Pos is not in 1-9

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy