heurísticas no relatório
[xYpjg3TdSw.git] / Game.cpp
blob409b5db67ddb0695701ba4eb189cfff652a6775c
1 #include "Game.h"
2 #include "Board.h"
3 #include "MoveHistoryEntry.h"
4 #include "RandomEnemy.h"
5 #include "MinimaxEnemy.h"
7 #include <cstdio>
8 #include <ctime>
9 #include <cassert>
11 /* nome do arquivo de log */
12 #define LOG_FILE "log.txt"
13 /* número máximo de movimentos de uma partida */
14 #define MAX_MOVES 1000
16 Game::Game()
18 assert(sizeof(Board::ImpComp) == 8);
19 _board = new Board();
20 _board->initial_position();
21 _player = 0;
22 _move_count = _current_move = 0;
23 _computer_color = 0;
24 _move_history = new MoveHistoryEntry[MAX_MOVES];
25 _move_history[_move_count = _current_move++] = MoveHistoryEntry(_board, -1, -1, -1, -1);
26 _out = fopen(LOG_FILE, "a");
27 assert(_out);
28 _enemy[0] = new MinimaxEnemy(0, _minimax_depth);
29 _enemy[1] = new MinimaxEnemy(1, _minimax_depth);
30 //_enemy[1] = new RandomEnemy(1, _minimax_depth);
32 time_t currtime;
33 char charTime[100] = {0};
34 time(&currtime);
35 strftime(charTime,sizeof(charTime)-1,"%c",localtime(&currtime));
36 fprintf(_out, "time: %s\n", charTime);
39 Game::~Game()
41 delete _board;
42 delete _move_history;
43 fclose(_out);
46 void Game::move(int fromX, int fromY, int toX, int toY)
48 _board->move(_player, Board::Move(INDEX(fromX, fromY), INDEX(toX, toY)));
49 _player = !_player;
50 //if(can_redo())
51 //fprintf(_out, "back: %i\n", _move_count - _current_move + 1);
52 //_move_history[_move_count = _current_move++] = MoveHistoryEntry(_board, fromX, fromY, toX, toY);
53 //fprintf(_out, "%i: %i %i %i %i\n", _move_count, fromX, fromY, toX, toY);
56 bool Game::think()
58 //TODO
59 if(_computer_color & (1 << _player)) {
60 //printf("Computer Turn\n");
61 //fflush(stdout);
62 int fromX, fromY, toX, toY;
63 _enemy[_player]->move(_board, fromX, fromY, toX, toY);
64 move(fromX, fromY, toX, toY);
65 return true;
66 } else {
67 //printf("Player Turn\n");
68 return false;
72 void Game::fill_moves(int x, int y, bool *to)
74 Board::MoveList moveList;
75 _board->moves(_player, x, y, moveList);
77 reps(i, moveList) {
78 to[(int) moveList[i].second] = 1;
82 bool Game::can_move(int x, int y)
84 return _board->get(_player, x, y);
87 void Game::fill_pieces(bool *isEmpty, char *piece)
89 rep(i, 8)
90 rep(j, 8)
91 isEmpty[INDEX(i, j)] = !(_board->get(0, i, j) || _board->get(1, i, j));
93 rep(i, 8)
94 rep(j, 8)
95 if(_board->get(1, i, j))
96 piece[INDEX(i, j)] = 1;
97 else piece[INDEX(i, j)] = 0;
100 void Game::set_player(bool player)
102 _player = player;
103 fprintf(_out, "player: %i\n", _player ? 1 : 0);
106 void Game::undo()
108 if(can_undo()) {
109 _enemy[0]->undo(_board);
110 _enemy[1]->undo(_board);
111 set(_move_history[--_current_move - 1]);
112 _player = !_player;
116 void Game::redo()
118 if(can_redo()) {
119 set(_move_history[_current_move++]);
120 _player = !_player;
124 void Game::set(MoveHistoryEntry entry) {
125 delete _board;
126 _board = entry.board()->copy();
129 void Game::get_last_move(int& fromX, int &fromY, int& toX, int& toY)
131 MoveHistoryEntry e = _move_history[_current_move - 1];
132 fromX = e.from_x();
133 fromY = e.from_y();
134 toX = e.to_x();
135 toY = e.to_y();
138 void Game::load(FILE *in)
140 int back, eof, player, fromX, fromY, toX, toY;
141 while(!feof(in)) {
142 if(0 != (eof = fscanf(in, "%*i: %i %i %i %i", &fromX, &fromY, &toX, &toY))) {
143 if(eof == EOF) break;
144 printf("move: %i %i %i %i\n", fromX, fromY, toX, toY);
145 move(fromX, fromY, toX, toY);
146 } else if(0 != (eof = fscanf(in, "time: %*[^\n]"))) {
147 if(eof == EOF) break;
148 printf("time\n");
149 } else if(0 != (eof = fscanf(in, "back: %i", &back))) {
150 if(eof == EOF) break;
151 printf("back: %i\n", back);
152 for(int i = 0; i < back; i++)
153 undo();
154 } else if(0 != (eof = fscanf(in, "player: %i", &player))) {
155 if(eof == EOF) break;
156 printf("player: %i\n", player);
157 set_player(player);
162 void Game::set_minimax_depth(int minimaxDepth)
164 _enemy[0]->set_minimax_depth(minimaxDepth);
165 _enemy[1]->set_minimax_depth(minimaxDepth);
168 bool Game::is_end()
170 bool res = _board->is_end();
171 //if(res) ;//fflush(_out);
172 return res;
175 void Game::set_players(Enemy *a, Enemy *b)
177 _enemy[0] = a;
178 _enemy[1] = b;
181 void Game::reset()
183 //while(can_undo())
184 //undo();
185 _board->initial_position();
186 _player = 0;
189 void Game::print() {
190 printf("\n\n");
191 rep(i, 8) {
192 rep(j, 8) {
193 if(_board->get(0, i, j)) printf("x");
194 else if(_board->get(1, i, j)) printf("o");
195 else printf("_");
197 printf("\n");
199 printf("\n");