Improved -> Broke -> Fixed basic_python AI
[progcomp2012.git] / progcomp / 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
114         if (graphicsEnabled)
115         {
116                 fprintf(stdout, "Click to move!\n");
117                 SDL_Event event; int mouseClick = 0;
118
119                 int x[] = {-1, -1}; int y[] = {-1, -1};
120                 while (mouseClick < 2)
121                 {
122                         
123                         while (SDL_PollEvent(&event))
124                         {
125                                 switch (event.type)
126                                 {
127                                         case SDL_QUIT:
128                                                 Game::theGame->logMessage("Exit called by human player!\n");
129                                                 exit(EXIT_SUCCESS);
130                                                 break;
131                                         case SDL_MOUSEBUTTONDOWN:
132                                         switch (event.button.button)
133                                         {
134                                                 case SDL_BUTTON_LEFT:
135                                                         SDL_GetMouseState(&x[mouseClick], &y[mouseClick]);
136                                                         x[mouseClick] /= 32; y[mouseClick] /= 32; //Adjust based on graphics grid size
137                                                         if (mouseClick == 0)
138                                                         {
139                                                                 stringstream s("");
140                                                                 s << x[0] << " " << y[0] << " ";
141                                                                 buffer += s.str();
142                                                         }
143                                                         else if (mouseClick == 1)
144                                                         {
145                                                                 int xDist = x[1] - x[0];
146                                                                 int yDist = y[1] - y[0];
147                                                                 if (abs(xDist) > abs(yDist))
148                                                                 {
149                                                                         if (xDist < 0)
150                                                                                 buffer += "LEFT";
151                                                                         else
152                                                                                 buffer += "RIGHT";
153                                                                 }
154                                                                 else if (yDist < 0)
155                                                                         buffer += "UP";
156                                                                 else
157                                                                         buffer += "DOWN";
158                                                         }
159                                                         mouseClick++;
160                                                         break;
161                                         }
162                                         break;
163                                 }
164                         }
165                 }
166                 fprintf(stdout, "Move complete!\n");
167         }
168         else
169         {
170                 buffer.clear();
171                 for (char in = fgetc(stdin); in != '\n'; in = fgetc(stdin))
172                 {
173                         buffer += in;
174                 }
175         }
176         
177         
178
179         return MovementResult::OK;
180         
181 }

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