Tic Tac Toe

For this problem you will use the C++ code you wrote in Lab 6 and implement a program that plays the game of tic-tac-toe against a human opponent. In addition to re-using the function that determines the state of the game from the grid array, you will write and integrate several new functions to manage an interactive version of tic-tac-toe.

Create a user-defined class named TTTGame that will maintain the state of the tic-tac-toe game. TTTGame should include a two-dimensional (3x3) array to represent the state of the game and any other data elements required to implement the various methods. Be sure to include appropriate constructors.
Convert the function you wrote in lab 6 to a member function of the TTTGame class named gameState.
Include a member function named displayGame that displays the current game state array on the console as follows:

- X O

- - -

O - -

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

Include a boolean member function named occupied that takes row and column values as arguments and returns a boolean indicating whether or not the indicated grid location has already been played by either player.
Include a void member function named update that takes row and column values plus a single character (‘X’, ‘O’) and updates the current game state.
Include a void member function named nextPlay that determines the row and column of the next move that the computer determines is the ‘best’ play. nextPlay should include 2 reference parameters to provide the values to the caller.


You may use any strategy you wish to determine the next moe. A few possibilities:

Select the first unoccupied grid cell you find
Use the rand() function to select a random cell
Locate rows and columns that have not been played by the opponent
...etc

Write a program to play the game using the following algorithm together with the member functions you have created:



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 column/row has not already been played. The function should continue to solicit input until a valid row/column value has been entered.

Input the opponent’s move from the console and verify that the specified row/column has not already been played. Your program should continue to solicit input until a valid row/column value has been entered. [Hint: this may be easier if you create/debug a stand-alone function first]
Update and display the (updated) board state.
Determine if the result of the opponent’s move is ‘win’, ‘draw’, or ‘continue’.
If opponent wins, then announce “Good game, you win”, and end the program.
If ‘draw’, then display “The game ends in a draw” and end the program.
If ‘continue’, then determine the computer’s next move.
Display “I will play row x, column y” using the row/column values determined in the previous step.
Update and display the board state with the computer’s move.
Determine the result of the computer’s move.
If ‘win’, then announce “I win, nice try” and end the program.
If ‘draw’, then announce “The game ends in a draw” and end the program.
If ‘continue’, then repeat steps a-1 until the program ends.

I was able to come up with this, but it doesn't meet the requirements. I need to simulate a computer that the user could play against using the rand() function. Is there a quick fix for this?


#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--;
}

i=checkwin();
player++;
}while(i==-1);
board();
if(i==1)
cout<<"==>\aPlayer "<<--player<<" win ";
else
cout<<"==>\aGame draw";

return 0;
}


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()
{
cout << "\n\n\tTic Tac Toe\n\n";
cout << "Player 1 (X) - Player 2 (O)" << endl << endl;
cout << endl;
cout << " " << square[1] << " | " << square[2] << " | " << square[3] << endl;
cout << "_____|_____|" << endl;
cout << " " << square[4] << " | " << square[5] << " | " << square[6] << endl;
cout << "_____|_____|" << endl;
cout << " " << square[7] << " | " << square[8] << " | " << square[9] << endl;
}




Topic archived. No new replies allowed.