First actual commit
[progcomp2012.git] / manager / controller.cpp
1 #include <sstream>
2
3 #include "stratego.h"
4
5 #include "controller.h"
6
7 using namespace std;
8
9 /**
10  * Queries the AI program to setup its pieces
11  * @returns the result of the response
12  */
13 Board::MovementResult Controller::Setup(const char * opponentName)
14 {
15         int y;
16         switch (colour)
17         {
18                 case Piece::RED:
19                         assert(SendMessage("RED %s %d %d", opponentName, Board::theBoard.Width(), Board::theBoard.Height()));
20                         y = 0;
21                         
22                         break;
23                 case Piece::BLUE:
24                         assert(SendMessage("BLUE %s %d %d", opponentName, Board::theBoard.Width(), Board::theBoard.Height()));
25                         y = Board::theBoard.Height()-4;
26                         
27                         break;
28                 case Piece::NONE:
29                 case Piece::BOTH:
30                         //Should never see this;
31                         assert(false);
32                         break;
33         }
34
35
36         int usedUnits[(int)(Piece::BOMB)];
37         for (int ii = 0; ii <= (int)(Piece::BOMB); ++ii)
38                 usedUnits[ii] = 0;
39
40         //The setup is spread across 4 lines of the board - blue at the top, red at the bottom. AI has 2.5s for each line.
41         
42
43
44
45         for (int ii=0; ii < 4; ++ii)
46         {
47                 string line="";
48                 if (!GetMessage(line, 2.5))
49                 {
50                         fprintf(stderr, "Timeout on setup\n");
51                         return Board::BAD_RESPONSE;
52                 }
53                 if ((int)(line.size()) != Board::theBoard.Width())
54                 {
55                         fprintf(stderr, "Bad length of \"%s\" on setup\n", line.c_str());
56                         return Board::BAD_RESPONSE;
57                 }
58         
59                 for (int x = 0; x < (int)(line.size()); ++x)
60                 {
61                         Piece::Type type = Piece::GetType(line[x]);
62                         if (type != Piece::NOTHING)
63                         {
64 //fprintf(stderr, "x y %d %d\n", x, y+ii);
65 //                                      fprintf(stderr, "Found unit of type '%c' (%d '%c') %d vs %d\n", line[x], (int)(type), Piece::tokens[(int)(type)], usedUnits[(int)(type)], Piece::maxUnits[(int)type]);
66                 ///                     fprintf(stderr, "Marshal is %d '%c', flag is %d '%c'\n", (int)Piece::MARSHAL, Piece::tokens[(int)(Piece::MARSHAL)], (int)Piece::FLAG, Piece::tokens[(int)(Piece::FLAG)]);
67
68                                 usedUnits[(int)(type)] += 1;
69                                 if (usedUnits[type] > Piece::maxUnits[(int)type])
70                                 {
71                                         fprintf(stderr, "Too many units of type %c\n", Piece::tokens[(int)(type)]);
72                                         return Board::BAD_RESPONSE;
73                                 }
74         
75                                 Board::theBoard.AddPiece(x, y+ii, type, colour);
76                         }
77                 }       
78         }
79
80         if (usedUnits[(int)Piece::FLAG] <= 0)
81         {
82                 return Board::BAD_RESPONSE; //You need to include a flag!
83         }
84
85         return Board::OK;
86 }
87
88
89 /**
90  * Queries the AI program to respond to a state of Board::theBoard
91  * @returns The result of the response and/or move if made
92  */
93 Board::MovementResult Controller::MakeMove(string & buffer)
94 {
95         
96         if (!Running())
97                 return Board::NO_MOVE; //AI has quit
98         Board::theBoard.Print(output, colour);
99
100         
101
102         
103         buffer.clear();
104         if (!GetMessage(buffer,2))
105         {
106                 return Board::NO_MOVE; //AI did not respond. It will lose by default.
107         }
108
109         int x; int y; string direction="";
110         stringstream s(buffer);
111         s >> x;
112         s >> y;
113         
114
115         s >> direction;
116         Board::Direction dir;
117         if (direction == "UP")
118         {
119                 dir = Board::UP;
120         }
121         else if (direction == "DOWN")
122         {
123                 dir = Board::DOWN;
124         }
125         else if (direction == "LEFT")
126         {
127                 dir = Board::LEFT;
128         }
129         else if (direction == "RIGHT")
130         {
131                 dir = Board::RIGHT;
132         }       
133         else
134         {
135                 fprintf(stderr, "BAD_RESPONSE \"%s\"\n", buffer.c_str());
136                 return Board::BAD_RESPONSE; //AI gave bogus direction - it will lose by default.        
137         }
138
139         int multiplier = 1;
140         if (s.peek() != EOF)
141                 s >> multiplier;
142         Board::MovementResult moveResult = Board::theBoard.MovePiece(x, y, dir, multiplier, colour);
143         switch (moveResult)
144         {
145                 case Board::OK:
146                         buffer += " OK";
147                         break;
148                 case Board::VICTORY:
149                         buffer += " FLAG";
150                         break;
151                 case Board::KILLS:
152                         buffer += " KILLS";
153                         break;
154                 case Board::DIES:
155                         buffer += " DIES";
156                         break;
157                 case Board::BOTH_DIE:
158                         buffer += " BOTHDIE";
159                         break;  
160                 default:
161                         buffer += " ILLEGAL";
162                         break;                  
163                 
164         }
165
166         if (!Board::LegalResult(moveResult))
167                 return Board::OK; //HACK - Legal results returned!
168         else
169                 return moveResult;      
170
171 }
172

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