Skip to content
Snippets Groups Projects
Commit 835fced2 authored by Bariatti Francesco's avatar Bariatti Francesco
Browse files

Fixed some errors with update moves

But is it over?
parent 19badb90
No related branches found
No related tags found
No related merge requests found
CC=g++-4.9 CC=g++
BIN=bin BIN=bin
INCLUDE=-I src/game -I src/util -I src/monte_carlo -I src/mcts -I src/minmax INCLUDE=-I src/game -I src/util -I src/monte_carlo -I src/mcts -I src/minmax
CFLAGS=-g -O3 -ffast-math -fopenmp -c -Wall -std=c++11 $(INCLUDE) CFLAGS=-g -O3 -ffast-math -fopenmp -c -Wall -std=c++11 $(INCLUDE)
LDFLAGS=-fopenmp -std=c++11 #-lprofiler -Wl,-no_pie LDFLAGS=-fopenmp -std=c++11 #-lprofiler -Wl,-no_pie
SOURCES=omp_util.cpp fast_log.cpp display_node.cpp morpion.cpp test_morpion.cpp monte_carlo.cpp test_monte_carlo.cpp test_fast_log.cpp\ SOURCES=omp_util.cpp fast_log.cpp display_node.cpp morpion.cpp test_morpion.cpp connect4.cpp test_connect4.cpp monte_carlo.cpp test_monte_carlo.cpp test_fast_log.cpp\
statistics.cpp node.cpp allocator.cpp test_allocator.cpp openings.cpp mcts_two_players.cpp test_mcts_two_players.cpp test_minmax.cpp\ statistics.cpp node.cpp allocator.cpp test_allocator.cpp openings.cpp mcts_two_players.cpp test_mcts_two_players.cpp test_minmax.cpp\
bits.cpp test_bits.cpp main.cpp bits.cpp test_bits.cpp main.cpp
OBJECTS=$(addprefix $(BIN)/, $(SOURCES:.cpp=.o)) OBJECTS=$(addprefix $(BIN)/, $(SOURCES:.cpp=.o))
......
...@@ -81,7 +81,13 @@ namespace game ...@@ -81,7 +81,13 @@ namespace game
return 0; return 0;
} }
/* Number of moves that you can play */
uint16_t morpion::number_of_moves() const uint16_t morpion::number_of_moves() const
{
return 9 - state.total_moves;
}
uint16_t morpion::number_moves_played() const
{ {
return state.total_moves; return state.total_moves;
} }
...@@ -116,16 +122,20 @@ namespace game ...@@ -116,16 +122,20 @@ namespace game
void morpion::update_moves() void morpion::update_moves()
{ {
uint16_t free_bitboard = ~(state.cross_bitboard | state.circle_bitboard); uint16_t free_bitboard = ~(state.cross_bitboard | state.circle_bitboard);
free_bitboard &= ALL_ONES; //When we complements, all unused bits in uint_16 are complemented to. ALL_ONES is a mask in which we have ones in the position used by the bitboard
cout << "Free bitboard: " << free_bitboard << endl;
state.possible_moves = 0; state.possible_moves = 0;
for(int i = 0; i <=8; i++) uint16_t mask = 256; //256 = 100 000 000
for(int i = 8; i >=0; i--)
{ {
if(free_bitboard & 1) if(free_bitboard & mask)
{ {
state.possible_moves += i; state.possible_moves = state.possible_moves << 4;
state.possible_moves = state.possible_moves << 4; state.possible_moves |= i;
} }
free_bitboard = free_bitboard >> 1; mask = mask >> 1;
} }
cout << "Possible moves: " << state.possible_moves << endl;
} }
void morpion::play(uint16_t m) void morpion::play(uint16_t m)
......
...@@ -31,7 +31,8 @@ namespace game ...@@ -31,7 +31,8 @@ namespace game
bool lost(std::uint8_t player) const; bool lost(std::uint8_t player) const;
bool draw(std::uint8_t player) const; bool draw(std::uint8_t player) const;
uint8_t current_player() const; //The player that has to play next (at the beginning, the first player) uint8_t current_player() const; //The player that has to play next (at the beginning, the first player)
std::uint16_t number_of_moves() const; //Moves played until now std::uint16_t number_of_moves() const; //Number of moves that you can play
std::uint16_t number_moves_played() const; //Number of moves played until now
void play(std::uint16_t m); //Play a move (updates the state) void play(std::uint16_t m); //Play a move (updates the state)
void undo(std::uint16_t m) {} void undo(std::uint16_t m) {}
std::string player_to_string(std::uint8_t player) const; //String representation of a player std::string player_to_string(std::uint8_t player) const; //String representation of a player
......
...@@ -20,9 +20,10 @@ namespace game ...@@ -20,9 +20,10 @@ namespace game
while(!mor.end_of_game()) while(!mor.end_of_game())
{ {
cout << mor << endl; cout << mor << endl;
cout << "It's" << mor.player_to_string(mor.current_player()) << "turn." << endl; cout << "It's " << mor.player_to_string(mor.current_player()) << " turn." << endl;
map<string, int> m; map<string, int> m;
uint64_t possible_moves = mor.get_state().possible_moves; uint64_t possible_moves = mor.get_state().possible_moves;
cout << "Number of possible moves: " << mor.number_of_moves() << endl;
for (int i = 0; i < mor.number_of_moves(); i++) for (int i = 0; i < mor.number_of_moves(); i++)
{ {
uint16_t move = possible_moves & ((uint64_t) 15); //15 = 1111 (a move is on 4 bits) uint16_t move = possible_moves & ((uint64_t) 15); //15 = 1111 (a move is on 4 bits)
...@@ -30,7 +31,18 @@ namespace game ...@@ -30,7 +31,18 @@ namespace game
m[mor.move_to_string(move)] = i; //In the map: the move as seen by the player and its index in possible moves m[mor.move_to_string(move)] = i; //In the map: the move as seen by the player and its index in possible moves
possible_moves = possible_moves >> 4; possible_moves = possible_moves >> 4;
} }
string move;
cin >> move;
mor.play(m[move]);
} }
cout << mor << endl;
if (mor.won(0))
cout << mor.player_to_string(0) << "has won" << endl;
else if (mor.won(1))
cout << mor.player_to_string(1) << "has won" << endl;
else
cout << "Draw! too bad :(" << endl;
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment