From 835fced2c45984e92b05342b3544b74bc2082e54 Mon Sep 17 00:00:00 2001 From: Francesco Bariatti <francesco.bariatti@insa-rennes.fr> Date: Fri, 27 Nov 2015 15:40:40 +0100 Subject: [PATCH] Fixed some errors with update moves But is it over? --- Makefile | 4 ++-- src/game/morpion.cpp | 20 +++++++++++++++----- src/game/morpion.hpp | 3 ++- src/game/test_morpion.cpp | 14 +++++++++++++- 4 files changed, 32 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index d6f8f1f..f1a40f3 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,9 @@ -CC=g++-4.9 +CC=g++ BIN=bin 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) 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\ bits.cpp test_bits.cpp main.cpp OBJECTS=$(addprefix $(BIN)/, $(SOURCES:.cpp=.o)) diff --git a/src/game/morpion.cpp b/src/game/morpion.cpp index bfecec1..6964dc0 100644 --- a/src/game/morpion.cpp +++ b/src/game/morpion.cpp @@ -81,7 +81,13 @@ namespace game return 0; } + /* Number of moves that you can play */ uint16_t morpion::number_of_moves() const + { + return 9 - state.total_moves; + } + + uint16_t morpion::number_moves_played() const { return state.total_moves; } @@ -116,16 +122,20 @@ namespace game void morpion::update_moves() { 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; - 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) diff --git a/src/game/morpion.hpp b/src/game/morpion.hpp index 0b74afd..75c2c86 100644 --- a/src/game/morpion.hpp +++ b/src/game/morpion.hpp @@ -31,7 +31,8 @@ namespace game bool lost(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) - 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 undo(std::uint16_t m) {} std::string player_to_string(std::uint8_t player) const; //String representation of a player diff --git a/src/game/test_morpion.cpp b/src/game/test_morpion.cpp index 21af4d3..d0638f1 100644 --- a/src/game/test_morpion.cpp +++ b/src/game/test_morpion.cpp @@ -20,9 +20,10 @@ namespace game while(!mor.end_of_game()) { 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; 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++) { uint16_t move = possible_moves & ((uint64_t) 15); //15 = 1111 (a move is on 4 bits) @@ -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 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; + } } -- GitLab