-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGame.cpp
More file actions
118 lines (97 loc) · 2.87 KB
/
Game.cpp
File metadata and controls
118 lines (97 loc) · 2.87 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "Game.h"
#include "ComputerPlayer.h"
#include "HumanPlayer.h"
#include "Memento.h"
#include "misc.h"
Deal *Game::getCurrentDeal() {
return _currentDeal;
}
Game::Game(array<Player *, 4> players) {
_players = players;
init();
}
Game::Game() {
cout << AdvancedConsole::Cursor(0, 0) << AdvancedConsole::Erase();
cout << "Welcome to the Bridge Game." << endl << endl;
Memento *mem = Memento::loadFile();
if (mem) {
cout << "Do you want to load last game?" << endl;
string q;
cout << "Yes(y) or No(n) : ";
getline(std::cin, q);
if (q == "y" || q == "Y" || q == "yes" || q == "YES") {
this->reset(mem);
_currentDeal = new Deal(_players, 0);
_currentDeal->reset(mem);
return;
}
cout << endl;
}
cout << "You should choose either player is a human or a computer. " << endl;
cout << "If it is a computer, just press ENTER." << endl;
cout << "If it is a human, just type the player name." << endl << endl;
string list[4] = {"North", "East", "South", "West"};
string name;
for (int i = 0; i < 4; i++) {
cout << "Player " << list[i] << " : ";
getline(std::cin, name);
bool isComputer = name.empty() || name == "computer";
if (isComputer) name = "Computer " + list[i];
_players[i] = createPlayer(isComputer);
_players[i]->setName(name);
}
init();
makeMemento();
}
void Game::init() {
_currentDeal = nullptr;
setTeams();
cout << endl;
for (const auto team : _teams) {
cout << *team;
}
cout << endl;
for (Player *player : _players) {
player->setGame(this);
}
}
void Game::setTeams() {
Team *team1 = new Team("Team 1", _players[0], _players[2]);
Team *team2 = new Team("Team 2", _players[1], _players[3]);
_teams = {team1, team2};
}
void Game::play() {
int dealer = 0;
bool isFinished = false;
while (!isFinished) {
if (!_currentDeal)
_currentDeal = new Deal(_players, dealer);
_currentDeal->play();
cout << _teams[0]->getName() << " : " << _teams[0]->getGameScore() << endl;
cout << _teams[1]->getName() << " : " << _teams[1]->getGameScore() << endl;
isFinished = true;
_currentDeal = nullptr;
}
}
Memento *Game::makeMemento() {
auto m = new Memento;
m->setPlayers(_players);
m->saveFile();
return m;
}
void Game::reset(Memento *mem) {
for (int i = 0; i < 4; i++) {
_players[i] = createPlayer(mem->players[i]["isComputer"]);
_players[i]->unserialize(mem->players[i]);
}
init();
_teams[0]->unserialize(mem->teams[0]);
_teams[1]->unserialize(mem->teams[1]);
}
Player *Game::createPlayer(int player_type) {
if (player_type == 0) {
return new HumanPlayer;
} else {
return new ComputerPlayer;
}
}