X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=manager%2Fcontroller.cpp;h=20b79aa101328775c265e471db403817adce8b65;hb=HEAD;hp=b7ab105bb17f22f77a42d53dfd4a5a39b524c15c;hpb=b563784f7e8b559fc100e174331c99fc6a1beda8;p=progcomp2012.git diff --git a/manager/controller.cpp b/manager/controller.cpp deleted file mode 100644 index b7ab105..0000000 --- a/manager/controller.cpp +++ /dev/null @@ -1,156 +0,0 @@ -#include "controller.h" - -#include -#include "game.h" - -using namespace std; - -/** - * Queries the player to setup their pieces - * - */ - -MovementResult Controller::Setup(const char * opponentName) -{ - string setup[4] = {"","","",""}; - MovementResult query = this->QuerySetup(opponentName, setup); - if (query != MovementResult::OK) - return query; - - - - int usedUnits[(int)(Piece::BOMB)]; - for (int ii = 0; ii <= (int)(Piece::BOMB); ++ii) - usedUnits[ii] = 0; - - int yStart = 0; - switch (colour) - { - case Piece::RED: - yStart = 0; - break; - case Piece::BLUE: - yStart = Game::theGame->theBoard.Height()-4; - break; - default: - return MovementResult::COLOUR_ERROR; - break; - } - - - for (int y = 0; y < 4; ++y) - { - if ((int)setup[y].length() != Game::theGame->theBoard.Width()) - return MovementResult::BAD_RESPONSE; - - for (int x = 0; x < Game::theGame->theBoard.Width(); ++x) - { - Piece::Type type = Piece::GetType(setup[y][x]); - if (type != Piece::NOTHING) - { - usedUnits[(int)(type)]++; - if (usedUnits[type] > Piece::maxUnits[(int)type]) - { - //fprintf(stderr, "Too many units of type %c\n", Piece::tokens[(int)(type)]); - return MovementResult::BAD_RESPONSE; - } - Game::theGame->theBoard.AddPiece(x, yStart+y, type, colour); - } - } - } - if (usedUnits[(int)Piece::FLAG] <= 0) - { - return MovementResult::BAD_RESPONSE; //You need to include a flag! - } - - return MovementResult::OK; - -} - - -/** - * Queries the player to respond to a state of Game::theGame->theBoard - * @param buffer String which is used to store the player's responses - * @returns The result of the response and/or move if made - */ -MovementResult Controller::MakeMove(string & buffer) -{ - buffer.clear(); - MovementResult query = this->QueryMove(buffer); - if (query != MovementResult::OK) - return query; - - - int x; int y; string direction=""; - stringstream s(buffer); - s >> x; - s >> y; - - - s >> direction; - Board::Direction dir; - if (direction == "UP") - { - dir = Board::UP; - } - else if (direction == "DOWN") - { - dir = Board::DOWN; - } - else if (direction == "LEFT") - { - dir = Board::LEFT; - } - else if (direction == "RIGHT") - { - dir = Board::RIGHT; - } - else - { - //fprintf(stderr, "BAD_RESPONSE \"%s\"\n", buffer.c_str()); - return MovementResult::BAD_RESPONSE; //Player gave bogus direction - it will lose by default. - } - - int multiplier = 1; - if (s.peek() != EOF) - s >> multiplier; - MovementResult moveResult = Game::theGame->theBoard.MovePiece(x, y, dir, multiplier, colour); - - s.clear(); s.str(""); - - //I stored the ranks in the wrong order; rank 1 is the marshal, 2 is the general etc... - //So I am reversing them in the output... great work - s << Piece::tokens[(int)(moveResult.attackerRank)] << " " << Piece::tokens[(int)(moveResult.defenderRank)]; - switch (moveResult.type) - { - case MovementResult::OK: - buffer += " OK"; - break; - case MovementResult::VICTORY: - buffer += " FLAG"; - break; - case MovementResult::KILLS: - buffer += " KILLS "; - buffer += s.str(); - - break; - case MovementResult::DIES: - buffer += " DIES "; - buffer += s.str(); - break; - case MovementResult::BOTH_DIE: - buffer += " BOTHDIE "; - buffer += s.str(); - break; - default: - buffer += " ILLEGAL"; - break; - - } - - if (Game::theGame->allowIllegalMoves && !Board::LegalResult(moveResult)) - return MovementResult::OK; //HACK - Legal results returned! - else - return moveResult; - -}