1 #include "Controller.h"
9 Controller::Controller(Game
*game
, bool isAuto
)
10 : _game(game
), _is_auto(isAuto
)
14 _is_empty
[i
][j
] = true;
15 _is_highlighted
[i
][j
] = false;
16 _highlight
[i
][j
] = false;
19 _has_highlight
= 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;
36 if(_is_highlighted
[x
][y
]) {
37 if(x
== _last_click_x
&& y
== _last_click_y
) {
42 // realiza movimento de (_last_click_x, _last_click_y) para (x, y)
43 _game
->move(_last_click_x
, _last_click_y
, x
, y
);
51 if(can_highlight(x
, y
)) {
52 _has_highlight
= true;
53 _is_highlighted
[x
][y
] = true;
56 // possibilidades de movimento
57 _game
->fill_moves(x
, y
, &**_is_highlighted
);
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
)
89 #include "MinimaxEnemy.h"
92 #define NUM_LIVE (NUM_POP / 2)
98 MinimaxEnemy::HeuType heu
[NUM_HEU
];
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;
112 srand((unsigned) time(NULL
));
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
);
126 printf("---------------------------\n");
127 printf("Ger: %5i\n", r
);
128 printf("---------------------------\n");
130 printf("%2i ", pop
[i
].wins
);
132 printf("%.5f ", pop
[i
].heu
[j
]);
136 printf("---------------------------\n\n");
139 pop
[i
].e
->set_player(0);
142 printf("%02i x %02i - ", i
, j
);
143 pop
[j
].e
->set_player(1);
144 _game
->set_players(pop
[i
].e
, pop
[j
].e
);
148 while(_game
->think() && n_moves
++ < MAX_MOVES
) {
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()) {
159 if(_game
->get_winner()) {
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
;
173 pop
[i
].heu
[l
] = pop
[j
].heu
[l
];
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
);
187 printf("%.5f ", pop
[0].heu
[i
]);
189 printf("\n+++++++++++++++++++++++++++\n\n");
190 //printf("endgame winner: %i\n", _game->get_winner());
195 _window
->disable_all();
196 _window
->enable("stop");
199 //printf("\n\n======================\nmax_n_moves: %i\n========================\n\n", max_n_moves);
202 void Controller::stop()
207 _window
->enable_all();
208 _window
->disable("stop");
212 void Controller::undo()
218 void Controller::redo()
224 void Controller::load(FILE *in
)
230 void Controller::after_display()
232 if(_is_playing
&& _game
->think()) {
237 bool Controller::is_empty(int x
, int y
)
239 return _is_empty
[x
][y
];
242 int Controller::piece(int x
, int 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()
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
);
278 _game
->get_last_move(fromX
, fromY
, toX
, toY
);
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
) {
292 //printf("digite\n");