-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGameLogicManager.cpp
276 lines (245 loc) · 8.99 KB
/
GameLogicManager.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
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
273
274
275
276
#include "GameLogicManager.h"
#include "Minions.h"
//#define GAMELOGICMANAGER_DEBUG
#include <chrono>
#include <thread>
// Create the two players that will battle
void GameLogicManager::createPlayers()
{
#ifdef GAMELOGICMANAGER_DEBUG
std::cout << "[GAMELOGICMANAGER DEBUG]: Called from " << __FILE__ << " at line " << __LINE__ << " GameLogicManager::createPlayers" << std::endl;
#endif
m_player1 = std::make_unique<Player>("Player");
m_bot = std::make_unique<Bot>("Bot");
}
// Create the board that will manage the cards
void GameLogicManager::createBoard()
{
#ifdef GAMELOGICMANAGER_DEBUG
std::cout << "[GAMELOGICMANAGER DEBUG]: Called from " << __FILE__ << " at line " << __LINE__ << " GameLogicManager::createBoard" << std::endl;
#endif
m_board = std::make_unique<Board>();
std::experimental::observer_ptr<Board> board_ptr = std::experimental::make_observer(m_board.get());
m_player1->linkBoard(board_ptr);
m_bot->linkBoard(board_ptr);
}
// Create the shop where players will buy and sell cards
void GameLogicManager::createShop()
{
#ifdef GAMELOGICMANAGER_DEBUG
std::cout << "[GAMELOGICMANAGER DEBUG]: Called from " << __FILE__ << " at line " << __LINE__ << " GameLogicManager::createShop" << std::endl;
#endif
m_shop = std::make_unique<Shop>();
}
void GameLogicManager::createCLI()
{
#ifdef GAMELOGICMANAGER_DEBUG
std::cout << "[GAMELOGICMANAGER DEBUG]: Called from " << __FILE__ << " at line " << __LINE__ << " GameLogicManager::createCLI" << std::endl;
#endif
std::cout << "Creating CLI..." << std::endl;
m_cli = std::make_unique<CLI>();
}
CLI::cli_input GameLogicManager::recruitementPhase()
{
#ifdef GAMELOGICMANAGER_DEBUG
std::cout << "[GAMELOGICMANAGER DEBUG]: Called from " << __FILE__ << " at line " << __LINE__ << " GameLogicManager::recruitementPhase" << std::endl;
#endif
// Give gold to the players based on the turn
m_shop->giveGold(*m_player1, m_turn);
m_shop->giveGold(*m_bot, m_turn);
// Draw 3 cards from the shop
m_shop->giveChoice(*m_player1);
// Get the input from the players
CLI::cli_input input;
do{
// Clear
m_cli->clear();
// Draw the board
m_cli->drawBoard(*m_board, *m_player1, *m_bot);
// Draw the choices
m_cli->drawChoices(*m_player1);
// Draw the hand
m_cli->drawHand(*m_player1);
// Draw the player stats
m_cli->drawStats(*m_bot);
m_cli->drawStats(*m_player1);
input = m_cli->getInput(*m_player1);
switch (input.choice)
{
case CLI::BUY:
m_player1->selectCardFromChoices(input.card, *m_shop);
break;
case CLI::SELL:
m_player1->sellCardFromHand(input.card, *m_shop);
break;
case CLI::ROLL:
m_shop->giveAnotherChoice(*m_player1);
break;
case CLI::UPGRADE:
m_player1->upgradeLevel();
break;
case CLI::PLAY:
m_player1->moveCardFromHandToBoardLeft(input.card);
break;
case CLI::EXIT:
std::cout << "Exiting..." << std::endl;
return {CLI::EXIT, 0};
}
}while(input.choice != CLI::BATTLE);
m_player1->resetChoices();
// bot phase
m_shop->giveChoice(*m_bot);
m_bot->playTurn(*m_shop);
m_cli->drawHand(*m_bot);
m_bot->resetChoices();
m_turn++;
return input;
}
CLI::cli_input GameLogicManager::battlePhase()
{
#ifdef GAMELOGICMANAGER_DEBUG
std::cout << "[GAMELOGICMANAGER DEBUG]: Called from " << __FILE__ << " at line " << __LINE__ << " GameLogicManager::battlePhase" << std::endl;
#endif
m_cli->drawBoard(*m_board, *m_player1, *m_bot);
std::this_thread::sleep_for(std::chrono::seconds(3));
// Player with the most cards on the board attacks first
int player_nb_cards = m_board->getNumberOfCards(*m_player1);
int bot_nb_cards = m_board->getNumberOfCards(*m_bot);
Player* attacker = (player_nb_cards > bot_nb_cards) ? m_player1.get() : m_bot.get();
Player* defender = (player_nb_cards > bot_nb_cards) ? m_bot.get() : m_player1.get();
// If same number of cards, random
if (player_nb_cards == bot_nb_cards)
{
int random = rand() % 2;
// Switch attacker and defender
if (random == 0)
{
Player* temp = attacker;
attacker = defender;
defender = temp;
}
}
// Get player cards on board
std::vector<std::reference_wrapper<Card>> player_cards = m_board->getPlayerCardsView(*attacker);
std::vector<std::reference_wrapper<Card>> player2_cards = m_board->getPlayerCardsView(*defender);
// For each card of the attacking player
for (auto card : player_cards)
{
// If the minion has no atack point
Minion* minion = dynamic_cast<Minion*>(&card.get());
if (minion->getAttack() == 0)
continue;
// If the enemy has no cards on board, attack the enemy directly
if (player2_cards.size() == 0)
{
minion->attackEnemy(*defender);
m_cli->clear();
m_cli->drawBoard(*m_board, *m_player1, *m_bot);
std::cout << minion->getName() << " attacks " << defender->getName() << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(3));
minion->setState(Minion::State::IDLING);
// Check if player is dead
if (defender->getHealth() <= 0)
{
std::cout << attacker->getName() << " WINS !" << std::endl;
return {CLI::EXIT, 0};
}
continue;
}
// Get enemy cards with taunt
std::vector<std::reference_wrapper<Card>> taunt_cards = m_board->getPlayerCardsViewWithTaunt(*defender);
// Select a random card of the defending player
int card_to_attack = rand() % player2_cards.size();
// Dynamic cast to minion
// If the enemy has taunt cards, attack a random taunt card
if (taunt_cards.size() > 0)
{
card_to_attack = rand() % taunt_cards.size();
// Get index of the taunt card
card_to_attack = m_board->getCardIndex(taunt_cards[card_to_attack].get());
}
Minion *minion2 = dynamic_cast<Minion *>(&player2_cards[card_to_attack].get());
// Attack the card if the minion is alive
m_cli->clear();
if (minion->getHealth() > 0)
{
minion->attackEnemy(*minion2);
std::cout << minion->getName() << " attacks " << minion2->getName() << std::endl;
}
m_cli->drawBoard(*m_board, *m_player1, *m_bot);
std::this_thread::sleep_for(std::chrono::seconds(3));
minion->setState(Minion::State::IDLING);
minion2->setState(Minion::State::IDLING);
m_board->destroyCards();
player2_cards = m_board->getPlayerCardsView(*defender);
m_cli->clear();
m_cli->drawBoard(*m_board, *m_player1, *m_bot);
std::this_thread::sleep_for(std::chrono::seconds(3));
}
// For each card of the defending player
for (auto card : player2_cards)
{
// If the minion has no atack point
Minion* minion = dynamic_cast<Minion*>(&card.get());
if (minion->getAttack() == 0)
continue;
// If the enemy has no cards on board, attack the enemy directly
if (player_cards.size() == 0)
{
minion->attackEnemy(*attacker);
m_cli->clear();
m_cli->drawBoard(*m_board, *m_player1, *m_bot);
std::cout << minion->getName() << " attacks " << attacker->getName() << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(3));
minion->setState(Minion::State::IDLING);
// Check if player is dead
if (attacker->getHealth() <= 0)
{
std::cout << defender->getName() << " WINS !" << std::endl;
return {CLI::EXIT, 0};
}
continue;
}
// Get enemy cards with taunt
std::vector<std::reference_wrapper<Card>> taunt_cards = m_board->getPlayerCardsViewWithTaunt(*attacker);
// Select a random card of the attacking player
int card_to_attack = rand() % player_cards.size();
// If the enemy has taunt cards, attack a random taunt card
if (taunt_cards.size() > 0)
{
card_to_attack = rand() % taunt_cards.size();
// Get index of the taunt card
card_to_attack = m_board->getCardIndex(taunt_cards[card_to_attack].get());
}
// Dynamic cast to minion
Minion *minion2 = dynamic_cast<Minion *>(&player_cards[card_to_attack].get());
// Attack the card
m_cli->clear();
if (minion->getHealth() > 0)
{
minion->attackEnemy(*minion2);
std::cout << minion->getName() << " attacks " << minion2->getName() << std::endl;
}
m_cli->drawBoard(*m_board, *m_player1, *m_bot);
std::this_thread::sleep_for(std::chrono::seconds(3));
minion->setState(Minion::State::IDLING);
minion2->setState(Minion::State::IDLING);
m_board->destroyCards();
player_cards = m_board->getPlayerCardsView(*attacker);
m_cli->clear();
m_cli->drawBoard(*m_board, *m_player1, *m_bot);
std::this_thread::sleep_for(std::chrono::seconds(3));
}
if (m_player1->getHealth() == 0){
std::cout << "You Lose !!!" << std::endl;
std::cout << "Exiting..." << std::endl;
return {CLI::EXIT, 0};
}
else if (m_bot->getHealth() == 0){
std::cout << "You Win !!!" << std::endl;
std::cout << "Exiting..." << std::endl;
return {CLI::EXIT, 0};
}
else
return {CLI::CONTINUE, 0};
}