diff --git a/Makefile b/Makefile index d6f8f1fba0fa1f0c99d825e57b3eabc238fab40a..f1a40f3c1104950808299b45b56d6109d5876d2d 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 bfecec180180d832ed3f5ec8246f0c32e253da0e..6964dc0cc525c1dd28b67d0940fa86606b0a5a99 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 0b74afda81840c46f8ac448e40b297d506711e59..75c2c86f123fa97099742d3c6b406112aa8050a7 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 21af4d3931ab29522d9e40a2d1fc137b3f37b491..d0638f1722f756088a7227d8ee6b356dca5eb4bb 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; + } }