home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / MotifApp / Extras / TicTacToe / Engine.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  3.9 KB  |  161 lines

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //////////////////////////////////////////////////////////////////////////////
  3. //         This example code is from the book:
  4. //
  5. //           Object-Oriented Programming with C++ and OSF/Motif
  6. //         by
  7. //           Douglas Young
  8. //           Prentice Hall, 1992
  9. //           ISBN 0-13-630252-1    
  10. //
  11. //         Copyright 1991 by Prentice Hall
  12. //         All Rights Reserved
  13. //
  14. //  Permission to use, copy, modify, and distribute this software for 
  15. //  any purpose except publication and without fee is hereby granted, provided 
  16. //  that the above copyright notice appear in all copies of the software.
  17. ///////////////////////////////////////////////////////////////////////////////
  18. //////////////////////////////////////////////////////////////////////////////
  19.  
  20.  
  21. /////////////////////////////////////////////////////////
  22. // Engine.C: The brains of the tic-tac-toe game
  23. /////////////////////////////////////////////////////////
  24. #include "TicTacToe.h"
  25. #include "Engine.h"
  26. #include "GameBoard.h"
  27. #include "MoveGenerator.h"
  28. #include "Message.h"
  29. #include "Board.h"
  30. #include <stdlib.h>  // Needed for exit()
  31.  
  32. Engine::Engine ( TicTacToe* game )
  33. {
  34.     _game      = game;
  35.     _gameOver  = FALSE;
  36.     _whoseMove = XX; // Start with X as the first move
  37.     _board = new Board(); // Create the Engine subcomponents
  38.     _moveGenerator = new MoveGenerator();
  39. }
  40.  
  41.  
  42. Engine::~Engine()
  43. {
  44.     delete _board;
  45.     delete _moveGenerator;
  46. }
  47.  
  48. void Engine::reset()
  49. {
  50.     _whoseMove = XX;
  51.     _gameOver  = FALSE;
  52.     _board->clear();
  53.     _game->gameBoard()->clear();
  54.     _game->messageArea()->postMessage ( NEWGAMEMSG );
  55. }
  56.  
  57. void Engine::recordMove ( int position )
  58. {
  59.     if ( _gameOver ) // Don't accept moves if the game is over
  60.     {
  61.     _game->messageArea()->postAlert( GAMEISOVERMSG );
  62.     return;
  63.     }
  64.     
  65.     // Record the move. If it is valid, display it on the board
  66.     // Otherwise ask the user to pick again.
  67.     
  68.     if ( _board->recordMove ( position, _whoseMove ) == validMove)
  69.     {
  70.     if(_whoseMove == XX)
  71.         _game->gameBoard()->markX ( position );
  72.     else
  73.         _game->gameBoard()->markO ( position );
  74.     }
  75.     else
  76.     {
  77.     _game->messageArea()->postAlert ( ILLEGALMOVEMSG );
  78.     return;
  79.     }
  80.     
  81.     // See if this move wins the game for the user
  82.     
  83.     checkForWin();
  84.     
  85.     if ( _gameOver )
  86.     return;
  87.     
  88.     // If this is the game's move, change to X's move and ask the 
  89.     // user to choose a square. If it is the user's move, change to
  90.     // our move and pick a move. Call this function
  91.     // recursively to record the game's choice.
  92.     
  93.     if ( _whoseMove == OO )
  94.     {    
  95.     _whoseMove = XX;
  96.     _game->messageArea()->postMessage ( USERSMOVEMSG );
  97.     }
  98.     else
  99.     {
  100.     _whoseMove = OO;
  101.     recordMove ( _moveGenerator->getNextMove ( _board ) );
  102.     }
  103. }
  104.  
  105. void Engine::checkForWin()
  106. {
  107.     int      i, *winningSquares;
  108.     markType winner;
  109.     
  110.     // If no one has won yet, just keep playing
  111.     
  112.     if    ( ( winner = _board->whoHasWon() ) == NOBODYYET )
  113.     {
  114.     return;
  115.     }
  116.     else if ( winner == TIE)
  117.     {
  118.     // If it's a tie, end the game and notify the user
  119.     
  120.     _gameOver = TRUE;    
  121.     
  122.     for ( i = 0 ; i < 9; i++ )
  123.         _game->gameBoard()->deemphasizeSquare ( i );
  124.     
  125.     _game->messageArea()->postAlert( TIEGAMEMSG );
  126.     }
  127.     else // Someone won 
  128.     {
  129.     _gameOver = TRUE;    
  130.     
  131.     // Get the mask for the wining pattern 
  132.     
  133.     winningSquares = _board->winningSquares();
  134.     
  135.     // Deactivate each square to prevent input. Highlight
  136.     // the winning squares, and fade others into the background.
  137.     
  138.     for ( i = 0 ; i < 9; i++ )
  139.     {
  140.         _game->gameBoard()->deactivateSquare ( i );
  141.         
  142.         if ( winningSquares[i] )        
  143.         _game->gameBoard()->highlightSquare ( i );
  144.         else
  145.         _game->gameBoard()->deemphasizeSquare ( i );
  146.     }
  147.     
  148.     // Finally, alert the user that someone has won
  149.     
  150.     if ( winner  == XX )
  151.         _game->messageArea()->postAlert ( XWINSMSG );
  152.     else
  153.         _game->messageArea()->postAlert ( OWINSMSG );    
  154.     }
  155. }
  156.  
  157. void Engine::quit()
  158. {
  159.     exit ( 0 );
  160. }
  161.