From c7b2a2ce373879f0924462b65a05e53c3ec2b7ca Mon Sep 17 00:00:00 2001 From: Francesco Bariatti <francesco.bariatti@insa-rennes.fr> Date: Tue, 8 Dec 2015 14:57:39 +0100 Subject: [PATCH] Completed player vs player test --- src/game/morpion.cpp | 10 ++++--- src/game/morpion.hpp | 2 +- src/game/test_morpion.cpp | 63 +++++++++++++++++++++++++++++++++------ src/game/test_morpion.hpp | 2 +- 4 files changed, 62 insertions(+), 15 deletions(-) diff --git a/src/game/morpion.cpp b/src/game/morpion.cpp index 6964dc0..a2dd622 100644 --- a/src/game/morpion.cpp +++ b/src/game/morpion.cpp @@ -122,8 +122,8 @@ 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; + //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 & ALL_ONES) << endl; state.possible_moves = 0; uint16_t mask = 256; //256 = 100 000 000 for(int i = 8; i >=0; i--) @@ -135,11 +135,13 @@ namespace game } mask = mask >> 1; } - cout << "Possible moves: " << state.possible_moves << endl; + //cout << "Possible moves: " << state.possible_moves << endl; } - + + //Play the move m. m is the position on the board of where you want to play void morpion::play(uint16_t m) { + //cout << "You choose move " << m << endl; if (current_player() == CROSS) state.cross_bitboard |= (((uint16_t) 1) << m); else diff --git a/src/game/morpion.hpp b/src/game/morpion.hpp index 75c2c86..4ab8594 100644 --- a/src/game/morpion.hpp +++ b/src/game/morpion.hpp @@ -33,7 +33,7 @@ namespace game 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; //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 the move m (updates the state). m is the position on the board of where you want to play void undo(std::uint16_t m) {} std::string player_to_string(std::uint8_t player) const; //String representation of a player std::string move_to_string(std::uint16_t m) const; //String representation of a move (for example, A1) diff --git a/src/game/test_morpion.cpp b/src/game/test_morpion.cpp index d0638f1..3679671 100644 --- a/src/game/test_morpion.cpp +++ b/src/game/test_morpion.cpp @@ -1,5 +1,6 @@ -#include "morpion.hpp" #include "test_morpion.hpp" +#include "mcts_two_players.hpp" +#include "morpion.hpp" #include <iostream> #include <map> @@ -10,9 +11,11 @@ namespace game { test_morpion::test_morpion() { - play(); + //play(); + playout(); } + /* Start a complete Player vs Player game */ void test_morpion::play() { morpion mor; @@ -21,28 +24,70 @@ namespace game { cout << mor << 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; + map<string, int> movesMap; cout << "Number of possible moves: " << mor.number_of_moves() << endl; - for (int i = 0; i < mor.number_of_moves(); i++) + uint64_t possible_moves = mor.get_state().possible_moves; + 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) cout << "Possible move: " << mor.move_to_string(move) << endl; - m[mor.move_to_string(move)] = i; //In the map: the move as seen by the player and its index in possible moves + movesMap[mor.move_to_string(move)] = move; //In the map: the move as seen by the player and its representation for the computer possible_moves = possible_moves >> 4; } string move; cin >> move; - mor.play(m[move]); + mor.play(movesMap[move]); } cout << mor << endl; if (mor.won(0)) - cout << mor.player_to_string(0) << "has won" << endl; + cout << mor.player_to_string(0) << " has won" << endl; else if (mor.won(1)) - cout << mor.player_to_string(1) << "has won" << endl; + cout << mor.player_to_string(1) << " has won" << endl; else cout << "Draw! too bad :(" << endl; } + + void test_morpion::playout() + { + morpion mor; + auto mctsPlayer = mcts::make_mcts_two_players(mor, 5000, 0.3, 4); + cout << "Player vs Computer game!" << endl; + cout << "Who's first? (Player)" << endl; + while(!mor.end_of_game()) + { + cout << mor << endl; + if(mor.current_player() == 1) //It's circle turn (computer is circle) + { + uint16_t move = mctsPlayer.select_move(); + cout << "Computer wants to play " << mor.move_to_string(move); + } + else //Human turn to play + { + cout << "It's your time to shine!" << endl; + map<string, int> movesMap; + cout << "Number of possible moves: " << mor.number_of_moves() << endl; + uint64_t possible_moves = mor.get_state().possible_moves; + 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) + cout << "Possible move: " << mor.move_to_string(move) << endl; + movesMap[mor.move_to_string(move)] = move; //In the map: the move as seen by the player and its representation for the computer + possible_moves = possible_moves >> 4; + } + string move; + cin >> move; + mor.play(movesMap[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; + } } diff --git a/src/game/test_morpion.hpp b/src/game/test_morpion.hpp index f1663ab..0396698 100644 --- a/src/game/test_morpion.hpp +++ b/src/game/test_morpion.hpp @@ -5,7 +5,7 @@ namespace game { class test_morpion { - //void playout(); + void playout(); void play(); public: test_morpion(); -- GitLab