home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / scase.zip / SCASE.Z / TTTENG.CC < prev    next >
C/C++ Source or Header  |  1994-10-17  |  5KB  |  157 lines

  1. /******************************************************************************
  2. *                         ** Company Confidential **
  3. *                  ** Copyright 1994, All rights Reserved **
  4. * %Z%%M% version %I% date:%G%
  5. ******************************************************************************/
  6.  
  7. // -MQBeg- [Module_Includes] [ttteng_cc] <00000017.00000000> Retained!
  8. #include "ttteng.h"
  9. // -MQEnd- [Module_Includes] [ttteng_cc] <00000017.00000000> 
  10.  
  11. // -MQBeg- [Module_Declarations_1] [ttteng_cc] <00000017.00000000> Retained!
  12. // -MQEnd- [Module_Declarations_1] [ttteng_cc] <00000017.00000000> 
  13.  
  14. // -MQBeg- [Class_Body] [TTT_Engine] <00000000.00000001> 
  15. /******************************************************************************
  16.                                   TTT_Engine
  17. ******************************************************************************/
  18.  
  19. const int TTT_Engine::freeMasks[] = {0x006,0x005,0x003,0x030,0x028,0x018,0x180,0x140,0x0c0,0x048,0x041,0x009,0x090,0x082,0x012,0x120,0x104,0x024,0x110,0x101,0x011,0x050,0x044,0x014};
  20. const int TTT_Engine::winningMasks[] = {0x1c0,0x038,0x007,0x124,0x092,0x049,0x111,0x054};
  21.  
  22. TTT_Engine::TTT_Engine()
  23. // -MQBeg- [Op_Init] [TTT_Engine::TTT_Engine] <0000002D.00000037> Retained!
  24. // -MQEnd- [Op_Init] [TTT_Engine::TTT_Engine] <0000002D.00000037> 
  25. {
  26. // -MQBeg- [Op_Body] [TTT_Engine::TTT_Engine] <0000002D.00000037> Retained!
  27.   this->Reset();
  28. // -MQEnd- [Op_Body] [TTT_Engine::TTT_Engine] <0000002D.00000037> 
  29. }
  30.  
  31. void
  32. TTT_Engine::Reset()
  33. {
  34. // -MQBeg- [Op_Body] [TTT_Engine::Reset] <0000002D.00000038> Retained!
  35.   MoveResult = LEGAL;
  36.   this->ClearBoard();
  37. // -MQEnd- [Op_Body] [TTT_Engine::Reset] <0000002D.00000038> 
  38. }
  39.  
  40. int
  41. TTT_Engine::MoveRequest(int CellNumber)
  42. {
  43. // -MQBeg- [Op_Body] [TTT_Engine::MoveRequest] <0000002D.00000039> Retained!
  44.   if( (1 << CellNumber) & (UserBoard | CompBoard) )
  45.     MoveResult = ILLEGAL;
  46.   else
  47.   { 
  48.     UserBoard |= 1 << CellNumber;
  49.     MoveResult = LEGAL;
  50.     this->WinLoseDraw();      // Modify MoveResult if game is over.
  51.     if (MoveResult == LEGAL)  // Game is not over.
  52.     {
  53.       this->ComputerTurn();
  54.       this->WinLoseDraw();
  55.     }  
  56.   }
  57.   return MoveResult;
  58. // -MQEnd- [Op_Body] [TTT_Engine::MoveRequest] <0000002D.00000039> 
  59. }
  60.  
  61. void
  62. TTT_Engine::GetBoards(int& User, int& Comp)
  63. {
  64. // -MQBeg- [Op_Body] [TTT_Engine::GetBoards] <0000002D.0000003A> Retained!
  65.   User = UserBoard;
  66.   Comp = CompBoard;
  67. // -MQEnd- [Op_Body] [TTT_Engine::GetBoards] <0000002D.0000003A> 
  68. }
  69.  
  70. void
  71. TTT_Engine::ComputerTurn(void)
  72. {
  73. // -MQBeg- [Op_Body] [TTT_Engine::ComputerTurn] <0000002D.0000003B> Retained!
  74.   int MadeMove = 0;
  75.   for( int i=0; i<24; i++)
  76.   {
  77.     if ((UserBoard & freeMasks[i]) == freeMasks[i])
  78.     {
  79.       int targetMask;
  80.       FreeSquare(freeMasks[i], i, targetMask);
  81.       if (CompBoard & targetMask)
  82.          continue;
  83.       CompBoard |= targetMask;
  84.       MadeMove = 1;
  85.       break;
  86.     }
  87.   }
  88.   if(!MadeMove)  // Find first available location
  89.   {
  90.      if ( !((CompBoard|UserBoard) & 0x010) ) // See if middle is free
  91.        CompBoard |= 0x010;
  92.      else
  93.      {
  94.        int mask = UserBoard|CompBoard;
  95.        for (int i=0; mask & 1; mask >>= 1) i++;
  96.        if (i<9) CompBoard |= 1<<i;
  97.      }
  98.   }
  99. // -MQEnd- [Op_Body] [TTT_Engine::ComputerTurn] <0000002D.0000003B> 
  100. }
  101.  
  102. void
  103. TTT_Engine::ClearBoard(void)
  104. {
  105. // -MQBeg- [Op_Body] [TTT_Engine::ClearBoard] <0000002D.0000003C> Retained!
  106.   UserBoard = 0;
  107.   CompBoard = 0;
  108. // -MQEnd- [Op_Body] [TTT_Engine::ClearBoard] <0000002D.0000003C> 
  109. }
  110.  
  111. void
  112. TTT_Engine::FreeSquare(int Mask, int i , int& TargetMask)
  113. {
  114. // -MQBeg- [Op_Body] [TTT_Engine::FreeSquare] <0000002D.0000003D> Retained!
  115.   int test1, mode = i/9;
  116.   if (mode == 0)
  117.     Mask ^= 0x007 << (i/3)*3;
  118.   else if (mode == 1)
  119.     Mask ^= 0x049 << ((i%9)/3);
  120.   else if (((i%9)/3) == 0)
  121.     Mask ^= 0x111;
  122.   else
  123.     Mask ^= 0x054;
  124.   int j;
  125.   for(j=0, test1=1; test1; test1 <<= 1, j++)
  126.     if (test1 & Mask)
  127.       break;
  128.   TargetMask = 1 << j;
  129. // -MQEnd- [Op_Body] [TTT_Engine::FreeSquare] <0000002D.0000003D> 
  130. }
  131.  
  132. void
  133. TTT_Engine::WinLoseDraw(void)
  134. {
  135. // -MQBeg- [Op_Body] [TTT_Engine::WinLoseDraw] <0000002D.0000003E> Retained!
  136.   for(int i=0; i<8; i++)
  137.   {
  138.     if ((UserBoard & winningMasks[i]) == winningMasks[i])
  139.     {
  140.       MoveResult = YOUWIN;
  141.       return;
  142.     }
  143.     else if ((CompBoard & winningMasks[i]) == winningMasks[i])
  144.     {
  145.       MoveResult = YOULOSE;
  146.       return; 
  147.     }
  148.   }
  149.   if ((UserBoard|CompBoard) == 0x1FF) MoveResult = DRAW;
  150. // -MQEnd- [Op_Body] [TTT_Engine::WinLoseDraw] <0000002D.0000003E> 
  151. }
  152.  
  153. // -MQEnd- [Class_Body] [TTT_Engine] <00000000.00000001> 
  154.  
  155. // -MQBeg- [Module_Declarations_2] [ttteng_cc] <00000017.00000000> Retained!
  156. // -MQEnd- [Module_Declarations_2] [ttteng_cc] <00000017.00000000> 
  157.