Add version 1.2 of Celsius
[progcomp2012.git] / judge / manager / human_controller.cpp
1 #include "human_controller.h"
2
3 #include "game.h"
4
5 #include <iostream> //Really I can't be bothered with fscanf any more
6
7 using namespace std;
8
9 MovementResult Human_Controller::QuerySetup(const char * opponentName, string setup[])
10 {
11         
12         static bool shownMessage = false;
13         if (!shownMessage)
14         {
15                 if (graphicsEnabled)
16                         fprintf(stdout, "WARNING: GUI not fully supported. You will be forced to use the default setup.\n");
17                 else
18                 {
19                         fprintf(stdout,"Enter %d x %d Setup grid\n", Game::theGame->theBoard.Width(), 4);
20                         fprintf(stdout,"Please enter one line at a time.\n");
21                         fprintf(stdout, "You must place at least the Flag (%c). Use '%c' for empty squares.\n", Piece::tokens[(int)Piece::FLAG], Piece::tokens[(int)Piece::NOTHING]);
22                 
23                         switch (colour)
24                         {
25                                 case Piece::RED:
26                                         fprintf(stdout, "You are RED and occupy the top 4 rows of the board.\n");
27                                         fprintf(stdout, "NOTE: Enter \"DEFAULT\" to use the setup:\n");
28                                         fprintf(stdout, "FB8sB479B8\nBB31555583\n6724898974\n967B669999\n");
29                                         break;
30                                 case Piece::BLUE:
31                                         fprintf(stdout, "You are BLUE and occupy the bottom 4 rows of the board.\n");
32                                         fprintf(stdout, "NOTE: Enter \"DEFAULT\" to use the setup:\n");
33                                         fprintf(stdout, "967B669999\n6724898974\nBB31555583\nFB8sB479B8\n");
34                                         break;
35                                 default:
36                                         fprintf(stdout, "WARNING: Unknown colour error! Please exit the game.\n");
37                                         break;
38                         }       
39                 }       
40                 
41                 shownMessage = true;
42         }
43
44         if (graphicsEnabled)
45         {
46                 switch(colour)
47                 {
48                         case Piece::RED:
49                                 setup[0] = "FB8sB479B8"; 
50                                 setup[1] = "BB31555583";
51                                 setup[2] = "6724898974";
52                                 setup[3] = "967B669999";
53                                 break;
54                         case Piece::BLUE:
55                                 setup[0] = "967B669999";
56                                 setup[1] = "6724898974";
57                                 setup[2] = "BB31555583";
58                                 setup[3] = "FB8sB479B8";
59                                 break;
60                         default:
61                                 assert(false);
62                                 break;
63                         }
64                 return MovementResult::OK;
65         }
66
67         for (int y = 0; y < 4; ++y)
68         {
69                 cin >> setup[y];
70                 if (y == 0 && setup[0] == "DEFAULT")
71                 {
72                         switch(colour)
73                         {
74                                 case Piece::RED:
75                                         setup[0] = "FB8sB479B8"; 
76                                         setup[1] = "BB31555583";
77                                         setup[2] = "6724898974";
78                                         setup[3] = "967B669999";
79                                         break;
80                                 case Piece::BLUE:
81                                         setup[0] = "967B669999";
82                                         setup[1] = "6724898974";
83                                         setup[2] = "BB31555583";
84                                         setup[3] = "FB8sB479B8";
85                                         break;
86                                 default:
87                                         assert(false);
88                                         break;
89                         }
90                         break;
91                 }
92         }
93         assert(cin.get() == '\n');
94         
95         return MovementResult::OK;
96 }
97
98 MovementResult Human_Controller::QueryMove(string & buffer)
99 {
100         static bool shownMessage = false;
101         if (!shownMessage)
102         {
103                 if (!graphicsEnabled)
104                 {
105                         fprintf(stdout, "Please enter your move in the format:\n X Y DIRECTION [MULTIPLIER=1]\n");
106                         fprintf(stdout, "Where X and Y indicate the coordinates of the piece to move;\n DIRECTION is one of UP, DOWN, LEFT or RIGHT\n and MULTIPLIER is optional (and only valid for scouts (%c))\n", Piece::tokens[(int)(Piece::SCOUT)]);
107                         
108                 }
109                 shownMessage = true;
110         }
111
112         
113         #ifdef BUILD_GRAPHICS
114         if (graphicsEnabled)
115         {
116                 
117                 fprintf(stdout, "Click to move!\n");
118                 SDL_Event event; int mouseClick = 0;
119
120                 int x[] = {-1, -1}; int y[] = {-1, -1};
121                 while (mouseClick < 2)
122                 {
123                         
124                         while (SDL_PollEvent(&event))
125                         {
126                                 switch (event.type)
127                                 {
128                                         case SDL_QUIT:
129                                                 Game::theGame->logMessage("Exit called by human player!\n");
130                                                 exit(EXIT_SUCCESS);
131                                                 break;
132                                         case SDL_MOUSEBUTTONDOWN:
133                                         switch (event.button.button)
134                                         {
135                                                 case SDL_BUTTON_LEFT:
136                                                         SDL_GetMouseState(&x[mouseClick], &y[mouseClick]);
137                                                         x[mouseClick] /= 32; y[mouseClick] /= 32; //Adjust based on graphics grid size
138                                                         if (mouseClick == 0)
139                                                         {
140                                                                 stringstream s("");
141                                                                 s << x[0] << " " << y[0] << " ";
142                                                                 buffer += s.str();
143                                                         }
144                                                         else if (mouseClick == 1)
145                                                         {
146                                                                 int xDist = x[1] - x[0];
147                                                                 int yDist = y[1] - y[0];
148                                                                 int magnitude = max(abs(xDist), abs(yDist));
149                                                                 if (abs(xDist) > abs(yDist))
150                                                                 {
151                                                                         if (xDist < 0)
152                                                                                 buffer += "LEFT";
153                                                                         else
154                                                                                 buffer += "RIGHT";
155                                                                 }
156                                                                 else if (yDist < 0)
157                                                                         buffer += "UP";
158                                                                 else
159                                                                         buffer += "DOWN";
160
161                                                                 if (magnitude > 1)
162                                                                 {
163                                                                         stringstream s("");
164                                                                         s << " " << magnitude;
165                                                                         buffer += s.str();
166                                                                 }
167                                                         }
168                                                         mouseClick++;
169                                                         break;
170                                         }
171                                         break;
172                                 }
173                         }
174                 }
175                 fprintf(stdout, "Move complete!\n");
176                 
177         }
178         else
179         #endif //BUILD_GRAPHICS
180         {
181                 buffer.clear();
182                 for (char in = fgetc(stdin); in != '\n'; in = fgetc(stdin))
183                 {
184                         buffer += in;
185                 }
186         }
187         
188         
189
190         return MovementResult::OK;
191         
192 }

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