X-Git-Url: https://git.ucc.asn.au/?p=progcomp2012.git;a=blobdiff_plain;f=judge%2Fmanager%2Fgame.cpp;h=f25c9f381cfcc62f25eaa951b3928bd34e44468d;hp=8d3eef0c398c7ab46ee64eaa144c8caf4e9b933f;hb=341297b4dce9528d54fe9dceeff0d6dc33f70abe;hpb=38c6e9b9fc245ca5e6e6cee2806cb64dcbd34e35 diff --git a/judge/manager/game.cpp b/judge/manager/game.cpp index 8d3eef0..f25c9f3 100644 --- a/judge/manager/game.cpp +++ b/judge/manager/game.cpp @@ -25,18 +25,25 @@ 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); - else - red = new AI_Controller(Piece::RED, redPath, timeoutTime); - - - if (strcmp(bluePath, "human") == 0) - blue = new Human_Controller(Piece::BLUE, graphicsEnabled); - else - blue = new AI_Controller(Piece::BLUE, bluePath, timeoutTime); + MakeControllers(redPath, bluePath); + + if (red == NULL || blue == NULL) + { + fprintf(stderr, "Game::Game - Error creating controller: "); + if (red == NULL) + { + if (blue == NULL) + fprintf(stderr, " BOTH! (red: \"%s\", blue: \"%s\"\n", redPath, bluePath); + else + fprintf(stderr, " RED! (red: \"%s\")\n", redPath); + } + else + fprintf(stderr, "BLUE! (blue: \"%s\")\n", bluePath); + exit(EXIT_FAILURE); + } + 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) @@ -472,7 +479,7 @@ MovementResult Game::Play() - + logMessage("Messaging red with \"START\"\n"); red->Message("START"); @@ -753,4 +760,101 @@ int Game::Tokenise(std::vector & buffer, std::string & str, char split) return buffer.size(); } +/** + * Creates Controller baseds off strings. Takes into account controllers other than AI_Controller. + * @param redPath - Either the path to an AI_Controller compatable executable, or one of %human or %network or %network:[IP_ADDRESS] + * @param bluePath - Ditto + * Sets this->red to a controller using redPath, and this->blue to a controller using bluePath + * TODO: Make nicer (this function should be ~half the length) + */ +void Game::MakeControllers(const char * redPath, const char * bluePath) +{ + Network * redNetwork = NULL; + Network * blueNetwork = NULL; + + if (redPath[0] == '@') + { + if (strcmp(redPath, "@human") == 0) + red = new Human_Controller(Piece::RED, graphicsEnabled); + else + { + const char * network = strstr(redPath, "@network"); + if (network == NULL) + { + red = NULL; + return; + } + network = strstr(network, ":"); + + if (network == NULL) + { + logMessage("Creating server for red AI... "); + redNetwork = new Server(); + logMessage("Successful!\n"); + + } + else + { + network = (const char*)(network+1); + logMessage("Creating client for red AI... "); + redNetwork = new Client(network); + logMessage("Connected to address %s\n", network); + } + + logMessage(" (Red's responses will be received over the connection)\n"); + red = new NetworkReceiver(Piece::RED, redNetwork); + } + } + else + red = new AI_Controller(Piece::RED, redPath, timeoutTime); + + if (bluePath[0] == '@') + { + if (strcmp(bluePath, "@human") == 0) + blue = new Human_Controller(Piece::BLUE, graphicsEnabled); + else + { + const char * network = strstr(bluePath, "@network"); + if (network == NULL) + { + blue = NULL; + return; + } + network = strstr(network, ":"); + + if (network == NULL) + { + logMessage("Creating server for blue AI... "); + blueNetwork = new Server(); + logMessage("Successful!\n"); + + } + else + { + network = (const char*)(network+1); + logMessage("Creating client for blue AI... "); + blueNetwork = new Client(network); + logMessage("Connected to address %s\n", network); + } + logMessage(" (Blue's responses will be received over the connection)\n"); + blue = new NetworkReceiver(Piece::BLUE, blueNetwork); + } + } + else + blue = new AI_Controller(Piece::BLUE, bluePath, timeoutTime); + + if (redNetwork != NULL) + { + + blue = new NetworkSender(Piece::BLUE,blue, redNetwork); + logMessage(" (Blue's responses will be copied over the connection)\n"); + } + if (blueNetwork != NULL) + { + + red = new NetworkSender(Piece::RED, red, blueNetwork); + logMessage(" (Red's responses will be copied over the connection)\n"); + } + +}