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

Program in C++ Please! Part One: The game of Tic-Tac-Toe (or Naughts and Crosses

ID: 3833325 • Letter: P

Question

Program in C++ Please!

Part One:

The game of Tic-Tac-Toe (or Naughts and Crosses) is an ancient game in which 2 players alternate turns placing either X's or O's on a 3x3 square grid. One player places X's on the grid and the opposing player places O's. The first player to place 3 consecutive X's (or O's) on any row, column or diagonal of the grid wins the game. If all 9 grid cells are occupied without either player winning, the game is declared a draw.

Write a C++ function that takes a 3x3 array of characters representing a tic-tac-toe game in progress and determines the current game state: player X has won, player O has won, the game is a draw, or none of those.

Your function should return a single character as follows: 'X' if player X has won the game, 'O' if player O has won the game, 'D' if the game is a draw (all cells occupied but neither player has won) and '*' if none of these conditions exist. (Hint: use a for loop that compares each element of the diagonal with the elements of its intersecting row and column, then check the diagonals)

Write a main() driver to verify that your function is correct. It should (1) declare a 3x3 character array, (2) use a loop to read in values for each of the 3 rows of the grid (use '-' to indicate a blank cell), (3) print out the contents of the grid as 3 rows of 3 values, (4) call your function with the array as an argument, and (5) output an appropriate message declaring the state of the game (as returned by your function).

Test your program using the 4 following cases (at a minimum)

Part 2:

Using the function from part 1, write and integrate several new functions to manage an interactive program that will play Tic-Tac-Toe against a human opponent. You need to do the following:

Make the output more "readable" by inserting a blank line after the last row of the game display.

b). Write a function named opponentPlay that takes the current game state array as input and returns the row and column of the next move that the opponent wishes to play. Your function should prompt the user to enter the row and column and verify that it is a legitimate move (1 <= row <= 3, 1 <= column <= 3). In addition, you should verify that the specified row/column has not already been played. The function should continue to solicit input until a valid row/column value has been entered.

c). Write a function named nextPlay that takes the current game state array as input and returns the row and column of the next move that the computer determines is the 'best' play. You may use any strategy you wish to determine the next move.

d). Write a main program to play the game using the following algorithm together with the four functions you've created: 1. Input the opponent's move, update the board state array and display the updated board state. 2. Determine if the result of the opponent's move is 'win', 'draw', or 'continue'. 3. If opponent wins, then announce "Good game, you win", and end the program. 4. If 'draw', then display "The game ends in a draw" and end the program. 5. If 'continue', then determine the next computer move. 6. Display "I will play row x, column y" using the row/column values determined in the previous step. 7. Update the board state array with the computer's move and display it. 8. Determine the result of the computer's move. 9. If 'win', then announce "I win, nice try" and end the program 10. If 'draw', then announce "The game ends in a draw" and end the program 11. If 'continue', then repeat steps 1-10 until the program ends.

X -0 X O X -0 X O X -0 X 0 X O X -0 0 0 XX 0 X 0 X

Explanation / Answer

Code:-

#include <iostream>
using namespace std;

char square[10] = {'o','1','2','3','4','5','6','7','8','9'};

int checkwin();
void board();

int main()
{
   int player = 1,i,choice;

   char mark;
   do
   {
       board();
       player=(player%2)?1:2;

       cout << "Player " << player << ", enter a number: ";
       cin >> choice;

       mark=(player == 1) ? 'X' : 'O';

       if (choice == 1 && square[1] == '1')

           square[1] = mark;
       else if (choice == 2 && square[2] == '2')

           square[2] = mark;
       else if (choice == 3 && square[3] == '3')

           square[3] = mark;
       else if (choice == 4 && square[4] == '4')

           square[4] = mark;
       else if (choice == 5 && square[5] == '5')

           square[5] = mark;
       else if (choice == 6 && square[6] == '6')

           square[6] = mark;
       else if (choice == 7 && square[7] == '7')

           square[7] = mark;
       else if (choice == 8 && square[8] == '8')

           square[8] = mark;
       else if (choice == 9 && square[9] == '9')

           square[9] = mark;
       else
       {
           cout<<"Invalid move ";

           player--;
           cin.ignore();
           cin.get();
       }
       i=checkwin();

       player++;
   }while(i==-1);
   board();
   if(i==1)

       cout<<"==>Player "<<--player<<" win ";
   else
       cout<<"==>Game draw";

   cin.ignore();
   cin.get();
   return 0;
}

/*********************************************

   FUNCTION TO RETURN GAME STATUS
   1 FOR GAME IS OVER WITH RESULT
   -1 FOR GAME IS IN PROGRESS
   O GAME IS OVER AND NO RESULT
**********************************************/

int checkwin()
{
   if (square[1] == square[2] && square[2] == square[3])

       return 1;
   else if (square[4] == square[5] && square[5] == square[6])

       return 1;
   else if (square[7] == square[8] && square[8] == square[9])

       return 1;
   else if (square[1] == square[4] && square[4] == square[7])

       return 1;
   else if (square[2] == square[5] && square[5] == square[8])

       return 1;
   else if (square[3] == square[6] && square[6] == square[9])

       return 1;
   else if (square[1] == square[5] && square[5] == square[9])

       return 1;
   else if (square[3] == square[5] && square[5] == square[7])

       return 1;
   else if (square[1] != '1' && square[2] != '2' && square[3] != '3'
&& square[4] != '4' && square[5] != '5' && square[6] != '6'
&& square[7] != '7' && square[8] != '8' && square[9] != '9')

       return 0;
   else
       return -1;
}


void board()
{
   system("cls");
   cout << " Tic Tac Toe ";

   cout << "Player 1 (X) - Player 2 (O)" << endl << endl;
   cout << endl;

   cout << " | | " << endl;
   cout << " " << square[1] << " | " << square[2] << " | " << square[3] << endl;

   cout << "_____|_____|_____" << endl;
   cout << " | | " << endl;

   cout << " " << square[4] << " | " << square[5] << " | " << square[6] << endl;

   cout << "_____|_____|_____" << endl;
   cout << " | | " << endl;

   cout << " " << square[7] << " | " << square[8] << " | " << square[9] << endl;

   cout << " | | " << endl << endl;
}

Dr Jack
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote