Tic Tac Toe
 
Loading...
Searching...
No Matches
ticTacToe.cpp File Reference

Implementation of Tic Tac Toe. More...

#include <iostream>
#include <iomanip>

Functions

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.
 

Detailed Description

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

Function Documentation

◆ changePlayer()

void changePlayer ( int & curPlayer)

Switches the turn to the other player.

Parameters
curPlayerThe 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
boardA 2D array representing the game board.
isOverIndicates whether the game is over.
isDrawIndicates whether the game ended in a draw.

◆ displayBoard()

void displayBoard ( char board[][3])

Displays the current state of the game board.

Parameters
boardA 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
boardA 2D array representing the game board.
rowThe row index of the desired position.
colThe column index of the desired position.
letterThe symbol of the player ('X' or 'O').
legalMoveIndicates whether the move was valid.

◆ gameBoard()

void gameBoard ( char board[][3])

Initializes the game board with empty spaces.

Parameters
boardA 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
boardA 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
rowThe row index provided by the player.
colThe column index provided by the player.
curPositionThe current player's identifier (0 for 'O', 1 for 'X').

◆ main()

int 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
curPlayerThe identifier of the winning player (0 for 'O', 1 for 'X').