relatório limpo. falta começar apresentação.
[xYpjg3TdSw.git] / Game.cpp
bloba103b76d76a1d51d10f16781d4348245df334279
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 MinimaxEnemy *t = new MinimaxEnemy(0, _minimax_depth);
29 t->set_weights(((HeuType [NUM_HEU]) {0., 0.82723, 0., 0.97915, 0., 0.96032, 0.96950, 0.}));
30 _enemy[0] = t;
31 t = new MinimaxEnemy(1, _minimax_depth);
32 t->set_weights(((HeuType [NUM_HEU]) {0., 0.82723, 0., 0.97915, 0., 0.96032, 0.96950, 0.}));
33 _enemy[1] = t;
34 //_enemy[1] = new MinimaxEnemy(1, _minimax_depth);
35 //_enemy[1] = new RandomEnemy(1, _minimax_depth);
37 time_t currtime;
38 char charTime[100] = {0};
39 time(&currtime);
40 strftime(charTime,sizeof(charTime)-1,"%c",localtime(&currtime));
41 fprintf(_out, "time: %s\n", charTime);
44 Game::~Game()
46 delete _board;
47 delete _move_history;
48 fclose(_out);
51 void Game::move(int fromX, int fromY, int toX, int toY)
53 _board->move(_player, Board::Move(INDEX(fromX, fromY), INDEX(toX, toY)));
54 _player = !_player;
55 //if(can_redo())
56 //fprintf(_out, "back: %i\n", _move_count - _current_move + 1);
57 _move_history[_move_count = _current_move++] = MoveHistoryEntry(_board, fromX, fromY, toX, toY);
58 //fprintf(_out, "%i: %i %i %i %i\n", _move_count, fromX, fromY, toX, toY);
61 bool Game::think()
63 //TODO
64 if(_computer_color & (1 << _player)) {
65 //printf("Computer Turn\n");
66 //fflush(stdout);
67 int fromX, fromY, toX, toY;
68 _enemy[_player]->move(_board, fromX, fromY, toX, toY);
69 move(fromX, fromY, toX, toY);
70 return true;
71 } else {
72 //printf("Player Turn\n");
73 return false;
77 void Game::fill_moves(int x, int y, bool *to)
79 Board::MoveList moveList;
80 _board->moves(_player, x, y, moveList);
82 reps(i, moveList) {
83 to[(int) moveList[i].second] = 1;
87 bool Game::can_move(int x, int y)
89 return _board->get(_player, x, y);
92 void Game::fill_pieces(bool *isEmpty, char *piece)
94 rep(i, 8)
95 rep(j, 8)
96 isEmpty[INDEX(i, j)] = !(_board->get(0, i, j) || _board->get(1, i, j));
98 rep(i, 8)
99 rep(j, 8)
100 if(_board->get(1, i, j))
101 piece[INDEX(i, j)] = 1;
102 else piece[INDEX(i, j)] = 0;
105 void Game::set_player(bool player)
107 _player = player;
108 fprintf(_out, "player: %i\n", _player ? 1 : 0);
111 void Game::undo()
113 if(can_undo()) {
114 _enemy[0]->undo(_board);
115 _enemy[1]->undo(_board);
116 set(_move_history[--_current_move - 1]);
117 _player = !_player;
118 if(!can_undo()) {
119 printf("!!!!!!!!!!!!!!!\n");
120 _board->initial_position();
125 void Game::redo()
127 if(can_redo()) {
128 set(_move_history[_current_move++]);
129 _player = !_player;
133 void Game::set(MoveHistoryEntry entry) {
134 delete _board;
135 _board = entry.board()->copy();
138 void Game::get_last_move(int& fromX, int &fromY, int& toX, int& toY)
140 MoveHistoryEntry e = _move_history[_current_move - 1];
141 fromX = e.from_x();
142 fromY = e.from_y();
143 toX = e.to_x();
144 toY = e.to_y();
147 void Game::load(FILE *in)
149 int back, eof, player, fromX, fromY, toX, toY;
150 while(!feof(in)) {
151 if(0 != (eof = fscanf(in, "%*i: %i %i %i %i", &fromX, &fromY, &toX, &toY))) {
152 if(eof == EOF) break;
153 printf("move: %i %i %i %i\n", fromX, fromY, toX, toY);
154 move(fromX, fromY, toX, toY);
155 } else if(0 != (eof = fscanf(in, "time: %*[^\n]"))) {
156 if(eof == EOF) break;
157 printf("time\n");
158 } else if(0 != (eof = fscanf(in, "back: %i", &back))) {
159 if(eof == EOF) break;
160 printf("back: %i\n", back);
161 for(int i = 0; i < back; i++)
162 undo();
163 } else if(0 != (eof = fscanf(in, "player: %i", &player))) {
164 if(eof == EOF) break;
165 printf("player: %i\n", player);
166 set_player(player);
171 void Game::set_minimax_depth(int minimaxDepth)
173 _enemy[0]->set_minimax_depth(minimaxDepth);
174 _enemy[1]->set_minimax_depth(minimaxDepth);
177 bool Game::is_end()
179 bool res = _board->is_end();
180 //if(res) ;//fflush(_out);
181 return res;
184 void Game::set_players(Enemy *a, Enemy *b)
186 _enemy[0] = a;
187 _enemy[1] = b;
190 void Game::reset()
192 //while(can_undo())
193 //undo();
194 _board->initial_position();
195 _player = 0;
198 void Game::print() {
199 printf("\n\n");
200 rep(i, 8) {
201 rep(j, 8) {
202 if(_board->get(0, i, j)) printf("x");
203 else if(_board->get(1, i, j)) printf("o");
204 else printf("_");
206 printf("\n");
208 printf("\n");