heurísticas no relatório
[xYpjg3TdSw.git] / Controller.cpp
blob0e27e114acc5335bb0edd9bb4edfe7c33c133430
1 #include "Controller.h"
2 #include "common.h"
3 #include "Window.h"
4 #include "Game.h"
6 #include <cstdio>
7 #include <cstdlib>
9 Controller::Controller(Game *game, bool isAuto)
10 : _game(game), _is_auto(isAuto)
12 rep(i, 8)
13 rep(j, 8) {
14 _is_empty[i][j] = true;
15 _is_highlighted[i][j] = false;
16 _highlight[i][j] = false;
17 _piece[i][j] = 0;
19 _has_highlight = false;
20 _is_playing = false;
22 _game->fill_pieces(&**_is_empty, &**_piece);
25 Controller::~Controller()
29 void Controller::click(int x, int y)
31 printf("Click: (%i, %i)\n", x, y);
33 if(!_is_playing) return;
35 if(_has_highlight) {
36 if(_is_highlighted[x][y]) {
37 if(x == _last_click_x && y == _last_click_y) {
38 clear_highlight();
39 } else {
40 printf("action()\n");
42 // realiza movimento de (_last_click_x, _last_click_y) para (x, y)
43 _game->move(_last_click_x, _last_click_y, x, y);
44 update_all();
46 } else {
47 clear_highlight();
49 } else {
50 clear_highlight();
51 if(can_highlight(x, y)) {
52 _has_highlight = true;
53 _is_highlighted[x][y] = true;
54 _highlight[x][y] = 1;
56 // possibilidades de movimento
57 _game->fill_moves(x, y, &**_is_highlighted);
59 _last_click_x = x;
60 _last_click_y = y;
65 void Controller::set_minimax_depth(int minimaxDepth)
67 printf("minimaxDepth: %i\n", minimaxDepth);
68 _minimax_depth = minimaxDepth;
69 _game->set_minimax_depth(minimaxDepth);
72 void Controller::set_first_move(int firstMove)
74 printf("firstMove: %i\n", firstMove);
75 _game->set_player(firstMove);
78 void Controller::set_computer_color(int computerColor)
80 printf("computerColor: %i\n", computerColor);
81 _game->set_computer_color(computerColor);
84 void Controller::set_window(Window *window)
86 _window = window;
89 #include "MinimaxEnemy.h"
91 #define NUM_POP 10
92 #define NUM_LIVE (NUM_POP / 2)
93 #define MAX_REPS 100
94 #define PROP_MUT 10
95 #define MAX_MOVES 100
97 typedef struct {
98 MinimaxEnemy::HeuType heu[NUM_HEU];
99 MinimaxEnemy *e;
100 int wins;
101 } Being;
103 int compBeing(const void *l, const void *r)
105 return ((Being *)r)->wins - ((Being *)l)->wins;
108 void Controller::play()
110 //int max_n_moves = -1;
111 if(_is_auto) {
112 srand((unsigned) time(NULL));
113 Being pop[NUM_POP];
114 rep(j, NUM_POP) {
115 rep(k, NUM_HEU) {
116 pop[j].heu[k] = rand() / (double) RAND_MAX;
118 pop[j].e = new MinimaxEnemy(0, _minimax_depth);
119 pop[j].e->set_weights(pop[j].heu);
121 rep(r, MAX_REPS) {
122 rep(i, NUM_POP) {
123 pop[i].wins = 0;
126 printf("---------------------------\n");
127 printf("Ger: %5i\n", r);
128 printf("---------------------------\n");
129 rep(i, NUM_POP) {
130 printf("%2i ", pop[i].wins);
131 rep(j, NUM_HEU) {
132 printf("%.5f ", pop[i].heu[j]);
134 printf("\n");
136 printf("---------------------------\n\n");
138 rep(i, NUM_POP) {
139 pop[i].e->set_player(0);
140 rep(j, NUM_POP) {
141 if(i == j) continue;
142 printf("%02i x %02i - ", i, j);
143 pop[j].e->set_player(1);
144 _game->set_players(pop[i].e, pop[j].e);
145 _game->reset();
146 //_game->print();
147 int n_moves = 0;
148 while(_game->think() && n_moves++ < MAX_MOVES) {
149 //_game->print();
150 //if(n_moves > max_n_moves) {
151 //max_n_moves = n_moves;
152 //printf("\n\n======================\nmax_n_moves: %i\n========================\n\n", max_n_moves);
154 if(_game->is_end()) {
155 //_game->print();
156 break;
159 if(_game->get_winner()) {
160 pop[j].wins++;
161 } else {
162 pop[i].wins++;
166 qsort(pop, NUM_POP, sizeof(Being), compBeing);
167 repb(i, NUM_LIVE, NUM_POP) {
168 int j = rand() % NUM_LIVE,
169 k = rand() % NUM_LIVE;
170 while(k == j) k = rand() % NUM_LIVE;
171 rep(l, NUM_HEU) {
172 if(rand() % 2) {
173 pop[i].heu[l] = pop[j].heu[l];
174 } else {
175 pop[i].heu[l] = pop[k].heu[l];
177 if(rand() % PROP_MUT == 0) {
178 pop[i].heu[l] = rand() / (double) RAND_MAX;
181 pop[i].e->set_weights(pop[i].heu);
183 printf("\n\n\n+++++++++++++++++++++++++++\n");
184 printf("%i %i %i %i", NUM_POP, NUM_LIVE, PROP_MUT, MAX_REPS);
185 printf("\n+++++++++++++++++++++++++++\n%2i ", pop[0].wins);
186 rep(i, NUM_HEU) {
187 printf("%.5f ", pop[0].heu[i]);
189 printf("\n+++++++++++++++++++++++++++\n\n");
190 //printf("endgame winner: %i\n", _game->get_winner());
192 } else {
193 printf("play()\n");
194 _is_playing = true;
195 _window->disable_all();
196 _window->enable("stop");
197 update_all();
199 //printf("\n\n======================\nmax_n_moves: %i\n========================\n\n", max_n_moves);
202 void Controller::stop()
204 //TODO
205 printf("stop()\n");
206 _is_playing = false;
207 _window->enable_all();
208 _window->disable("stop");
209 update_all();
212 void Controller::undo()
214 _game->undo();
215 update_all();
218 void Controller::redo()
220 _game->redo();
221 update_all();
224 void Controller::load(FILE *in)
226 _game->load(in);
227 update_all();
230 void Controller::after_display()
232 if(_is_playing && _game->think()) {
233 update_all();
237 bool Controller::is_empty(int x, int y)
239 return _is_empty[x][y];
242 int Controller::piece(int x, int y)
244 return _piece[x][y];
247 bool Controller::is_highlighted(int x, int y)
249 return _is_highlighted[x][y];
252 int Controller::highlight(int x, int y)
254 return _highlight[x][y];
257 void Controller::clear_highlight()
259 rep(i, 8)
260 rep(j, 8) {
261 _is_highlighted[i][j] = false;
262 _highlight[i][j] = false;
264 _has_highlight = false;
265 _last_click_x = _last_click_y = -1;
268 bool Controller::can_highlight(int x, int y)
270 return _game->can_move(x, y);
273 void Controller::update_all()
275 int fromX, fromY, toX, toY;
276 _game->fill_pieces(&**_is_empty, &**_piece);
277 clear_highlight();
278 _game->get_last_move(fromX, fromY, toX, toY);
279 if(fromX != -1) {
280 _is_highlighted[fromX][fromY] = _is_highlighted[toX][toY] = true;
281 _highlight[fromX][fromY] = _highlight[toX][toY] = 1;
283 if(_game->can_undo()) _window->enable("undo");
284 else _window->disable("undo");
285 if(_game->can_redo()) _window->enable("redo");
286 else _window->disable("redo");
287 if(_game->is_end() && _is_playing) {
288 stop();
289 //fflush(stdout);
291 _window->display();
292 //printf("digite\n");
293 //fflush(stdout);
294 //scanf("%*c");