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

Command line interface improved, comments added in code

parent ccaaeb00
No related branches found
No related tags found
No related merge requests found
......@@ -11,8 +11,16 @@ namespace game
{
test_morpion::test_morpion()
{
//play();
playout();
cout << "=== Play tic tac toe ==="<<endl;
cout << "Do you want to play a Player vs (P)layer or a Player vs (C)omputer match? (enter P or C) "<<endl;
string choice;
cin >> choice;
if (choice == "P")
play();
else if (choice == "C")
playout();
else
cout << "Bad choice." << endl;
}
/* Start a complete Player vs Player game */
......@@ -25,19 +33,23 @@ namespace game
cout << mor << endl;
cout << "It's " << mor.player_to_string(mor.current_player()) << " turn." << endl;
map<string, int> movesMap;
cout << "Number of possible moves: " << mor.number_of_moves() << endl;
//cout << "Number of possible moves: " << mor.number_of_moves() << endl;
cout << "Possible moves:";
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;
//cout << "Possible move: " << mor.move_to_string(move) << endl;
cout << " " << mor.move_to_string(move);
movesMap[mor.move_to_string(move)] = i; //In the map: the move as seen by the player and its representation for the computer
possible_moves = possible_moves >> 4;
}
cout << endl << "Choose your move: ";
string move;
cin >> move;
mor.play(movesMap[move]);
}
//Game ended: print the final state of the board
cout << mor << endl;
if (mor.won(0))
cout << mor.player_to_string(0) << " has won" << endl;
......@@ -48,41 +60,50 @@ namespace game
}
/* Start a complete Player vs Computer game */
void test_morpion::playout()
{
morpion mor;
auto mctsPlayer = mcts::make_mcts_two_players(mor, 5000, 0.3, 4); // creation du player a partir du morpion. 5000: ms de reflexion 0.3 fateur d'exploration 4: nb_visit_before_expansion on attend d'etre arrive 4 fois sur un noeud avant de le developper
auto mctsPlayer = mcts::make_mcts_two_players(mor, 1000, 0.3, 4); // creation du player a partir du morpion. 1000: ms de reflexion 0.3 fateur d'exploration 4: nb_visit_before_expansion on attend d'etre arrive 4 fois sur un noeud avant de le developper
cout << "Player vs Computer game!" << endl;
cout << "Who's first? (Player)" << endl;
cout << "Who's first? (P)layer or (C)omputer?" << endl;
string choice;
cin >> choice;
int IAPlayer = 1; //Default: the computer will play second
if (choice == "C") //If however we choosed to let the computer play first
IAPlayer = 0;
while(!mor.end_of_game())
{
cout << mor << endl;
if(mor.current_player() == 1) //It's circle turn (computer is circle)
if(mor.current_player() == IAPlayer) //It's computer's turn
{
mctsPlayer.reset();
mctsPlayer.reset(); //Delete the entire tree: it will construct a new one starting from this state
uint16_t move = mctsPlayer.select_move();
cout << "Computer played move number " << move << endl;
//cout << "Computer played move number " << move << endl;
mor.play(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;
//cout << "Number of possible moves: " << mor.number_of_moves() << endl;
cout << "Possible moves:";
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;
//cout << "Possible move: " << mor.move_to_string(move) << endl;
cout << " " << mor.move_to_string(move);
movesMap[mor.move_to_string(move)] = i; //In the map: the move as seen by the player and
possible_moves = possible_moves >> 4;
}
cout << endl << "Choose your move: ";
string move;
cin >> move;
mor.play(movesMap[move]);
}
}
//Game ended: print the final state of the board
cout << mor << endl;
if (mor.won(0))
cout << mor.player_to_string(0) << " has won" << 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