Tic Tac Toe

Tic Tac Toe

Program 4:

Tic Tac Toe

 

Write a program that will allow two users to play tic-tac-toe. The program should ask for moves alternatively from player X and player O. The display should be board-like:

  1  2  3

  4  5  6

  7  8  9

The players enter their moves by entering a number corresponding to the place to be marked. After each move, the program displays the changed board. After several moves the board may look like this: 

    X    X    O

    4    5    6

    O    8    9

You will write a class called Game. The class will have as a data member 

1) an array of size 9, type char, called board

It should also have the following member functions:

A constructor that takes no parameters but initializes the game board to 1 through 9

A function that displays the game board. Remember that after ever 3 elements, it should print a new line so that the board displays as shown above. The board needs to be displayed before every move.

A function called play that will take in at least one parameter of either X or O, depending on which player’s turn it is. Additionally, you could add the parameter of the move that player entered (or you could ask for the input within this function).  The main purpose of this function is to modify the game board. It must check that only 1 through 9 has been entered, that the space the user has chosen is not yet taken, and then change the game board accordingly. It is up to you to give the player another turn or to pass to the next player if they have chosen an incorrect space.

A function called checkWin, which checks to see if there are any winners. In tic-tac-toe, you win with all of the same marker across, down, or diagonal. You may want this to be a Boolean function. 

If you would like to add more data members or functions, you may do so, but the 5 above are required.

 
Powered by