-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBeginGame.cpp
More file actions
86 lines (71 loc) · 2.52 KB
/
Copy pathBeginGame.cpp
File metadata and controls
86 lines (71 loc) · 2.52 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
//
// Created by Raunak Anand on 2019-06-01.
//
#include "BattleShip.hpp"
BattleShip::BeginGame::BeginGame(bool beginGame): beginGame(beginGame), view(std::make_unique<BoardView>(std::cin, std::cout)){
}
BattleShip::BeginGame::BeginGame(){
}
void BattleShip::BeginGame::setupGame(const std::string& GameConfigurationFile, int seed) {
BattleShip::AiPlayer::seed_random_number_generator(seed);
setupGame(GameConfigurationFile);
}
void BattleShip::BeginGame::setupGame(const std::string& GameConfigurationFile) {
creation.fileArguments(GameConfigurationFile);
NumPlayer config = (*view).getPlayerConfiguration();
for (int i = 0; i < config.HumanPlayers; i++) {
creation.addPlayer<HumanPlayer>((*view));
}
for (int j = 0; j < config.AiPlayers; j++) {
int choice = (*view).getAiChoice();
if (choice == 1) {
creation.addPlayer<CheatingAi>((*view));
} else if (choice == 2) {
creation.addPlayer<RandomAI>((*view));
} else if (choice == 3) {
creation.addPlayer<HuntDestroyAI>((*view));
}
}
creation.SetOpponents();
beginGame = true;
}
void BattleShip::BeginGame::setupGame() {
}
void BattleShip::BeginGame::play() {
while (!creation.isGameOver()) {
DeclareResult result;
Player &attacker = creation.getAttackingPlayer();
int status;
std::string className = abi::__cxa_demangle(typeid(attacker).name(), 0, 0, &status);
if (strncmp(className.c_str(), "BattleShip::HumanPlayer", 16) == 0) {
(*view).showPlayersBoard(attacker);
(*view).showPlacementBoard(attacker);
std::pair<int, int> pos = (*view).getFiringCoordinate(attacker);
while (true) {
if (attacker.getOpponent().getBoard().at(pos.first, pos.second).alreadyFiredAt()) {
pos = (*view).getFiringCoordinate(attacker);
} else {
break;
}
}
result = attacker.fire(pos.first, pos.second);
(*view).showPlayersBoard(attacker);
(*view).showPlacementBoard(attacker);
} else {
std::unique_ptr<Move> move = attacker.getMove();
while (true) {
if (attacker.getOpponent().getBoard().at((*move).getRowAttack(), (*move).getColAttack()).alreadyFiredAt()) {
move = attacker.getMove();
} else {
break;
}
}
result = attacker.fire((*move).getRowAttack(), (*move).getColAttack());
(*view).showPlayersBoard(attacker);
(*view).showPlacementBoard(attacker);
}
(*view).showAttackResult(attacker, result);
creation.changeTurn();
}
(*view).showWinner(creation.getWinner());
}