More adding of pointless crap to manager
[progcomp2012.git] / manager / controller.cpp
1 #include "controller.h"
2
3 #include <sstream>
4 #include "game.h"
5
6 using namespace std;
7
8 /**
9  * Queries the player to setup their pieces
10  *
11  */
12
13 MovementResult Controller::Setup(const char * opponentName)
14 {
15         string setup[4] = {"","","",""};
16         MovementResult query = this->QuerySetup(opponentName, setup);
17         if (query != MovementResult::OK)
18                 return query;
19
20         
21
22         int usedUnits[(int)(Piece::BOMB)];
23         for (int ii = 0; ii <= (int)(Piece::BOMB); ++ii)
24                 usedUnits[ii] = 0;
25
26         int yStart = 0;
27         switch (colour)
28         {
29                 case Piece::RED:
30                         yStart = 0;
31                         break;
32                 case Piece::BLUE:
33                         yStart = Game::theGame->theBoard.Height()-4;
34                         break;
35                 default:
36                         return MovementResult::COLOUR_ERROR; 
37                         break;
38         }
39
40
41         for (int y = 0; y < 4; ++y)
42         {
43                 if ((int)setup[y].length() != Game::theGame->theBoard.Width())
44                         return MovementResult::BAD_RESPONSE;
45
46                 for (int x = 0; x < Game::theGame->theBoard.Width(); ++x)
47                 {
48                         Piece::Type type = Piece::GetType(setup[y][x]);
49                         if (type != Piece::NOTHING)
50                         {
51                                 usedUnits[(int)(type)]++;
52                                 if (usedUnits[type] > Piece::maxUnits[(int)type])
53                                 {
54                                         //fprintf(stderr, "Too many units of type %c\n", Piece::tokens[(int)(type)]);
55                                         return MovementResult::BAD_RESPONSE;
56                                 }
57                                 Game::theGame->theBoard.AddPiece(x, yStart+y, type, colour);
58                         }
59                 }
60         }
61         if (usedUnits[(int)Piece::FLAG] <= 0)
62         {
63                 return MovementResult::BAD_RESPONSE; //You need to include a flag!
64         }
65
66         return MovementResult::OK;
67
68 }
69
70
71 /**
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
75  */
76 MovementResult Controller::MakeMove(string & buffer)
77 {
78         buffer.clear();
79         MovementResult query = this->QueryMove(buffer);
80         if (query != MovementResult::OK)
81                 return query;
82
83         
84         int x; int y; string direction="";
85         stringstream s(buffer);
86         s >> x;
87         s >> y;
88         
89
90         s >> direction;
91         Board::Direction dir;
92         if (direction == "UP")
93         {
94                 dir = Board::UP;
95         }
96         else if (direction == "DOWN")
97         {
98                 dir = Board::DOWN;
99         }
100         else if (direction == "LEFT")
101         {
102                 dir = Board::LEFT;
103         }
104         else if (direction == "RIGHT")
105         {
106                 dir = Board::RIGHT;
107         }       
108         else
109         {
110                 //fprintf(stderr, "BAD_RESPONSE \"%s\"\n", buffer.c_str());
111                 return MovementResult::BAD_RESPONSE; //Player gave bogus direction - it will lose by default.   
112         }
113
114         int multiplier = 1;
115         if (s.peek() != EOF)
116                 s >> multiplier;
117         MovementResult moveResult = Game::theGame->theBoard.MovePiece(x, y, dir, multiplier, colour);
118
119         s.clear();      s.str("");
120
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)
125         {
126                 case MovementResult::OK:
127                         buffer += " OK";
128                         break;
129                 case MovementResult::VICTORY:
130                         buffer += " FLAG";
131                         break;
132                 case MovementResult::KILLS:
133                         buffer += " KILLS ";
134                         buffer += s.str();
135
136                         break;
137                 case MovementResult::DIES:
138                         buffer += " DIES ";
139                         buffer += s.str();
140                         break;
141                 case MovementResult::BOTH_DIE:
142                         buffer += " BOTHDIE ";
143                         buffer += s.str();
144                         break;  
145                 default:
146                         buffer += " ILLEGAL";
147                         break;                  
148                 
149         }
150
151         if (Game::theGame->allowIllegalMoves && !Board::LegalResult(moveResult))
152                 return MovementResult::OK; //HACK - Legal results returned!
153         else
154                 return moveResult;      
155
156 }

UCC git Repository :: git.ucc.asn.au