-
Notifications
You must be signed in to change notification settings - Fork 4
/
Deal.cpp
216 lines (183 loc) · 5.93 KB
/
Deal.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include "Deal.h"
Deal::Deal(array<Player *, 4> players, int dealer) :
_players(players),
_dealer(dealer),
_round(-1),
_first_player(-1),
_contract(nullptr),
_contractor(-1),
_state(State::Dealing) {};
Trick *Deal::getCurrentTrick() {
return _currentTrick;
}
Contract *Deal::getContract() {
return _contract;
}
void Deal::dealing() {
makeMemento();
//shuffling
auto deck = Card::getDeck();
random_shuffle(begin(deck), end(deck));
//distribution
int i = (_dealer + 1) % 4;
for (Card *c: deck) {
_players[i]->giveCard(c);
i = (i + 1) % 4;
}
_state = State::Bidding;
makeMemento();
}
void Deal::bidding() {
int speaker = ((_contractor == -1 ? _dealer : _contractor) + 1) % 4;
int nbrOfPass = 0;
Contract *tmp_c;
cout << AdvancedConsole::Erase();
cout << AdvancedConsole::Cursor(0, 0) << "Proposes:";
while ((nbrOfPass < 3) || (nbrOfPass == 3 && _contract == nullptr)) {
cout << AdvancedConsole::Cursor(6, 0)
<< AdvancedConsole::Erase(AdvancedConsole::AC_ALL, AdvancedConsole::AC_LINE)
<< "Player: " << *_players[speaker] << endl;
tmp_c = _players[speaker]->proposeContract(_contract);
cout << AdvancedConsole::Cursor(speaker + 2, 0)
<< AdvancedConsole::Erase(AdvancedConsole::AC_ALL, AdvancedConsole::AC_LINE);
if (tmp_c == Contract::Pass()) {
cout << *_players[speaker] << " passed" << endl;
nbrOfPass++;
} else if (_contract == nullptr || tmp_c > _contract) {
cout << *_players[speaker] << " proposed " << *tmp_c << endl;
nbrOfPass = 0;
_contract = tmp_c;
_contractor = speaker;
makeMemento();
} else {
cout << "Error, contract lower than the previous one" << endl;
nbrOfPass++;
}
speaker = (speaker + 1) % 4;
}
_state = State::Playing;
makeMemento();
}
void Deal::playing() {
if (_first_player == -1) _first_player = (_contractor + 1) % 4;
if (_round == -1) _round = 0;
//we play 13 tricks
while (_round < 13) {
//the four players have to play a trick
_currentTrick = new Trick();
cout << AdvancedConsole::Cursor(0, 0)
<< AdvancedConsole::Erase(AdvancedConsole::AC_ALL, AdvancedConsole::AC_SCREEN) << "Round: " << _round + 1
<< endl;
for (int i = 0; i < 4; i++) {
int current_player = (_first_player + i) % 4;
Card *card_played = _players[current_player]->playCard();
cout << AdvancedConsole::Cursor(i+2, 0)
<< AdvancedConsole::Erase(AdvancedConsole::AC_ALL, AdvancedConsole::AC_LINE)
<< *_players[current_player] << ": " << *card_played << endl;
_currentTrick->addCard(card_played, current_player);
}
_first_player = _currentTrick->getWinner(_contract);
cout << "Winner: " << *_players[_first_player] << endl << endl;
_players[_first_player]->getTeam()->winTrick(_currentTrick);
_round++;
makeMemento();
cout << "Press any key to start next round..." << AdvancedConsole::Erase(AdvancedConsole::AC_AFTER, AdvancedConsole::AC_SCREEN);
cin.ignore();
cin.get();
}
}
void Deal::scoring() {
//Teams North-South and East-West
Team *Contracting, *Defending;
Contracting = _players[_contractor]->getTeam();
Defending = _players[(_contractor + 1) % 4]->getTeam();
// nbr of tricks made after the first 6
int nbrOfOddTricks = static_cast<int>(Contracting->getWonTricks().size()) - 6;
if (nbrOfOddTricks < _contract->getTricksAmount()) {
//if contract is failed
Defending->addToGameScore(scoreIfLost(nbrOfOddTricks));
} else {
// if contract is a success
Contracting->addToGameScore(scoreIfWon(nbrOfOddTricks));
}
Contracting->newDeal();
Defending->newDeal();
_state = State::Completed;
makeMemento();
}
int Deal::scoreIfWon(int nbrOfOddTricks) {
int slamBonus;
int contractPoints = 0;
switch (_contract->getTricksAmount()) {
case 6:
slamBonus = 500;
break;
case 7:
slamBonus = 1000;
break;
default :
slamBonus = 0;
break;
}
switch (_contract->getColor()) {
case Color::CLUB:
case Color::DIAMOND:
contractPoints = 20 * nbrOfOddTricks;
break;
case Color::HEART:
case Color::SPADE:
contractPoints = 30 * nbrOfOddTricks;
break;
case Color::NOTRUMP:
contractPoints = 10 + 30 * nbrOfOddTricks;
break;
}
return contractPoints + slamBonus;
}
int Deal::scoreIfLost(int nbrOfOddTricks) {
int requiredTricks = _contract->getTricksAmount();
return 50 * (requiredTricks - nbrOfOddTricks);
}
void Deal::play() {
switch (_state) {
case State::Dealing:
dealing();
case State::Bidding:
bidding();
case State::Playing:
playing();
scoring();
break;
case State::Completed:
break;
}
}
Memento *Deal::makeMemento() {
auto m = new Memento;
m->setFirstPlayer(_first_player);
m->setState(_state);
m->setRound(_round);
m->setPlayers(_players);
m->setContract(_contract, _contractor);
m->saveFile();
return m;
}
void Deal::reset(Memento *mem) {
_first_player = mem->first_player;
_state = mem->state;
_round = mem->round;
int pi = 0;
for (const auto &player:mem->players) {
_players[pi]->unserialize(player);
pi++;
}
_contractor = mem->contractor;
if (!mem->contract.is_null()) {
_contract = Contract::unserialize(mem->contract);
}
int i = 0;
for (auto &player: _players) {
player->unserialize(mem->players[i]);
i++;
}
}