TicTacToe_Example.cpp

#include "tictactoe.h"
#include <iostream>
#include <stdlib.h>
#include <time.h>
/*
  \example    TicTacToe_Example.cpp
 \brief   Example code source for class TicTacToe.
          In this example, the first player is a human, and the second one is a random player.
 \author  Philippe Lucidarme
 \version 1.0
 \date    18 mars 2011
 */
/*
 \file    TicTacToe_example.cpp
 \brief   Example code source for class TicTacToe.
          In this example, the first player is a human, and the second one is a random player.
 \author  Philippe Lucidarme
 \version 1.0
 \date    18 mars 2011
 */


// 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 << "\nCe sont les croix qui gagnent\n"; break;
    case CIRCLE : cout << "\nCe sont les ronds qui gagnent\n"; break;
    case DRAW   : cout << "\nMatch nul !\n";
    }
}



// Play randomly a move
void RandomPlayer(TicTacToe *Game)
{
    int Cell=-1;
    do
            Cell=random()%9+1;
    while (Game->Play(Cell)==0);
}



// Human player, ask for a human to enter its move
void HumanPlayer(TicTacToe *Game)
{
    int Cell=10;   
    do
    {
        cout << "Dans quelle case jouez-vous : ";
        std::cin >> Cell;
    }
    while (Game->Play(Cell)==0);
}
 All Classes Files Functions Defines
Generated on Sat Mar 19 17:36:09 2011 for Tic-Tac-Toe by  doxygen 1.6.3