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

Write a program that allows two players to play a game of tic-tac-toe. Use a two

ID: 3641521 • Letter: W

Question

Write a program that allows two players to play a game of tic-tac-toe. Use a two dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that:
• Displays the contents of the board array (column headings, row labels)
• Allows player #1 to select a location on the board for an X. The program should ask the user to enter the row and column number.
• Allows player #2 to select a location on the board for an O. The program should ask the user to enter the row and column number.
• Determines whether a player has won, or a tie has occurred. If a player has won, the program should declare that player the winner and end. If a tie has occurred, the program should say so and end.
Player #1 wins if there are three Xs in a row on the game board. The Xs can appear in a row, in a column, or diagonally across the board. A tie occurs when all of the locations on the board are full, but there is not winner.

Explanation / Answer

#include <iostream>

using namespace std;

int main() {

       char array[3][3];

       for(int i = 0; i < 3; ++i) {

              for(int j = 0; j < 3; ++j) {

                     array[i][j] = '*';

              }

       }

       bool gameover = false;

       int playTurn = 1;

       int rowNum = 0;

       int colNum = 0;

       int playCount = 1;

       while(!gameover) {

              cout << "    0 1 2" << endl;

              for(int i = 0; i < 3; ++i) {

                     cout << i << ": ";

                     for(int j = 0; j < 3; ++j) {

                           cout << "[" << array[i][j] << "]";

                     }

                     cout << endl;

              }

              if(playTurn == 1) {

                     bool valid = false;

                     while(!valid) {

                           bool validRow = false;

                           while(!validRow) {

                                  cout << "Player 1: Enter a row number: ";

                                  cin >> rowNum;

                                  if(rowNum >= 0 & rowNum < 3) {

                                         validRow = true;

                                  }

                                  else {

                                         cout << "Not a valid row, try again." << endl;

                                  }

                           }

                           bool validCol = false;

                           while(!validCol) {

                                  cout << "Player 1: Enter a column number: ";

                                  cin >> colNum;

                                  if(colNum >= 0 & colNum < 3) {

                                         validCol = true;

                                  }

                                  else {

                                         cout << "Not a valid column, try again." << endl;

                                  }

                           }

                           if(array[rowNum][colNum] == '*') {

                                  array[rowNum][colNum] = 'X';

                                  valid = true;

                           }

                           else {

                                  cout << "That space is already occupied, try again." << endl;

                           }

                     }

                     playTurn = 2;

              }

              else {

                     bool valid = false;

                     while(!valid) {

                           bool validRow = false;

                           while(!validRow) {

                                  cout << "Player 2: Enter a row number: ";

                                  cin >> rowNum;

                                  if(rowNum >= 0 & rowNum < 3) {

                                         validRow = true;

                                  }

                                  else {

                                         cout << "Not a valid row, try again." << endl;

                                  }

                           }

                           bool validCol = false;

                           while(!validCol) {

                                  cout << "Player 2: Enter a column number: ";

                                  cin >> colNum;

                                  if(colNum >= 0 & colNum < 3) {

                                         validCol = true;

                                  }

                                  else {

                                         cout << "Not a valid column, try again." << endl;

                                  }

                           }

                           if(array[rowNum][colNum] == '*') {

                                  array[rowNum][colNum] = 'O';

                                  valid = true;

                           }

                           else {

                                  cout << "That space is already occupied, try again." << endl;

                           }

                     }

                     playTurn = 1;

              }

              playCount++;

              if(playCount > 9) {

                     gameover = true;

                     cout << "Game ends in a tie" << endl;

              }

              for(int a = 0; a < 3; ++a) {

                     if(array[a][0] == 'X' && array[a][1] == 'X' && array[a][2] == 'X') {

                           cout << "Player 1 wins" << endl;

                           gameover = true;

                     }

                     if(array[a][0] == 'O' && array[a][1] == 'O' && array[a][2] == 'O') {

                           cout << "Player 2 wins" << endl;

                           gameover = true;

                     }

              }

              for(int b = 0; b < 3; ++b) {

                     if(array[0][b] == 'X' && array[1][b] == 'X' && array[2][b] == 'X') {

                           cout << "Player 1 wins" << endl;

                           gameover = true;

                     }

                     if(array[0][b] == 'O' && array[1][b] == 'O' && array[2][b] == 'O') {

                           cout << "Player 2 wins" << endl;

                           gameover = true;

                     }

              }

              if(array[0][0] == 'X' && array[1][1] == 'X' && array[2][2] == 'X') {

                     cout << "Player 1 wins" << endl;

                     gameover = true;

              }

              if(array[0][0] == 'O' && array[1][1] == 'O' && array[2][2] == 'O') {

                     cout << "Player 2 wins" << endl;

                     gameover = true;

              }

              if(array[0][2] == 'X' && array[1][1] == 'X' && array[2][0] == 'X') {

                     cout << "Player 1 wins" << endl;

                     gameover = true;

              }

              if(array[0][2] == 'O' && array[1][1] == 'O' && array[2][0] == 'O') {

                     cout << "Player 2 wins" << endl;

                     gameover = true;

              }

       }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote