Implementation of Tic Tac Toe.
More...
#include <iostream>
#include <iomanip>
|
void | gameBoard (char board[][3]) |
| Initializes the game board with empty spaces.
|
|
void | displayBoard (char board[][3]) |
| Displays the current state of the game board.
|
|
void | fillBoard (char board[][3], int row, int col, char letter, bool &legalMove) |
| Fill a position on the board with a player's symbol.
|
|
void | getPosition (int &row, int &col, int &curPosition) |
| Prompts the current player to enter the row and column for their move.
|
|
bool | gameOver (char board[][3]) |
| Checks if the game is over due to all positions being filled.
|
|
void | changePlayer (int &curPlayer) |
| Switches the turn to the other player.
|
|
void | checkWin (char board[][3], bool &isOver, bool &isDraw) |
| Checks if a player has won or if the game has ended in a draw.
|
|
void | winnerMessage (int curPlayer) |
| Displays a message announcing the winner.
|
|
int | main () |
| Main function to run the Tic-Tac-Toe game.
|
|
Implementation of Tic Tac Toe.
This program allows two players to play a game of Tic-Tac-Toe in the terminal. The game supports alternating turns, win conditions, and a draw message.
- Author
- Nishan Subba @github https://github.com/nisSubba2024
- Date
- 01/08/25
◆ changePlayer()
void changePlayer |
( |
int & | curPlayer | ) |
|
Switches the turn to the other player.
- Parameters
-
curPlayer | The current player's identifier (0 for 'O', 1 for 'X'). |
◆ checkWin()
void checkWin |
( |
char | board[][3], |
|
|
bool & | isOver, |
|
|
bool & | isDraw ) |
Checks if a player has won or if the game has ended in a draw.
- Parameters
-
board | A 2D array representing the game board. |
isOver | Indicates whether the game is over. |
isDraw | Indicates whether the game ended in a draw. |
◆ displayBoard()
void displayBoard |
( |
char | board[][3] | ) |
|
Displays the current state of the game board.
- Parameters
-
board | A 2D array representing the game board. |
◆ fillBoard()
void fillBoard |
( |
char | board[][3], |
|
|
int | row, |
|
|
int | col, |
|
|
char | letter, |
|
|
bool & | legalMove ) |
Fill a position on the board with a player's symbol.
- Parameters
-
board | A 2D array representing the game board. |
row | The row index of the desired position. |
col | The column index of the desired position. |
letter | The symbol of the player ('X' or 'O'). |
legalMove | Indicates whether the move was valid. |
◆ gameBoard()
void gameBoard |
( |
char | board[][3] | ) |
|
Initializes the game board with empty spaces.
- Parameters
-
board | A 2D array representing the game board. |
◆ gameOver()
bool gameOver |
( |
char | board[][3] | ) |
|
Checks if the game is over due to all positions being filled.
- Parameters
-
board | A 2D array representing the game board. |
- Returns
- true If the board is completely filled.
-
false If there are still empty positions on the board.
◆ getPosition()
void getPosition |
( |
int & | row, |
|
|
int & | col, |
|
|
int & | curPosition ) |
Prompts the current player to enter the row and column for their move.
- Parameters
-
row | The row index provided by the player. |
col | The column index provided by the player. |
curPosition | The current player's identifier (0 for 'O', 1 for 'X'). |
◆ main()
Main function to run the Tic-Tac-Toe game.
Initializes the game board and alternates turns between two players. Checks for win conditions or a draw after each move.
- Returns
- int Exit status of the program.
◆ winnerMessage()
void winnerMessage |
( |
int | curPlayer | ) |
|
Displays a message announcing the winner.
- Parameters
-
curPlayer | The identifier of the winning player (0 for 'O', 1 for 'X'). |