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();