-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
272 lines (229 loc) · 5.07 KB
/
Game.cpp
File metadata and controls
272 lines (229 loc) · 5.07 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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include "Attack.h"
#include "Card.h"
#include "Durak.h"
#include "Game.h"
#include "LogUtil.h"
#include "NetworkHandler.h"
#include "Player.h"
#include "TextureManager.h"
#include <iostream>
#include <qstring.h>
#include <qtimer.h>
void startGame() //Should only executed by server
{
game->preInit(server->clients.size() + 1, 0);
//Start the game for everyone
for (Client* client : server->clients)
{
sendPacketOnly(client->id, INIT, std::format("{};{};{}", std::to_string(server->clients.size() + 1), std::to_string(client->id), std::to_string(game->trump)));
}
}
void resetGame()
{
delete game;
if (isServer())
{
sendPacket(RESET);
startGame();
}
}
Game::~Game()
{
for (Player* p : players)
{
delete p;
}
for (Card* card : cardRegister)
{
delete card;
}
delete currentAttack;
durak->close();
cleanUpTextures();
}
void Game::preInit(int playerAmount, int playerId)
{
//Initiliaze the game
initTextures();
genCards();
durak = new Durak();
durak->initUi();
this->playerAmount = playerAmount;
//Init players
for (int i = 0; i < playerAmount; i++)
{
players.push_back(new Player(i));
}
setCurrentPlayer(playerId);
if (isServer())
{
//Shuffle cards and select the trump
shuffleCards(&cardStack);
setTrump(cardStack.back()->type);
}
if (isClient()) sendPacket(INIT_RESPONSE); //If client, let server know init is done
logI("PreInit completed");
}
void Game::postInit() //Only run by the server after all clients are initialized
{
//Synchronize the stack
for (Card* card : game->cardStack)
{
sendPacket(ADDTOSTACK, card->id);
}
game->initialDraw();
//Start the game
sendPacket(START);
durak->timerStart();
durak->show();
//Initial attack
//TODO: Make first defender the one next to guy with lowest trump
Attack::createAttack(getPlayer(0), getPlayer(-1), getPlayer(+1), false);
logI("PostInit completed");
}
void Game::tick()
{
if (currentAttack->isFinished) //Start a new attack if last one has finished
{
//Stock up cards and calculate new defender before finishing
int newDef = currentAttack->defender->id + (currentAttack->isDefended ? 1 : 2);
stockUpCards(currentAttack->defender, currentAttack->attacker1, currentAttack->attacker2);
//Clear the bord and start a new attack
currentAttack->finish();
//Check if only one player is remaining (game done), else start a new attack
if (getFinishedPlayers() >= players.size() - 1)
{
endGame();
return;
}
else
{
Attack::createAttack(getPlayer(newDef), getPlayer(newDef - 1), getPlayer(newDef + 1), false);
}
}
}
void Game::endGame()
{
if (isServer)
{
gameTimer->stop();
sendPacket(ENDGAME);
}
updatePlayers();
durak->setPlayerStatus(game->player->status); //TODO: method in "game" to update player status, accounting for future player status widget
durak->showEndScreen(player->finalPos);
}
int Game::getFinishedPlayers()
{
//Count the amount of players that are finished
int playersDone = 0;
for (Player* player : players)
{
if (player->status == FINISHED)
{
playersDone++;
}
}
return playersDone;
}
void Game::setCurrentPlayer(int id)
{
//Set the player that is playing on this instance
player = getPlayer(id);
durak->setPlayer(player);
}
void Game::initialDraw()
{
bool invalidHand = false;
//Give each player 6 cards
for (Player* player : players)
{
for (int i = 0; i < 6; i++)
{
drawCard(player);
//if (player->invalidHand()) invalidHand = true;
}
}
//Check for "Neumischung"
if (invalidHand)
{
logI("Redrawing cards because a player had an invalid hand");
//TODO: Cards from player hand have to go back to stack !BUG!
for (Player* player : players) player->clearHand();
initialDraw();
}
}
void Game::genCards()
{
//Init cards
for (int i = 0; i < 4; ++i)
{
CardType type = static_cast<CardType>(i);
for (int value = 6; value < 15; value++)
{
//Add card to the register. If server, also add card to the stack
Card* card = new Card(type, value, Card::nameFromValue(value));
cardRegister.push_back(card);
if (isServer) cardStack.push_back(card);
}
}
}
void Game::setTrump(CardType type)
{
trump = type;
durak->setTrump(type);
}
void Game::drawCard(Player* player)
{
if (cardStack.size() != 0)
{
//Take topmost card from the stack and add it to player's inv
Card* card = cardStack.front();
cardStack.erase(cardStack.begin());
player->addCard(card, false);
}
}
void Game::stockUpCards(Player* def, Player* att1, Player* att2)
{
int i = 0;
//Attacker 1
while (att1->hand.size() < 6)
{
if (cardStack.size() == 0) return;
drawCard(att1);
}
//Attacker 2
if (att2 != att1)
{
while (att2->hand.size() < 6)
{
if (cardStack.size() == 0) return;
drawCard(att2);
}
}
//Defender
while (def->hand.size() < 6)
{
if (cardStack.size() == 0) return;
drawCard(def);
}
}
Player* Game::getPlayer(int id)
{
int index = ((id % playerAmount) + playerAmount) % playerAmount;
return players[index];
}
void Game::updatePlayers()
{
for (Player* player : players)
{
player->updateStatus();
}
}
Card* getCard(std::string id)
{
for (Card* card : cardRegister)
{
if (card->id == id) return card;
}
}