If you use this project, please reference the repository :)
Table of Contents
Tic-tac-toe is a paper-and-pencil game for two players, X and O, who take turns marking the spaces in a 3×3 grid. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row wins the game.
The following example game is won by the first player, X:
Above is a flow chart showing Tic-Tac-Toe's algorithm. We made a 1-D array with a size of 9 corresponding to the Tic-Tac-Toe board, and gave values from 1 to 9.
And if you press the keypad, O or X is entered in the corresponding position array and the number of turn increases. If these inputs are repeated over and over again, the game ends and the winner is output. If there is no winner and the number of turn reaches 9, the Tic-Tac-Toe board is full with O or X, so the game ends and a draw is output.
gameIsPlay()
drawRect()
- We created a function to draw a 3x3 board using
GLCD_putPixel()
and call it fromgameIsPlay()
to use it.
- We created a function to draw a 3x3 board using
printOX()
- Output O or X in the corresponding cell ((x,y) coordinates)
- The turn is output as the ASCII code value:
- ASCII code of O is 79
- ASCII code of X is 88
void printOX(int input, int x, int y, int *turn) // print O and X at (x,y) coordinates
{
if(array[input-1] == 79 || array[input-1] == 88 ) // if the array already has O or X (TO PREVENT DUPLICATE ENTRIES)
{
GLCD_displayStringLn(Line1, "* TRY AGAIN *");
Delay(SEC_1);
}
else // there are empty entries
{
array[input-1] = *turn; // input O or X
GLCD_displayChar(x, y, *turn); // print O and X at (x,y) coordinates
changeTurn(&*turn);
count++; // increment the count
}
}
- if
winFlag
is not zero(0), display "Player O Win"
- if
winFlag
is zero, display "DRAW"