Tweaking manager/simulate.py, updating manual and webpage
[progcomp2012.git] / judge / 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         //Removed 3/01/12 NO_MOVE now not allowed, SURRENDER is undocumented and not necessary
85         if (buffer == "NO_MOVE")
86         {
87                 buffer += " OK";
88                 return MovementResult::OK;
89         }
90         if (buffer == "SURRENDER")
91         {
92                 buffer += " OK";
93                 return MovementResult::SURRENDER;
94         }
95         */
96         
97         int x; int y; string direction="";
98         stringstream s(buffer);
99         s >> x;
100         s >> y;
101         
102
103         s >> direction;
104         Board::Direction dir;
105         if (direction == "UP")
106         {
107                 dir = Board::UP;
108         }
109         else if (direction == "DOWN")
110         {
111                 dir = Board::DOWN;
112         }
113         else if (direction == "LEFT")
114         {
115                 dir = Board::LEFT;
116         }
117         else if (direction == "RIGHT")
118         {
119                 dir = Board::RIGHT;
120         }       
121         else
122         {
123                 if (Game::theGame->allowIllegalMoves)
124                         return MovementResult::OK;
125                 else
126                         return MovementResult::BAD_RESPONSE; //Player gave bogus direction - it will lose by default.   
127         }
128
129         int multiplier = 1;
130         if (s.peek() != EOF)
131                 s >> multiplier;
132         MovementResult moveResult = Game::theGame->theBoard.MovePiece(x, y, dir, multiplier, colour);
133
134         s.clear();      s.str("");
135
136         //I stored the ranks in the wrong order; rank 1 is the marshal, 2 is the general etc...
137         //So I am reversing them in the output... great work
138         s << Piece::tokens[(int)(moveResult.attackerRank)] << " " << Piece::tokens[(int)(moveResult.defenderRank)];     
139         switch (moveResult.type)
140         {
141                 case MovementResult::OK:
142                         buffer += " OK";
143                         break;
144                 case MovementResult::VICTORY_FLAG:
145                         buffer += " VICTORY_FLAG";
146                         break;
147                 case MovementResult::VICTORY_ATTRITION:
148                         buffer += " VICTORY_ATTRITION";
149                         break;
150                 case MovementResult::KILLS:
151                         buffer += " KILLS ";
152                         buffer += s.str();
153
154                         break;
155                 case MovementResult::DIES:
156                         buffer += " DIES ";
157                         buffer += s.str();
158                         break;
159                 case MovementResult::BOTH_DIE:
160                         buffer += " BOTHDIE ";
161                         buffer += s.str();
162                         break;  
163                 default:
164                         buffer += " ILLEGAL";
165                         break;                  
166                 
167         }
168
169         
170         if (!Board::LegalResult(moveResult))
171         {
172                 
173                 if (Game::theGame->allowIllegalMoves)
174                 {
175                         
176                         return MovementResult::OK; //HACK - Illegal results returned as legal! (Move not made)
177                 }
178                 else if (this->HumanController()) //Cut human controllers some slack and let them try again...
179                 {
180                         //Yes, checking type of object is "not the C++ way"
181                         //      But sometimes its bloody useful to know!!!
182                         Message("Bad move: \'" + buffer + "\' <- Please try again!");
183                         buffer = "";
184                         return this->MakeMove(buffer);
185                 }
186         }
187
188         return moveResult;      
189
190 }
191
192 /**
193  * Fixes the name of the controller
194  * Should be called AFTER the constructor, since it modifies the name string, which might be used in the constructor
195  */
196 void Controller::FixName()
197 {
198         for (unsigned int ii=0; ii < name.size(); ++ii)
199         {
200                 if (name[ii] == ' ')
201                 {
202                         name.erase(ii); //Just erase everything after the first whitespace
203                         break;
204                 }
205         }
206         //This is kind of hacky; I added it so that I could pass arguments to the AIs
207         //Because simulate.py doesn't like extra arguments showing up in the AI name at the end of the game (its fine until then)
208         //So I'll just remove them, after they have been passed! What could possibly go wrong?
209         // - Last entry in Sam Moore's diary, 2012
210 }
211

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