Modified manager output/protocol, added "basic" AI, made Asmodeus better
[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         if (buffer == "NO_MOVE")
84         {
85                 buffer += " OK";
86                 return MovementResult::OK;
87         }
88         if (buffer == "SURRENDER")
89         {
90                 buffer += " OK";
91                 return MovementResult::SURRENDER;
92         }
93         
94         int x; int y; string direction="";
95         stringstream s(buffer);
96         s >> x;
97         s >> y;
98         
99
100         s >> direction;
101         Board::Direction dir;
102         if (direction == "UP")
103         {
104                 dir = Board::UP;
105         }
106         else if (direction == "DOWN")
107         {
108                 dir = Board::DOWN;
109         }
110         else if (direction == "LEFT")
111         {
112                 dir = Board::LEFT;
113         }
114         else if (direction == "RIGHT")
115         {
116                 dir = Board::RIGHT;
117         }       
118         else
119         {
120                 //fprintf(stderr, "BAD_RESPONSE \"%s\"\n", buffer.c_str());
121                 return MovementResult::BAD_RESPONSE; //Player gave bogus direction - it will lose by default.   
122         }
123
124         int multiplier = 1;
125         if (s.peek() != EOF)
126                 s >> multiplier;
127         MovementResult moveResult = Game::theGame->theBoard.MovePiece(x, y, dir, multiplier, colour);
128
129         s.clear();      s.str("");
130
131         //I stored the ranks in the wrong order; rank 1 is the marshal, 2 is the general etc...
132         //So I am reversing them in the output... great work
133         s << Piece::tokens[(int)(moveResult.attackerRank)] << " " << Piece::tokens[(int)(moveResult.defenderRank)];     
134         switch (moveResult.type)
135         {
136                 case MovementResult::OK:
137                         buffer += " OK";
138                         break;
139                 case MovementResult::VICTORY:
140                         buffer += " FLAG";
141                         break;
142                 case MovementResult::KILLS:
143                         buffer += " KILLS ";
144                         buffer += s.str();
145
146                         break;
147                 case MovementResult::DIES:
148                         buffer += " DIES ";
149                         buffer += s.str();
150                         break;
151                 case MovementResult::BOTH_DIE:
152                         buffer += " BOTHDIE ";
153                         buffer += s.str();
154                         break;  
155                 default:
156                         buffer += " ILLEGAL";
157                         break;                  
158                 
159         }
160
161         if (Game::theGame->allowIllegalMoves && !Board::LegalResult(moveResult))
162                 return MovementResult::OK; //HACK - Illegal results returned as legal!
163         else
164                 return moveResult;      
165
166 }

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