Tic-tac-toe is the classic O's and X's game where two players play on a 3x3 board. Initially, the game starts with an empty grid, and each player assigns a unique symbol ( X
or O
). The first player who places consecutive symbols of one kind in a complete horizontal, vertical, or diagonal manner wins the game.
In our implementation, we will follow the below-mentioned flow to create this game.
Set the board
Display the board
Player input
Make a move
Win or draw
First of all, we will set the board for this game. We will use a vector matrix to initialize the 3x3 board with a space character. The code to do this is as follows.
vector<vector<char>> board(3, vector<char>(3, ' '));
In the game, we need to display the board after every move so that the user must be familiar with the state of the game and can decide what to do on the next move. We can iterate over the whole board and print all the values.
void displayBoard(const vector<vector<char>>& board){for (int i = 0; i < BoardSize; ++i){for (int j = 0; j < BoardSize; ++j) {cout << board[i][j] << " ";}cout << endl;}cout << endl;}// Here BoardSize is the global variable.
In this step, we will take the box's location from the user where they want to place the symbol in a row and a column.
cout << "Player " << currentPlayer << ", enter your move (row and column): ";cin >> row >> col;
After the players have placed a symbol, they need to decide whether a player has won the game or drawn it. If either is not the case, then the game should continue. The code for making a move is given below.
bool makeMove(vector<vector<char>>& board, int row, int col, char player){if (row >= 0 && row < BoardSize && col >= 0 && col < BoardSize && board[row][col] == ' '){board[row][col] = player;return true;}return false;}
The makeMove
function checks if the input specified by the player lies within the board and that particular box is empty. If it is, the function will place a symbol. Otherwise, the function will return false.
Checking for the win or draw is essential for every game. It decides whether the game has to stop or it should continue. In tic-tac-toe, we check the board's columns, rows, and diagonals to verify whether a player has won.
// Checking rows if a player has won.bool rowWin(const vector<vector<char>>& board, char player){// Check rows and columnsfor (int i = 0; i < BoardSize; ++i){if (board[0][i] == player && board[1][i] == player && board[2][i] == player)return true;}return false;}// Checking rows if a player has won.bool colWin(const vector<vector<char>>& board, char player){for (int i = 0; i < BoardSize; ++i){if (board[i][0] == player && board[i][1] == player && board[i][2] == player)return true;}return false;}bool diagonalWin(const vector<vector<char>>& board, char player){if (board[0][0] == player && board[1][1] == player && board[2][2] == player)return true;if (board[2][0] == player && board[1][1] == player && board[0][2] == player)return true;return false;}// Function to check if a player has wonbool checkWin(const vector<vector<char>>& board, char player){// Check diagonalsif(rowWin(board,player) or colWin(board,player) or diagonalWin(board,player)){return true;}return false;}
If all the places on the board are filled with symbols, and nobody wins, the game is in a drawn state. The code for the draw function is given below.
// Function to check if the game ended in a drawbool checkDraw(const vector<vector<char>>& board){for (int i = 0; i < BoardSize; ++i){for (int j = 0; j < BoardSize; ++j){if (board[i][j] == ' ')return false;}}return true;}
By merging all the above-explained functions, we have our tic-tac-toe in a flow. Look at the code and play the game with your friend to have fun.
#include <iostream>#include <vector>using namespace std;const int BoardSize = 3;// We need to specify the input in the box as follow, if you want to run the code here.// 0 0// 0 1// 0 2// 1 0// 1 1// 1 2// 2 0// 2 1// 2 2void PrintBoard(const vector<vector<char>>& board){cout<<endl;for (int i = 0; i < BoardSize; ++i){for (int j = 0; j < BoardSize; ++j){cout << board[i][j] << " ";}cout << endl;}cout << endl;}// Checking rows if a player has won.bool rowWin(const vector<vector<char>>& board, char player){// Check rows and columnsfor (int i = 0; i < BoardSize; ++i){if (board[0][i] == player && board[1][i] == player && board[2][i] == player)return true;}return false;}// Checking rows if a player has won.bool colWin(const vector<vector<char>>& board, char player){for (int i = 0; i < BoardSize; ++i){if (board[i][0] == player && board[i][1] == player && board[i][2] == player)return true;}return false;}bool diagonalWin(const vector<vector<char>>& board, char player){if (board[0][0] == player && board[1][1] == player && board[2][2] == player)return true;if (board[2][0] == player && board[1][1] == player && board[0][2] == player)return true;return false;}// Function to check if a player has wonbool checkWin(const vector<vector<char>>& board, char player){// Check diagonalsif(rowWin(board,player) or colWin(board,player) or diagonalWin(board,player)){return true;}return false;}// Function to check if the game ended in a drawbool checkDraw(const vector<vector<char>>& board){for (int i = 0; i < BoardSize; ++i){for (int j = 0; j < BoardSize; ++j){if (board[i][j] == ' ')return false;}}return true;}// Function to make a movebool makeMove(vector<vector<char>>& board, int row, int col, char player){if (row >= 0 && row < BoardSize && col >= 0 && col < BoardSize && board[row][col] == ' '){board[row][col] = player;return true;}return false;}int main(){vector<vector<char>> board(BoardSize, vector<char>(BoardSize, ' '));char CurrentPlayerSymbol = 'X';cout << "Tic-Tac-Toe Game\n";PrintBoard(board);while (true){int row, col;cout << "Player " << CurrentPlayerSymbol << " turn, please enter row and column : ";cin >> row >> col;if (makeMove(board, row, col, CurrentPlayerSymbol)){PrintBoard(board);if (checkWin(board, CurrentPlayerSymbol)){cout << "Player " << CurrentPlayerSymbol << " wins!" << endl;break;}if (checkDraw(board)){cout << "It's a draw!" << endl;break;}CurrentPlayerSymbol = (CurrentPlayerSymbol == 'X') ? 'O' : 'X';}else{cout << "Invalid move. Try again." << endl;}}return 0;}
Enter the input below
This Answer discusses the mechanics and implementation of the tic-tac-toe game. This is a fun exercise that allows beginners to apply their concepts practically. You can also create an AI-based tic-tac-toe game using a minimax algorithm with different programming languages.
Free Resources