-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNullBattle.cpp
More file actions
192 lines (137 loc) · 4.4 KB
/
NullBattle.cpp
File metadata and controls
192 lines (137 loc) · 4.4 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
#include "NullBattle.h"
#include <DataLoader.hpp>
void log(std::string text) {
std::cout << "[CoreEngine] " + text << std::endl;
}
void runPlayersActions(BattleStateMachine& engine) {
Pokemon& p1Pokemon = engine.p1ActivePokemon();
Pokemon& p2Pokemon = engine.p2ActivePokemon();
for (unsigned int k = 0; k <= 1; k++) {
printBattlePanel();
int actionIndex = 0;
std::cout << "[Player " + std::to_string(k+1) + "] > Faça uma ação: ";
int action;
std::cin >> action;
if (ActionTypeMap[action] == ActionType::USE_MOVE) {
printMovesPanel((k+1) == 1 ? p1Pokemon : p2Pokemon, k + 1);
std::cout << "[Player " + std::to_string(k + 1) + "] > Escolha um golpe: ";
std::cin >> actionIndex;
}
if (actionIndex == 0) { actionIndex = 1; }
if (k + 1 == 1) {
engine.player1Action(BattleAction{ ActionTypeMap[action], actionIndex - 1 });
} else { engine.player2Action(BattleAction{ ActionTypeMap[action], actionIndex - 1 }); }
}
engine.startExecutingTurn();
std::cout << std::endl;
}
void initPlayers(Player& player1, Player& player2,
std::vector<PokemonTemplate> pokemonList, std::map<std::string, Move> moves
) {
player1.id = 1;
player2.id = 2;
PokemonTemplate pikachuTmp = pokemonList[24];
PokemonTemplate charmanderTmp = pokemonList[3];
PokemonTemplate bulbasaurTmp = pokemonList[0];
PokemonTemplate squirtleTmp = pokemonList[6];
Pokemon pikachu = pikachuTmp.build(
5,
{ "thunder-shock", "growl", "thunder-wave", "tail-whip" },
moves
);
Pokemon bulbasaur = bulbasaurTmp.build(
5,
{ "tackle", "growl", "leech-seed", "vine-whip" },
moves
);
Pokemon squirtle = squirtleTmp.build(
5,
{ "tackle", "bubble", "withdraw", "tail-whip" },
moves
);
Pokemon charmander = charmanderTmp.build(
5,
{ "tackle", "growl", "ember", "flamethrower" },
moves
);
player1.team.party = {pikachu, bulbasaur};
player2.team.party = {charmander, squirtle};
}
void forceSwitchActivePokemon(Player& player) {
log("Player " + std::to_string(player.id) + " vai trocar de Pokémon!");
int pokemonIndex = 0;
unsigned int partyIndex = 0;
for (auto& poke : player.team.party) {
std::cout << printPokemonData(poke) << std::endl;
}
std::cout << "[Player " + std::to_string(player.id) + "] Escolha o index do pokémon do " +
"seu time para substituir por " + player.team.inBattle().name + ": ";
std::cin >> pokemonIndex;
auto chosenPokemon = player.team.party[pokemonIndex];
log("Pokémon escolhido: " + chosenPokemon.name);
player.team.switchActivePokemon(pokemonIndex);
}
void saveMatchResult(
const std::string& sessionId,
int totalTurns,
const Player& winnerPlayer
) {
nlohmann::json result;
result["sessionId"] = sessionId;
result["totalTurns"] = totalTurns;
result["winnerPlayer"] = {
{"id", winnerPlayer.id},
{"lastUsed", winnerPlayer.team.party.size()}
};
std::string filename = "match_" + sessionId + ".json";
std::ofstream file(filename);
if (!file.is_open()) {
throw std::runtime_error("Nao foi possivel salvar o resultado em: " + filename);
}
file << result.dump(2);
file.close();
log("Resultado salvo em " + filename + "\n");
}
int main()
{
setlocale(LC_ALL, "Portuguese");
try {
DataLoader dataLoader = DataLoader();
std::string session = UuidV4().generate();
log("Starting session " + session + "...");
auto movesMap = dataLoader.loadMoves();
auto pokemonList = dataLoader.loadPokemon();
Player player1, player2;
initPlayers(player1, player2, pokemonList, movesMap);
BattleStateMachine battleEngine = BattleStateMachine(
player1, player2
);
bool battleEnd = false;
while (!battleEnd) {
switch (battleEngine.getState()) {
case BattleState::ACTION_TURN:
log("Starting battle...");
runPlayersActions(battleEngine);
break;
case BattleState::SWITCH_AFTER_FAINT:
forceSwitchActivePokemon(
battleEngine.whoWillSwitchPokemon == 1 ? player1 : player2);
battleEngine.startNewTurn();
break;
case BattleState::ACTION_EXECUTING_TURN:
log("Entrando em modo de execução do turno...");
battleEngine.executeTurnActions();
break;
case BattleState::BATTLE_END:
printVictoryScreen(battleEngine.winnerPlayer, battleEngine.getTotalTurns());
saveMatchResult(session, battleEngine.getTotalTurns(), battleEngine.winnerPlayer);
battleEnd = true;
break;
}
}
}
catch (const std::exception& e) {
std::cerr << "Erro: " << e.what() << std::endl;
}
return 0;
}