Assignment #12:  Due 11/3/2009 at the beginning of class.

 

Implement the following specifications for a tic tac toe game class:

 

class tictactoeGame

{

    public:

        tictactoeGame();  

        // postcondition:  boardConfig[i][j] is character ' ' for each i,j from 0 to 2

          

        bool placeX(int x, int y);

        // precondition:  boardConfig[x][y] == ' '

        //                if this is not the case, return false

        // postcondition:  boardConfig[x][y] = 'X', return true

       

        bool placeO(int x, int y);

        // precondition:  boardConfig[x][y] == ' '

        //                if this is not the case, return false

        // postcondition:  boardConfig[x][y] = 'O', return true

  

        void clear();

        // postcondition:  boardConfig[x][y] = ' ' for each x and y from 0 to 2

 

        bool xWins();

        // Return true if there are 3 'X' marks placed in a single

        // column, row, or diaganol.  Return false otherwise.

       

        bool oWins();

        // Return true if there are 3 'O' marks placed in a single

        // column, row, or diagnol.  Return false otherwise.

     

        bool gameOver();

        // Return true if there are either 3 'X' marks or 3 'O' marks

        // placed in a single column, row, or diaganol, or if the board is full.

  // Return false otherwise.

  

        void display();

        // cout a nice looking picture of the board configuration

  

    private:

        char boardConfig[3][3];   // two dimensional array stores current board configuration

};

 

 

For example, consider the client program below:

 

int main()

{

    tictactoeGame mygame;

   

    mygame.placeX(1,1);

    mygame.placeO(2,1);

    mygame.placeX(0,2);

    mygame.placeO(2,2);

   

    mygame.display();

}

 

This should display something like:

   |   | X

-----------

   | X | 

-----------

   | O | O

 

Once you have implemented the tictactoeGame class, write a client program that uses a tictactoeGame to implement a game in which two players (you and a friend) take turns placing X’s and O’s onto the board.  After each turn, the current board configuration should be displayed, and once a player connects three of a kind, the game should end declaring that player the winner.

 

For extra credit, modify the tictactoeBoard so that a single person can play a game against the computer (have the computer make intelligent choices, or make random valid moves).