-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
72 lines (63 loc) · 2.79 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "Board.h"
#include "CommandLinePlayer.h"
#include "OptimalSolutionPlayer.h"
#include "RandomPlayer.h"
#include "RemainingTiles.h"
#include "Tile.h"
#include <iostream>
#include <memory>
#include <algorithm>
using namespace OptiTake;
int main()
{
std::cout << "This is the game Take it Easy!\n";
RemainingTiles remainingTiles{};
int numberOfPlayers;
std::cout << "Enter the number of players: ";
std::cin >> numberOfPlayers;
std::vector<std::pair<std::unique_ptr<Player>, Board>> playersWithBoard;
for (int i = 0; i < numberOfPlayers; ++i){
std::string playerName;
std::cout << "Enter a name: ";
std::cin >> playerName;
playersWithBoard.emplace_back(std::make_unique<CommandLinePlayer>(std::move(playerName)), Board{});
}
auto fallback = std::make_unique<RandomPlayer>();
playersWithBoard.emplace_back(std::make_unique<OptimalSolutionPlayer>(std::move(fallback)), Board{});
int constexpr numBoardPositions = 19;
for (int i = 0; i < numBoardPositions; ++i){
// The game as a total of 27 tiles.
// In each round, one random tile is drawn from the remaining set of tiles.
// The tile is presented and the position is chosen by the user.
Tile chosenTile = remainingTiles.drawRandomTile();
for(auto & [player, board] : playersWithBoard) {
std::cout << "---------------------\n";
std::cout << "The current board of " << player->getName()
<< " with the score of " << board.GetScore()
<< " is:\n" << board << "\n";
bool success = false;
while (!success) {
auto const chosen_position = player->SelectPosition(chosenTile, board);
success = board.SetTileToPosition(chosenTile, chosen_position);
if(success) {
std::cout << "The chosen position is: " << chosen_position << "\n";
}
else {
std::cout << "The position " << chosen_position << " is not a valid, free position. Try again!\n";
}
}
}
}
std::cout << "\n\n***GAME OVER***\n";
std::vector<int> finalScores;
for(auto const & [player, board] : playersWithBoard) {
finalScores.emplace_back(board.GetScore());
std::cout << player->getName()
<< " finished with a final score of "
<< board.GetScore() << " with the board: \n" << board << "\n";
}
std::vector<int>::const_iterator bestPlayer = std::ranges::max_element(finalScores);
std::cout << "***The player "
<< playersWithBoard.at(std::distance(finalScores.cbegin(), bestPlayer)).first->getName()
<< " wins with the score of " << *bestPlayer << "!***\n";
}