Tic-Tac-Toe 2.0

TicTacToe_Example.cpp

/*
 \file      TicTacToe_example.cpp

 \brief     Example code source for class TicTacToe.
            In this example, the first player is a human (CROSSES),
            and the second one is a random player (CIRCLES).

 \author    Philippe Lucidarme
 \version   1.0
 \date      18 mars 2011
 */


#include "tictactoe.h"
#include <iostream>
#include <stdlib.h>
#include <time.h>


// Ask for the user to enter its move using the keyboard
void    HumanPlayer(TicTacToe *Game);

// Random selection of the move
void    RandomPlayer(TicTacToe *Game);



int main(int argc, char *argv[])
{
    TicTacToe *Game=new TicTacToe;                                      // Create a pointer on a TicTacToe object
    srand ( time(NULL) );                                               // Initialyze pseudo random generaor using current time

    // ::: Game core :::

    Game->DrawGame();                                                   // Show the game
    while (Game->GameOver()==0) {                                       // Play until the game is over
        if (Game->CurrentPlayer()==CROSS) {                             // Current player is player one
            cout << "\n ::: Player 1 (Crosses) :::\n";
            HumanPlayer(Game);                                          // Player 1 is the human player
            Game->DrawGame();                                           // Draw the game after the move
        }
        else {
            cout << "\n::: Player 2 (Circles) :::\n";
            RandomPlayer(Game);                                         // Player 2 is a random player
            Game->DrawGame();                                           // Draw the game after the move
        }
    }

    // ::: End of the game :::

    std::cout << "\n ::: Game Over ::: \n\n";
    Game->DrawGame();
    switch (Game->GameOver()) {                                         // Print the result of the game
    case CROSS  : cout << "\nPlayer 1 wins (CROSSES)\n"; break;         // Player 1 is winning
    case CIRCLE : cout << "\nPlayer 2 wins (CIRCLES)\n"; break;         // Player 2 is winning
    case DRAW   : cout << "\nDraw !\n";                                 // Draw game
    }
}

// ::: Play randomly a move :::

void RandomPlayer(TicTacToe *Game)
{
    int Cell=random()%9+1;                                              // Randomly select a move
    while (Game->Play(Cell)==0)                                         // while the move is not possible...
        Cell=random()%9+1;                                              // ... select a new one
}

// ::: Human player :::

void HumanPlayer(TicTacToe *Game)
{
    int Cell;
    do
    {
        cout << "Dans quelle case jouez-vous : ";                       // Print a message for the user
        cin >> Cell;                                                    // Read the move to the keyboard
    }
    while (Game->Play(Cell)==0);                                        // Until the move is possible
}
 All Classes Files Functions Defines