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

Completed player vs player test

parent 835fced2
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
......@@ -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)
......
#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;
}
}
......@@ -5,7 +5,7 @@ namespace game
{
class test_morpion
{
//void playout();
void playout();
void play();
public:
test_morpion();
......
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