Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

\"Tic-tac-toe, also called noughts and crosses (in the British Commonwealth coun

ID: 3676846 • Letter: #

Question

"Tic-tac-toe, also called noughts and crosses (in the British Commonwealth countries), X's and O's (in Ireland) and X and 0 (in India) is a pencil-and-paper game for two players, X and O, who take turns marking the spaces in a 3×3 grid. The X player usually goes first. The player who succeeds in placing three respective marks in a horizontal, vertical, or diagonal row wins the game." (Wikipedia)

Create a basic tic-tac-toe game for the console. The game should work as follows:

1. Output a board in the following format:

No other board format will be accepted. You must number the rows and columns, not each box.

2. Ask the user for a row and column number.

3. Put the appropriate symbol for that player (X or O) in the specified spot.

4. Repeat for the next player until someone has won the game.

5. If someone has won, announce which player won and exit.

Explanation / Answer

main.cpp

#include <iostream>
#include "TicTacToe.h"

using namespace std;

int main(/*int argc, char* argv[]*/)
{
   TicTacToe Game;
   int row, col;
   char answer;

   while (1) //Game Loop
   {
       while (Game.isWon() == TicTacToe::IN_PROGRESS)
       {
           system("cls");
           Game.print();
           cout << "Player " << Game.getPlayer() << ": Enter your move: row,column" << endl;
           cin >> row;
           cin.ignore(1);
           cin >> col;
           while (!Game.move(row - 1, col - 1))
           {
               cout << "Invalid Move" << endl;
               cout << "Player " << Game.getPlayer() << ": Enter your move: row,column" << endl;
               cin >> row;
               cin.ignore(1);
               cin >> col;
           }
       }

       system("cls");
       Game.print();

       if (Game.isWon() == TicTacToe::DRAW)
           cout << "The Game Was Drawn";
       else if (Game.isWon() == TicTacToe::PLAYER1)
           cout << "Player 1 Won!";
       else if (Game.isWon() == TicTacToe::PLAYER2)
           cout << "Player 2 Won!";
       cout << endl << endl;

       cout << "Would you like to play again ? (Y/N) > ";
       cin >> answer;
      
       if (answer == 'N' || answer == 'n')
           break;
       else if (answer == 'Y' || answer == 'y')
           Game.restart();
   }

   return 0;
}

TicTacToe.h

#ifndef TICTACTOE_H
#define TICTACTOE_H

//#include <string>

class TicTacToe
{
private:
   char board[3][3];
   int currentPlayer;

public:
   TicTacToe();
   ~TicTacToe();

   int getPlayer() { return currentPlayer; }
   enum game_state{IN_PROGRESS, PLAYER1, PLAYER2, DRAW };
   void restart();
   bool move(int, int);
   void print();
   int isWon();
};
#endif


TicTacToe.cpp
#include <iostream>
#include "TicTacToe.h"

using namespace std;

TicTacToe::TicTacToe()
{
   //Initialize the board, set all squares blank, set player to player 1
   for (int i = 0; i < 3; i++)
   {
       for (int j = 0; j < 3; j++)
       {
           board[i][j] = ' ';
       }
   }

   currentPlayer = PLAYER1;;
}

TicTacToe::~TicTacToe()
{

}

void TicTacToe::restart()
{
   //clear the board: set all squares blank, set player to player 1
   for (int i = 0; i < 3; i++)
   {
       for (int j = 0; j < 3; j++)
       {
           board[i][j] = ' ';
       }
   }

   currentPlayer = PLAYER1;;
}

bool TicTacToe::move(int row, int col)
{
   if (row > 2 || row < 0 || col > 2 || col < 0) // move must be within the 3x3 board
   {
       return false; // Invalid move
   }
   else if (board[row][col] != ' ' ) // block must be empty
   {
       return false; // Invalid move
   }
   else
   {
       if (currentPlayer == PLAYER1)
       {
           board[row][col] = 'X';
           currentPlayer = PLAYER2;
       }
       else
       {
           board[row][col] = 'O';
           currentPlayer = PLAYER1;
       }
       return true;
   }
}

