1 #include "controller.h"
9 * Queries the player to setup their pieces
13 MovementResult Controller::Setup(const char * opponentName)
15 string setup[4] = {"","","",""};
16 MovementResult query = this->QuerySetup(opponentName, setup);
17 if (query != MovementResult::OK)
22 int usedUnits[(int)(Piece::BOMB)];
23 for (int ii = 0; ii <= (int)(Piece::BOMB); ++ii)
33 yStart = Game::theGame->theBoard.Height()-4;
36 return MovementResult::COLOUR_ERROR;
41 for (int y = 0; y < 4; ++y)
43 if ((int)setup[y].length() != Game::theGame->theBoard.Width())
44 return MovementResult::BAD_RESPONSE;
46 for (int x = 0; x < Game::theGame->theBoard.Width(); ++x)
48 Piece::Type type = Piece::GetType(setup[y][x]);
49 if (type != Piece::NOTHING)
51 usedUnits[(int)(type)]++;
52 if (usedUnits[type] > Piece::maxUnits[(int)type])
54 //fprintf(stderr, "Too many units of type %c\n", Piece::tokens[(int)(type)]);
55 return MovementResult::BAD_RESPONSE;
57 Game::theGame->theBoard.AddPiece(x, yStart+y, type, colour);
61 if (usedUnits[(int)Piece::FLAG] <= 0)
63 return MovementResult::BAD_RESPONSE; //You need to include a flag!
66 return MovementResult::OK;
72 * Queries the player to respond to a state of Game::theGame->theBoard
73 * @param buffer String which is used to store the player's responses
74 * @returns The result of the response and/or move if made
76 MovementResult Controller::MakeMove(string & buffer)
79 MovementResult query = this->QueryMove(buffer);
80 if (query != MovementResult::OK)
84 int x; int y; string direction="";
85 stringstream s(buffer);
92 if (direction == "UP")
96 else if (direction == "DOWN")
100 else if (direction == "LEFT")
104 else if (direction == "RIGHT")
110 //fprintf(stderr, "BAD_RESPONSE \"%s\"\n", buffer.c_str());
111 return MovementResult::BAD_RESPONSE; //Player gave bogus direction - it will lose by default.
117 MovementResult moveResult = Game::theGame->theBoard.MovePiece(x, y, dir, multiplier, colour);
119 s.clear(); s.str("");
121 //I stored the ranks in the wrong order; rank 1 is the marshal, 2 is the general etc...
122 //So I am reversing them in the output... great work
123 s << Piece::tokens[(int)(moveResult.attackerRank)] << " " << Piece::tokens[(int)(moveResult.defenderRank)];
124 switch (moveResult.type)
126 case MovementResult::OK:
129 case MovementResult::VICTORY:
132 case MovementResult::KILLS:
137 case MovementResult::DIES:
141 case MovementResult::BOTH_DIE:
142 buffer += " BOTHDIE ";
146 buffer += " ILLEGAL";
151 if (Game::theGame->allowIllegalMoves && !Board::LegalResult(moveResult))
152 return MovementResult::OK; //HACK - Legal results returned!