Professional Documents
Culture Documents
Advanced Tic Tac Toe v2
Advanced Tic Tac Toe v2
SOCIETY
Visayas State University
Visca, Baybay City, Leyte, PHILIPPINES
cscube@vsu.edu.ph
MENTORSHIP PROGRAM
Code Writing Exercise for CSci 13 – Fundamentals of Programming
First Semester, AY 2022-2023
Tic-tac-toe is a simple game played on a 3x3 grid with paper and pencil by two
players. Each player takes turns placing either an X or an O on the grid. The goal is to
get three of your marks in a row, either horizontally, vertically, or diagonally. The player
who accomplishes this first wins the game. It is a solved game, meaning that with
optimal play from both players, the result will always be a draw.
Figure 1. An example state of the board where player X wins the game in 7 steps.
In the following example, the first player (X) wins the game in seven steps:
Page 1 of 14 fb.me/VSUCSCube
Version 2 | 23 Dec 2022
COMPUTER SCIENCE STUDENTS’
SOCIETY
Visayas State University
Visca, Baybay City, Leyte, PHILIPPINES
cscube@vsu.edu.ph
Figure 2. The step-by-step (left-to-right) of the earlier figure resulting to player X's win.
There is no universally agreed rule as to who plays first, but in this program the
convention that X plays first is used.
Objective: In this exercise, you will apply the programming fundamentals in creating
a simple tic-tac-toe program. The procedures in the succeeding pages will serve as
your guide, but you may opt to create your own version of this exercise.
Difficulty: Advanced
Topics Applied:
• Program Control Constructs (If-Else, Pre-test Iterations, Post-test Iterations,
Nested Loops)
• Arrays (One-Dimensional, and Two-Dimensional Arrays)
• Functions
Page 2 of 14 fb.me/VSUCSCube
Version 2 | 23 Dec 2022
COMPUTER SCIENCE STUDENTS’
SOCIETY
Visayas State University
Visca, Baybay City, Leyte, PHILIPPINES
cscube@vsu.edu.ph
Implementation Guide:
Page 3 of 14 fb.me/VSUCSCube
Version 2 | 23 Dec 2022
COMPUTER SCIENCE STUDENTS’
SOCIETY
Visayas State University
Visca, Baybay City, Leyte, PHILIPPINES
cscube@vsu.edu.ph
Initialize the two-dimensional array board with character values '-'. It should
look like this:
[0] [1] [2]
Figure 3. A two-dimensional array containing a dash '-' character in all its elements.
int col
Page 4 of 14 fb.me/VSUCSCube
Version 2 | 23 Dec 2022
COMPUTER SCIENCE STUDENTS’
SOCIETY
Visayas State University
Visca, Baybay City, Leyte, PHILIPPINES
cscube@vsu.edu.ph
After declaring all properties listed in Table 1, and Table 2, the tictactoe.c
will look like this:
#include <stdio.h>
int main()
{
int row, col;
char board[3][3] = {
{'-', '-', '-'},
{'-', '-', '-'},
{'-', '-', '-'}
};
}
Page 5 of 14 fb.me/VSUCSCube
Version 2 | 23 Dec 2022
COMPUTER SCIENCE STUDENTS’
SOCIETY
Visayas State University
Visca, Baybay City, Leyte, PHILIPPINES
cscube@vsu.edu.ph
In this section, you will be creating a simple loop construct that switch between two
players of the game.
$ ./tictactoe
Player X's turn
Player O's turn
Player X's turn
Player O's turn
Player X's turn
Player O's turn
Page 6 of 14 fb.me/VSUCSCube
Version 2 | 23 Dec 2022
COMPUTER SCIENCE STUDENTS’
SOCIETY
Visayas State University
Visca, Baybay City, Leyte, PHILIPPINES
cscube@vsu.edu.ph
In this section, you will be adding support to user input and determine whether they
are valid or not.
When either of the inputs are outside range, the value of the validateInput()
function will return 0 or equivalent to false. Otherwise, it will return 1 or
equivalent to true.
8. After the code block created in step 6, create an if-else construct that satisfies
the following:
a. If the resulting value when calling the validateInput() function is 0,
do the following:
i. Print that the user input is invalid.
ii. Next, prompt the user to press any key to continue by calling the
getch() function.
iii. Decrement the playerTurn variable by 1 to reverse the value
increment that is made for every loop from step 1.
Page 7 of 14 fb.me/VSUCSCube
Version 2 | 23 Dec 2022
COMPUTER SCIENCE STUDENTS’
SOCIETY
Visayas State University
Visca, Baybay City, Leyte, PHILIPPINES
cscube@vsu.edu.ph
10. In the function main(), call the function system() with the a lone parameter
"cls" at the top of the for loop structure. This clears the terminal window every
time the program loops.
When compiled and run, the resulting program would look like this:
$ ./tictactoe
~[terminal is cleared after running program]~
Page 8 of 14 fb.me/VSUCSCube
Version 2 | 23 Dec 2022
COMPUTER SCIENCE STUDENTS’
SOCIETY
Visayas State University
Visca, Baybay City, Leyte, PHILIPPINES
cscube@vsu.edu.ph
In this section, you will be integrating the user input into the game board, by checking
whether the space has been marked or not.
11. In the function displayBoard(), create a nested for loop construct that iterates
through the board variable to display the current state of the game. Be sure to
add a space between every column and create a new line for every row.
12. In the function main(), before the function displayPlayerTurn() in the for
loop construct you called in step 4, call the displayBoard() function bearing
the board variable as a sole parameter to print the current board state in every
loop.
13. In the function updateBoard(), create a simple if-else construct that satisfies
the following conditions:
a. If the playerTurn’s value is an even integer, assign the value of 'X' to
the board’s index based on row and col values.
b. Else, assign the value of 'O' to the same index as 13a.
14. In the blank else construct in step 8b, call the function updateBoard() bearing
the parameters board, playerTurn, row, and col variables, respectively.
When compiled and run, the resulting program should look like this.
$ ./tictactoe
~[terminal is cleared after running program]~
- - -
- - -
- - -
Player X's turn
Enter row [0-2]: 2
Enter column [0-2]: 2
~[terminal is cleared after entering input]~
Page 9 of 14 fb.me/VSUCSCube
Version 2 | 23 Dec 2022
COMPUTER SCIENCE STUDENTS’
SOCIETY
Visayas State University
Visca, Baybay City, Leyte, PHILIPPINES
cscube@vsu.edu.ph
- - -
- - -
- - X
Player O's turn
Enter row [0-2]: 0
Enter column [0-2]: 1
~[terminal is cleared after entering input]~
- O -
- - -
- - X
Player X's turn
Enter row [0-2]:
Page 10 of 14 fb.me/VSUCSCube
Version 2 | 23 Dec 2022
COMPUTER SCIENCE STUDENTS’
SOCIETY
Visayas State University
Visca, Baybay City, Leyte, PHILIPPINES
cscube@vsu.edu.ph
In this section, you will be implementing an algorithm that determines whether there’s
already a game winner.
There are 8 ways to check for a winner, categorized into 3 (vertical, horizontal,
diagonal)
Vertically (Column)
[0] 'X' '-' '-' [0] '-' 'X' '-' [0] '-' '-' 'X'
[1] 'X' '-' '-' [1] '-' 'X' '-' [1] '-' '-' 'X'
[2] 'X' '-' '-' [2] '-' 'X' '-' [2] '-' '-' 'X'
Horizontally (Row)
[0] 'X' 'X' 'X' [0] '-' '-' '-' [0] '-' '-' '-'
[1] '-' '-' '-' [1] 'X' 'X' 'X' [1] '-' '-' '-'
[2] '-' '-' '-' [2] '-' '-' '-' [2] 'X' 'X' 'X'
Page 11 of 14 fb.me/VSUCSCube
Version 2 | 23 Dec 2022
COMPUTER SCIENCE STUDENTS’
SOCIETY
Visayas State University
Visca, Baybay City, Leyte, PHILIPPINES
cscube@vsu.edu.ph
15. In the function checkWinner(), Create a for loop that iterates 3 times
(rows/columns). It is possible to have a single for loop to iterate each column
and row.
16. Inside the for loop, create an if-else construct that satisfies the following:
a. If the first and second column of the current row are equal; and
b. If the second and third column of the current row are equal; and
c. If the first column of the current row is not equal to character '-'.
17. Repeat step 16 but for comparing the rows of the current column. The logic is
omitted as your exercise.
18. Outside the for loop, repeat step 16 twice but for both upward and downward
diagonals. The logic is omitted as your exercise.
19. If neither of the above steps are satisfied, the function will return a value of 0.
20. In the function displayWinner(), create an if-else construct that satisfies the
following:
a. If the playerTurn’s value is an even integer, print that player 'X' has
won the game.
b. Else, print that player 'O' has won the game.
21. In function main(), create an if construct that calls the checkWinner() function
as the condition.
22. Inside the if construct you created in step 21, call the displayWinner()
function bearing the playerTurn variable as the sole parameter to display the
winner of the game.
23. Then, call the break statement next to the displayWinner() call..
Page 12 of 14 fb.me/VSUCSCube
Version 2 | 23 Dec 2022
COMPUTER SCIENCE STUDENTS’
SOCIETY
Visayas State University
Visca, Baybay City, Leyte, PHILIPPINES
cscube@vsu.edu.ph
[Skipped to winner result; winner is only colored yellow for easy visualization]
Won Diagonally
X O O
- X -
- - X
X wins!
Page 13 of 14 fb.me/VSUCSCube
Version 2 | 23 Dec 2022
COMPUTER SCIENCE STUDENTS’
SOCIETY
Visayas State University
Visca, Baybay City, Leyte, PHILIPPINES
cscube@vsu.edu.ph
24. Finally, outside the for loop you created earlier in step 1, simply print that the
game was a draw.
Since it’s the end of the program, there’s no need for a break statement.
When compiled and run, the resulting program should be as follows:
X O X
O O X
X X O
Draw!
Page 14 of 14 fb.me/VSUCSCube
Version 2 | 23 Dec 2022