heuristicas começando a implementar ...
[xYpjg3TdSw.git] / Game.cpp
blob537d756915edd2146be48de51646c9025a2961df
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);
31 time_t currtime;
32 char charTime[100] = {0};
33 time(&currtime);
34 strftime(charTime,sizeof(charTime)-1,"%c",localtime(&currtime));
35 fprintf(_out, "time: %s\n", charTime);
38 Game::~Game()
40 delete _board;
41 delete _move_history;
42 fclose(_out);
45 void Game::move(int fromX, int fromY, int toX, int toY)
47 _board->move(_player, Board::Move(INDEX(fromX, fromY), INDEX(toX, toY)));
48 _player = !_player;
49 if(can_redo())
50 fprintf(_out, "back: %i\n", _move_count - _current_move + 1);
51 _move_history[_move_count = _current_move++] = MoveHistoryEntry(_board, fromX, fromY, toX, toY);
52 fprintf(_out, "%i: %i %i %i %i\n", _move_count, fromX, fromY, toX, toY);
55 bool Game::think()
57 //TODO
58 if(_computer_color & (1 << _player)) {
59 printf("Computer Turn\n");
60 //fflush(stdout);
61 int fromX, fromY, toX, toY;
62 _enemy[_player]->move(_board, fromX, fromY, toX, toY);
63 move(fromX, fromY, toX, toY);
64 return true;
65 } else {
66 printf("Player Turn\n");
67 return false;
71 void Game::fill_moves(int x, int y, bool *to)
73 Board::MoveList moveList;
74 _board->moves(_player, x, y, moveList);
76 reps(i, moveList) {
77 to[(int) moveList[i].second] = 1;
81 bool Game::can_move(int x, int y)
83 return _board->get(_player, x, y);
86 void Game::fill_pieces(bool *isEmpty, char *piece)
88 rep(i, 8)
89 rep(j, 8)
90 isEmpty[INDEX(i, j)] = !(_board->get(0, i, j) || _board->get(1, i, j));
92 rep(i, 8)
93 rep(j, 8)
94 if(_board->get(1, i, j))
95 piece[INDEX(i, j)] = 1;
96 else piece[INDEX(i, j)] = 0;
99 void Game::set_player(bool player)
101 _player = player;
102 fprintf(_out, "player: %i\n", _player ? 1 : 0);
105 void Game::undo()
107 if(can_undo()) {
108 _enemy[0]->undo(_board);
109 _enemy[1]->undo(_board);
110 set(_move_history[--_current_move - 1]);
111 _player = !_player;
115 void Game::redo()
117 if(can_redo()) {
118 set(_move_history[_current_move++]);
119 _player = !_player;
123 void Game::set(MoveHistoryEntry entry) {
124 delete _board;
125 _board = entry.board()->copy();
128 void Game::get_last_move(int& fromX, int &fromY, int& toX, int& toY)
130 MoveHistoryEntry e = _move_history[_current_move - 1];
131 fromX = e.from_x();
132 fromY = e.from_y();
133 toX = e.to_x();
134 toY = e.to_y();
137 void Game::load(FILE *in)
139 int back, eof, player, fromX, fromY, toX, toY;
140 while(!feof(in)) {
141 if(0 != (eof = fscanf(in, "%*i: %i %i %i %i", &fromX, &fromY, &toX, &toY))) {
142 if(eof == EOF) break;
143 printf("move: %i %i %i %i\n", fromX, fromY, toX, toY);
144 move(fromX, fromY, toX, toY);
145 } else if(0 != (eof = fscanf(in, "time: %*[^\n]"))) {
146 if(eof == EOF) break;
147 printf("time\n");
148 } else if(0 != (eof = fscanf(in, "back: %i", &back))) {
149 if(eof == EOF) break;
150 printf("back: %i\n", back);
151 for(int i = 0; i < back; i++)
152 undo();
153 } else if(0 != (eof = fscanf(in, "player: %i", &player))) {
154 if(eof == EOF) break;
155 printf("player: %i\n", player);
156 set_player(player);
161 void Game::set_minimax_depth(int minimaxDepth)
163 _enemy[0]->set_minimax_depth(minimaxDepth);
164 _enemy[1]->set_minimax_depth(minimaxDepth);
167 bool Game::is_end()
169 bool res = _board->is_end();
170 if(res) ;//fflush(_out);
171 return res;