X-Git-Url: https://git.ucc.asn.au/?p=progcomp2012.git;a=blobdiff_plain;f=manager%2Fmain.cpp;fp=manager%2Fmain.cpp;h=6adc34de53d5c7970de288a22ecc5e9cb32c5456;hp=e3e435e6c5c764674617554d7e48e02c1e2d5b4a;hb=4a3c0478160e7e9b637a12e7cf22f8da61b66ad2;hpb=53a666903d770b14969f542d6548e267e5017b31;ds=sidebyside diff --git a/manager/main.cpp b/manager/main.cpp index e3e435e..6adc34d 100644 --- a/manager/main.cpp +++ b/manager/main.cpp @@ -1,198 +1,162 @@ #include #include -#include "common.h" -#include "controller.h" -#include "stratego.h" -using namespace std; - - - -#define theBoard Board::theBoard -#ifdef GRAPHICS - bool CheckForQuitWhilstWaiting(int wait); -#endif //GRAPHICS -Controller * red; -Controller * blue; -Colour turn; -void cleanup(); +#include "game.h" -void BrokenPipe(int sig); +using namespace std; int main(int argc, char ** argv) { - assert(argc == 3); - - - for (int y = 4; y < 6; ++y) + + char * red = NULL; char * blue = NULL; double timeout = 0.00001; bool graphics = false; bool allowIllegal = false; FILE * log = NULL; + Piece::Colour reveal = Piece::BOTH; + for (int ii=1; ii < argc; ++ii) { - for (int x = 2; x < 4; ++x) + if (argv[ii][0] == '-') { - theBoard.AddPiece(x,y,Piece::BOULDER, Piece::NONE); + switch (argv[ii][1]) + { + case 't': + if (argc - ii <= 1) + { + fprintf(stderr, "Expected timeout value after -t switch!\n"); + exit(EXIT_FAILURE); + } + timeout = atof(argv[ii+1]); + ++ii; + break; + case 'g': + graphics = true; + break; + case 'i': + allowIllegal = true; + break; + + case 'o': + if (argc - ii <= 1) + { + fprintf(stderr, "Expected filename or \"stdout\" after -o switch!\n"); + exit(EXIT_FAILURE); + } + if (log != NULL) + { + fprintf(stderr, "Expected at most ONE -o switch!\n"); + exit(EXIT_FAILURE); + } + if (strcmp(argv[ii+1], "stdout") == 0) + log = stdout; + else + log = fopen(argv[ii+1], "w"); + setbuf(log, NULL); + + ++ii; + break; + + case 'r': + if (reveal == Piece::BOTH) + reveal = Piece::BLUE; + else + reveal = Piece::NONE; + break; + case 'b': + if (reveal == Piece::BOTH) + reveal = Piece::RED; + else + reveal = Piece::NONE; + break; + case 'h': + system("clear"); + system("less manual.txt"); + exit(EXIT_SUCCESS); + break; + case '-': + if (strcmp(argv[ii]+2, "help") == 0) + { + system("clear"); + system("less manual.txt"); + exit(EXIT_SUCCESS); + } + else + { + fprintf(stderr, "Unrecognised switch \"%s\"...\n", argv[ii]); + exit(EXIT_FAILURE); + } + } + } - for (int x = 6; x < 8; ++x) + else { - theBoard.AddPiece(x,y,Piece::BOULDER, Piece::NONE); + if (red == NULL) + red = argv[ii]; + else if (blue == NULL) + blue = argv[ii]; + else + { + fprintf(stderr, "Unexpected argument \"%s\"...\n", argv[ii]); + exit(EXIT_FAILURE); + } } } - - - red = new Controller(Piece::RED, argv[1]); - blue = new Controller(Piece::BLUE, argv[2]); - atexit(cleanup); - signal(SIGPIPE, BrokenPipe); - - MovementResult redSetup = red->Setup(argv[2]); - MovementResult blueSetup = blue->Setup(argv[1]); - if (redSetup != MovementResult::OK) + if (argc == 1) { - fprintf(stderr, "Blue wins by DEFAULT!\n"); - red->SendMessage("ILLEGAL"); - blue->SendMessage("DEFAULT"); + fprintf(stderr, "Usage: stratego [options] red blue\n"); + fprintf(stderr, " stratego --help\n"); exit(EXIT_SUCCESS); + } - if (blueSetup != MovementResult::OK) + + Game game(red, blue, graphics, timeout, allowIllegal, log, reveal); + + + if (!game.Setup(red, blue)) { - fprintf(stderr, "Red wins by DEFAULT!\n"); - red->SendMessage("DEFAULT"); - blue->SendMessage("ILLEGAL"); + fprintf(stdout, "NONE %d\n",game.TurnCount()); exit(EXIT_SUCCESS); } - MovementResult result(MovementResult::OK); - system("clear"); - int count = 1; - - #ifdef GRAPHICS - if (!Graphics::Initialised()) - Graphics::Initialise("Stratego", theBoard.Width()*32, theBoard.Height()*32); - - #endif //GRAPHICS - - string buffer; + MovementResult result = game.Play(); + game.PrintEndMessage(result); - red->SendMessage("START"); - turn = Piece::RED; - while (Board::LegalResult(result)) + Piece::Colour winner = game.Turn(); + if (Board::LegalResult(result)) { - - - turn = Piece::RED; - fprintf(stderr, "%d RED: ", count); - result = red->MakeMove(buffer); - red->SendMessage(buffer); - blue->SendMessage(buffer); - fprintf(stderr, "%s\n", buffer.c_str()); - if (!Board::LegalResult(result)) - break; - #ifdef GRAPHICS - Board::theBoard.Draw(); - if (CheckForQuitWhilstWaiting(0.5)) - { - red->SendMessage("QUIT"); - blue->SendMessage("QUIT"); - exit(EXIT_SUCCESS); - } - #endif //GRAPHICS - - turn = Piece::BLUE; - fprintf(stderr, "%d BLU: ", count); - result = blue->MakeMove(buffer); - blue->SendMessage(buffer); - red->SendMessage(buffer); - fprintf(stderr, "%s\n", buffer.c_str()); - - if (!Board::LegalResult(result)) - break; - - - - #ifdef GRAPHICS - Board::theBoard.Draw(); - if (CheckForQuitWhilstWaiting(0.5)) - { - red->SendMessage("QUIT"); - blue->SendMessage("QUIT"); - exit(EXIT_SUCCESS); - } - #else - Board::theBoard.Print(stderr); - sleep(1); - system("clear"); - #endif //GRAPHICS - - ++count; + if (winner == Piece::BOTH) + winner = Piece::NONE; + else + { + if (winner == Piece::RED) + winner = Piece::BLUE; + else + winner = Piece::RED; + } } + - printf("Final board state\n"); - #ifdef GRAPHICS - Board::theBoard.Draw(); - - if (CheckForQuitWhilstWaiting(4)) - { - red->SendMessage("QUIT"); - blue->SendMessage("QUIT"); - exit(EXIT_SUCCESS); - } - - #else - Board::theBoard.Print(stderr); - #endif //GRAPHICS - sleep(2); - - - if (turn == Piece::RED) - { - fprintf(stderr,"Game ends on RED's turn - REASON: "); - } - else if (turn == Piece::BLUE) - { - fprintf(stderr,"Game ends on BLUE's turn - REASON: "); - } - else - { - fprintf(stderr,"Game ends on ERROR's turn - REASON: "); - - } - switch (result.type) + switch (winner) { - case MovementResult::NO_BOARD: - fprintf(stderr,"Board does not exit?!\n"); - break; - case MovementResult::INVALID_POSITION: - fprintf(stderr,"Coords outside board\n"); - break; - case MovementResult::NO_SELECTION: - fprintf(stderr,"Move does not select a piece\n"); + case Piece::RED: + fprintf(stdout, "%s RED %d\n", red,game.TurnCount()); break; - case MovementResult::NOT_YOUR_UNIT: - fprintf(stderr,"Selected piece belongs to other player\n"); + case Piece::BLUE: + fprintf(stdout, "%s BLUE %d\n", blue,game.TurnCount()); break; - case MovementResult::IMMOBILE_UNIT: - fprintf(stderr,"Selected piece is not mobile (FLAG or BOMB)\n"); + case Piece::BOTH: + fprintf(stdout, "DRAW %d\n",game.TurnCount()); break; - case MovementResult::INVALID_DIRECTION: - fprintf(stderr,"Selected unit cannot move that way\n"); - break; - case MovementResult::POSITION_FULL: - fprintf(stderr,"Attempted move into square occupied by allied piece\n"); - break; - case MovementResult::VICTORY: - fprintf(stderr,"Captured the flag\n"); - break; - case MovementResult::BAD_RESPONSE: - fprintf(stderr,"Unintelligable response\n"); - break; - case MovementResult::NO_MOVE: - fprintf(stderr,"Did not make a move (may have exited)\n"); + case Piece::NONE: + fprintf(stdout, "NONE %d\n",game.TurnCount()); break; + } + + + exit(EXIT_SUCCESS); @@ -200,66 +164,4 @@ int main(int argc, char ** argv) return 0; } -#ifdef GRAPHICS - -bool CheckForQuitWhilstWaiting(int wait) -{ - - - TimerThread timer(wait*1000000); //Wait in seconds - timer.Start(); - while (!timer.Finished()) - { - SDL_Event event; - while (SDL_PollEvent(&event)) - { - switch (event.type) - { - case SDL_QUIT: - timer.Stop(); - return true; - break; - } - } - } - timer.Stop(); - return false; -} - -void cleanup() -{ - delete red; - delete blue; -} -void BrokenPipe(int sig) -{ - if (turn == Piece::RED) - { - fprintf(stderr,"Game ends on RED's turn - REASON: Broken pipe\n"); - blue->SendMessage("DEFAULT"); - } - else if (turn == Piece::BLUE) - { - fprintf(stderr,"Game ends on BLUE's turn - REASON: Broken pipe\n"); - red->SendMessage("DEFAULT"); - } - else - { - fprintf(stderr,"Game ends on ERROR's turn - REASON: Broken pipe\n"); - - } - Board::theBoard.Draw(); - while (true) - { - if (CheckForQuitWhilstWaiting(4000)) - { - red->SendMessage("QUIT"); - blue->SendMessage("QUIT"); - exit(EXIT_SUCCESS); - } - } - exit(EXIT_SUCCESS); -} - -#endif //GRAPHICS