void TicTacToe::print()
{
   cout << " | 1 | 2 | 3 |" << endl;
   cout << "1| " << board[0][0] << " | " << board[0][1] << " | " << board[0][2] << " | " << endl;
   cout << "2| " << board[1][0] << " | " << board[1][1] << " | " << board[1][2] << " | " << endl;
   cout << "3| " << board[2][0] << " | " << board[2][1] << " | " << board[2][2] << " | " << endl << endl;
}

int TicTacToe::isWon()
{
   int player1 = 0, player2 = 0, empty = 0;

   //check rows
   for (int i = 0; i < 3; i++)
   {
       player1 = player2 = 0;

       for (int j = 0; j < 3; j++)
       {
           if (board[i][j] == 'X')
               ++player1;
           else if (board[i][j] == 'O')
               ++player2;
           else
               ++empty;
       }

       if (player1 == 3)
           return PLAYER1;
       else if (player2 == 3)
           return PLAYER2;
   }

   //check columns
   for (int i = 0; i < 3; i++)
   {
       player1 = player2 = 0;

       for (int j = 0; j < 3; j++)
       {
           if (board[j][i] == 'X')
               ++player1;
           else if (board[j][i] == 'O')
               ++player2;
       }

       if (player1 == 3)
           return PLAYER1;
       else if (player2 == 3)
           return PLAYER2;
   }

   //check diagonal 1
   player1 = player2 = 0;
   for (int i = 0; i < 3; i++)
   {

       if (board[i][i] == 'X')
           ++player1;
       else if (board[i][i] == 'O')
           ++player2;
   }

   if (player1 == 3)
       return PLAYER1;
   else if (player2 == 3)
       return PLAYER2;

   //check diagonal 2
   player1 = player2 = 0;
   for (int i = 0; i < 3; i++)
   {

       if (board[2-i][i] == 'X')
           ++player1;
       else if (board[2-i][i] == 'O')
           ++player2;
   }

   if (player1 == 3)
       return PLAYER1;
   else if (player2 == 3)
       return PLAYER2;

   if (empty == 0)
       return DRAW;

   return IN_PROGRESS;
}


output
| 1 | 2 | 3 |                                                                                                                                              
1|   |   |   |                                                                                                                                              
2|   |   |   |                                                                                                                                              
3|   |   |   |                                                                                                                                              
                                                                                                                                                            
Player 1: Enter your move: row,column                                                                                                                       
1 1                                                                                                                                                         
sh: cls: command not found                                                                                                                                  
| 1 | 2 | 3 |                                                                                                                                              
1| X |   |   |                                                                                                                                              
2|   |   |   |                                                                                                                                              
3|   |   |   |                                                                                                                                              
                                                                                                                                                            
Player 2: Enter your move: row,column                                                                                                                       
2 3                                                                                                                                                         

| 1 | 2 | 3 |                                                                                                                                              
1| X | O |   |                                                                                                                                              
2|   |   | O |                                                                                                                                              
3| X | O | X |                                                                                                                                              
                                                                                                                                                            
Player 1: Enter your move: row,column                                                                                                                       
1 3                                                                                                                                                         
| 1 | 2 | 3 |                                                                                                                                              
1| X | O | X |                                                                                                                                              
2|   |   | O |                                                                                                                                              
3| X | O | X |                                                                                                                                              
                                                                                                                                                            
Player 2: Enter your move: row,column                                                                                                                       
2 1                                                                                                                                                         

1| X | O | X |                                                                                                                                              
2| O |   | O |                                                                                                                                              
3| X | O | X |                                                                                                                                              
                                                                                                                                                            
Player 1: Enter your move: row,column                                                                                                                       
2 2                                                                                                                                                         
sh: cls: command not found                                                                                                                                  
| 1 | 2 | 3 |                                                                                                                                              
1| X | O | X |                                                                                                                                              
2| O | X | O |                                                                                                                                              
3| X | O | X |                                                                                                                                              
                                                                                                                                                            
Player 1 Won!                                                                                                                                               
                                                                                                                                                            
Would you like to play again ? (Y/N) > n