Began implementation of networking
[progcomp2012.git] / judge / manager / game.cpp
index 3df968c..92dc71a 100644 (file)
@@ -7,7 +7,7 @@ using namespace std;
 Game* Game::theGame = NULL;
 bool Game::gameCreated = false;
 
-Game::Game(const char * redPath, const char * bluePath, const bool enableGraphics, double newStallTime, const bool allowIllegal, FILE * newLog, const  Piece::Colour & newReveal, int newMaxTurns, bool newPrintBoard, double newTimeoutTime) : red(NULL), blue(NULL), turn(Piece::RED), theBoard(10,10), graphicsEnabled(enableGraphics), stallTime(newStallTime), allowIllegalMoves(allowIllegal), log(newLog), reveal(newReveal), turnCount(0), input(NULL), maxTurns(newMaxTurns), printBoard(newPrintBoard), timeoutTime(newTimeoutTime)
+Game::Game(const char * redPath, const char * bluePath, const bool enableGraphics, double newStallTime, const bool allowIllegal, FILE * newLog, const  Piece::Colour & newReveal, int newMaxTurns, bool newPrintBoard, double newTimeoutTime, bool server, bool client) : red(NULL), blue(NULL), turn(Piece::RED), theBoard(10,10), graphicsEnabled(enableGraphics), stallTime(newStallTime), allowIllegalMoves(allowIllegal), log(newLog), reveal(newReveal), turnCount(0), input(NULL), maxTurns(newMaxTurns), printBoard(newPrintBoard), timeoutTime(newTimeoutTime)
 {
        gameCreated = false;
        if (gameCreated)
@@ -25,16 +25,33 @@ Game::Game(const char * redPath, const char * bluePath, const bool enableGraphic
                        Graphics::Initialise("Stratego", theBoard.Width()*32, theBoard.Height()*32);
        #endif //BUILD_GRAPHICS
 
-       if (strcmp(redPath, "human") == 0)
-               red = new Human_Controller(Piece::RED, graphicsEnabled);
+
+       if (client)
+       {
+               red = new Client(Piece::RED, "server", redPath); //TODO: Retrieve server address
+       }
        else
-               red = new AI_Controller(Piece::RED, redPath, timeoutTime);
+       {
+               assert(redPath != NULL);
+               if (strcmp(redPath, "human") == 0)
+                       red = new Human_Controller(Piece::RED, graphicsEnabled);
+               else
+                       red = new AI_Controller(Piece::RED, redPath, timeoutTime);
+       }
        
        
-       if (strcmp(bluePath, "human") == 0)
-               blue = new Human_Controller(Piece::BLUE, graphicsEnabled);
+       if (server)
+       {
+               blue = new Server(Piece::BLUE, "client");
+       }
        else
-               blue = new AI_Controller(Piece::BLUE, bluePath, timeoutTime);
+       {
+               assert(bluePath != NULL);
+               if (strcmp(bluePath, "human") == 0)
+                       blue = new Human_Controller(Piece::BLUE, graphicsEnabled);
+               else
+                       blue = new AI_Controller(Piece::BLUE, bluePath, timeoutTime);
+       }
 
 
 }
@@ -442,9 +459,24 @@ void Game::PrintEndMessage(const MovementResult & result)
        }
 
 }
+/** Checks for victory by attrition (destroying all mobile pieces)
+ *
+ *  @returns OK for no victory, 
+ *     DRAW if both players have no pieces, or 
+ *     VICTORY_ATTRITION  if the current player has won by attrition
+ */
+MovementResult Game::CheckVictoryAttrition()
+{
+        if (theBoard.MobilePieces(Piece::OppositeColour(turn)) == 0)
+       {
+               if (theBoard.MobilePieces(turn) == 0)
+                       return MovementResult::DRAW;
+               else
+                       return MovementResult::VICTORY_ATTRITION;
+       }
+       return MovementResult::OK;
 
-
-
+}
 MovementResult Game::Play()
 {
 
@@ -483,22 +515,27 @@ MovementResult Game::Play()
                #endif //BUILD_GRAPHICS
                
                turn = Piece::RED;
+               blue->Pause();
+               red->Continue();
+               if (!Board::HaltResult(result))
+               {
+                       result = CheckVictoryAttrition();
+               }
+               if (Board::HaltResult(result))
+                       break;
+
                logMessage( "%d RED: ", turnCount);
                result = red->MakeMove(buffer);
                red->Message(buffer);
                blue->Message(buffer);
                logMessage( "%s\n", buffer.c_str());
-               if (Board::HaltResult(result))
-                       break;
 
-               if (theBoard.MobilePieces(Piece::BLUE) == 0)
+               if (!Board::HaltResult(result))
                {
-                       if (theBoard.MobilePieces(Piece::RED) == 0)
-                               result = MovementResult::DRAW;
-                       else
-                               result = MovementResult::VICTORY_ATTRITION;
-                       break;                  
+                       result = CheckVictoryAttrition();
                }
+               if (Board::HaltResult(result))
+                       break;
 
                if (stallTime >= 0)
                        Wait(stallTime);
@@ -523,12 +560,25 @@ MovementResult Game::Play()
                
                
                turn = Piece::BLUE;
+               red->Pause();
+               blue->Continue();
+               if (!Board::HaltResult(result))
+               {
+                       result = CheckVictoryAttrition();
+               }
+               if (Board::HaltResult(result))
+                       break;
+
                logMessage( "%d BLU: ", turnCount);
                result = blue->MakeMove(buffer);
                blue->Message(buffer);
                red->Message(buffer);
                logMessage( "%s\n", buffer.c_str());
 
+               if (!Board::HaltResult(result))
+               {
+                       result = CheckVictoryAttrition();
+               }
                if (Board::HaltResult(result))
                        break;
 

UCC git Repository :: git.ucc.asn.au