heuristicas começando a implementar ...
[xYpjg3TdSw.git] / Controller.cpp
blob8891413b465509f454990517341e4d89944fabd8
1 #include "Controller.h"
2 #include "common.h"
3 #include "Window.h"
4 #include "Game.h"
6 #include <cstdio>
8 Controller::Controller(Game *game)
9 : _game(game)
11 rep(i, 8)
12 rep(j, 8) {
13 _is_empty[i][j] = true;
14 _is_highlighted[i][j] = false;
15 _highlight[i][j] = false;
16 _piece[i][j] = 0;
18 _has_highlight = false;
19 _is_playing = false;
21 _game->fill_pieces(&**_is_empty, &**_piece);
24 Controller::~Controller()
28 void Controller::click(int x, int y)
30 printf("Click: (%i, %i)\n", x, y);
32 if(!_is_playing) return;
34 if(_has_highlight) {
35 if(_is_highlighted[x][y]) {
36 if(x == _last_click_x && y == _last_click_y) {
37 clear_highlight();
38 } else {
39 printf("action()\n");
41 // realiza movimento de (_last_click_x, _last_click_y) para (x, y)
42 _game->move(_last_click_x, _last_click_y, x, y);
43 update_all();
45 } else {
46 clear_highlight();
48 } else {
49 clear_highlight();
50 if(can_highlight(x, y)) {
51 _has_highlight = true;
52 _is_highlighted[x][y] = true;
53 _highlight[x][y] = 1;
55 // possibilidades de movimento
56 _game->fill_moves(x, y, &**_is_highlighted);
58 _last_click_x = x;
59 _last_click_y = y;
64 void Controller::set_minimax_depth(int minimaxDepth)
66 //TODO
67 printf("minimaxDepth: %i\n", minimaxDepth);
68 _game->set_minimax_depth(minimaxDepth);
71 void Controller::set_first_move(int firstMove)
73 printf("firstMove: %i\n", firstMove);
74 _game->set_player(firstMove);
77 void Controller::set_computer_color(int computerColor)
79 printf("computerColor: %i\n", computerColor);
80 _game->set_computer_color(computerColor);
83 void Controller::set_window(Window *window)
85 _window = window;
88 void Controller::play()
90 //TODO
91 printf("play()\n");
92 _is_playing = true;
93 _window->disable_all();
94 _window->enable("stop");
95 update_all();
98 void Controller::stop()
100 //TODO
101 printf("stop()\n");
102 _is_playing = false;
103 _window->enable_all();
104 _window->disable("stop");
105 update_all();
108 void Controller::undo()
110 _game->undo();
111 update_all();
114 void Controller::redo()
116 _game->redo();
117 update_all();
120 void Controller::load(FILE *in)
122 _game->load(in);
123 update_all();
126 void Controller::after_display()
128 if(_is_playing && _game->think()) {
129 update_all();
133 bool Controller::is_empty(int x, int y)
135 return _is_empty[x][y];
138 int Controller::piece(int x, int y)
140 return _piece[x][y];
143 bool Controller::is_highlighted(int x, int y)
145 return _is_highlighted[x][y];
148 int Controller::highlight(int x, int y)
150 return _highlight[x][y];
153 void Controller::clear_highlight()
155 rep(i, 8)
156 rep(j, 8) {
157 _is_highlighted[i][j] = false;
158 _highlight[i][j] = false;
160 _has_highlight = false;
161 _last_click_x = _last_click_y = -1;
164 bool Controller::can_highlight(int x, int y)
166 return _game->can_move(x, y);
169 void Controller::update_all()
171 int fromX, fromY, toX, toY;
172 _game->fill_pieces(&**_is_empty, &**_piece);
173 clear_highlight();
174 _game->get_last_move(fromX, fromY, toX, toY);
175 if(fromX != -1) {
176 _is_highlighted[fromX][fromY] = _is_highlighted[toX][toY] = true;
177 _highlight[fromX][fromY] = _highlight[toX][toY] = 1;
179 if(_game->can_undo()) _window->enable("undo");
180 else _window->disable("undo");
181 if(_game->can_redo()) _window->enable("redo");
182 else _window->disable("redo");
183 if(_game->is_end() && _is_playing) {
184 stop();
185 //fflush(stdout);
187 _window->display();
188 //printf("digite\n");
189 //fflush(stdout);
190 //scanf("%*c");