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)
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);
+ }
}
}
}
+/** 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()
{
#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);
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;