X-Git-Url: https://git.ucc.asn.au/?p=progcomp2012.git;a=blobdiff_plain;f=judge%2Fmanager%2Fgame.cpp;h=f50ce270610755a87fb779dcee7736e9bc2a1ffa;hp=ea7a7c9ef5ec8da5958e396118b557261c8066b4;hb=36bffb1754deb18b223a3206daa2478568675909;hpb=c4988773f023c2334d864cff5e95d4798c160935 diff --git a/judge/manager/game.cpp b/judge/manager/game.cpp index ea7a7c9..f50ce27 100644 --- a/judge/manager/game.cpp +++ b/judge/manager/game.cpp @@ -1,5 +1,7 @@ #include "game.h" #include +#include + using namespace std; @@ -7,7 +9,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, const char * newImageOutput) : 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), imageOutput(newImageOutput) { gameCreated = false; if (gameCreated) @@ -22,7 +24,13 @@ Game::Game(const char * redPath, const char * bluePath, const bool enableGraphic #ifdef BUILD_GRAPHICS if (graphicsEnabled && (!Graphics::Initialised())) - Graphics::Initialise("Stratego", theBoard.Width()*32, theBoard.Height()*32); + { + string s = "Stratego: "; + s += string(redPath); + s += " "; + s += string(bluePath); + Graphics::Initialise(s.c_str(), theBoard.Width()*GRID_SIZE, theBoard.Height()*GRID_SIZE); + } #endif //BUILD_GRAPHICS @@ -46,7 +54,7 @@ Game::Game(const char * redPath, const char * bluePath, const bool enableGraphic // logMessage("Game initialised.\n"); } -Game::Game(const char * fromFile, 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 * fromFile, const bool enableGraphics, double newStallTime, const bool allowIllegal, FILE * newLog, const Piece::Colour & newReveal, int newMaxTurns, bool newPrintBoard, double newTimeoutTime,const char * newImageOutput) : 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), imageOutput(newImageOutput) { gameCreated = false; if (gameCreated) @@ -60,7 +68,11 @@ Game::Game(const char * fromFile, const bool enableGraphics, double newStallTime #ifdef BUILD_GRAPHICS if (graphicsEnabled && (!Graphics::Initialised())) - Graphics::Initialise("Stratego", theBoard.Width()*32, theBoard.Height()*32); + { + string s = "Stratego: (file) "; + s += string(fromFile); + Graphics::Initialise(s.c_str(), theBoard.Width()*GRID_SIZE, theBoard.Height()*GRID_SIZE); + } #endif //BUILD_GRAPHICS input = fopen(fromFile, "r"); @@ -208,21 +220,23 @@ void Game::Wait(double wait) if (wait <= 0) return; - TimerThread timer(wait*1000000); //Wait in seconds - timer.Start(); + + #ifdef BUILD_GRAPHICS + + if (!graphicsEnabled) { - while (!timer.Finished()); - timer.Stop(); + usleep(1000000*wait); //Wait in seconds return; } - #endif //BUILD_GRAPHICS + TimerThread timer(wait*1000000); //Wait in seconds + timer.Start(); while (!timer.Finished()) { - #ifdef BUILD_GRAPHICS + SDL_Event event; while (SDL_PollEvent(&event)) { @@ -234,9 +248,12 @@ void Game::Wait(double wait) break; } } - #endif //BUILD_GRAPHICS } timer.Stop(); + + #else + usleep(wait*1000000); //Wait in seconds + #endif //BUILD_GRAPHICS } @@ -482,7 +499,7 @@ MovementResult Game::Play() // logMessage("Messaging red with \"START\"\n"); red->Message("START"); - + int moveCount = 0; while (!Board::HaltResult(result) && (turnCount < maxTurns || maxTurns < 0)) { @@ -501,7 +518,15 @@ MovementResult Game::Play() #ifdef BUILD_GRAPHICS if (graphicsEnabled) + { theBoard.Draw(toReveal); + if (imageOutput != "") + { + string imageFile = "" + imageOutput + "/"+ itostr(moveCount) + ".bmp"; + Graphics::ScreenShot(imageFile.c_str()); + } + + } #endif //BUILD_GRAPHICS turn = Piece::RED; @@ -541,10 +566,19 @@ MovementResult Game::Play() theBoard.PrintPretty(stdout, toReveal); fprintf(stdout, "\n\n"); } + + ++moveCount; #ifdef BUILD_GRAPHICS if (graphicsEnabled) + { theBoard.Draw(toReveal); + if (imageOutput != "") + { + string imageFile = "" + imageOutput + "/" + itostr(moveCount) + ".bmp"; + Graphics::ScreenShot(imageFile.c_str()); + } + } #endif //BUILD_GRAPHICS @@ -589,7 +623,7 @@ MovementResult Game::Play() else ReadUserCommand(); - + ++moveCount; ++turnCount; } @@ -858,6 +892,15 @@ void Game::MakeControllers(const char * redPath, const char * bluePath) red = new NetworkSender(Piece::RED, red, blueNetwork); logMessage(" (Red's responses will be copied over the connection)\n"); } + + red->FixName(); blue->FixName(); } +string itostr(int i) +{ + stringstream s; + s << i; + return s.str(); +} +