-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.cpp
More file actions
117 lines (96 loc) · 2.73 KB
/
Copy pathPlayer.cpp
File metadata and controls
117 lines (96 loc) · 2.73 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
//
// Created by Raunak Anand on 2019-06-01.
//
#include "BattleShip.hpp"
int BattleShip::Player::instances = 0;
BattleShip::Player::Player(const Game& gameAttributes, View& view) : id(instances), view(view), board(Board(gameAttributes.getNumRows(),gameAttributes.getNumCols())) {
++instances;
shipHealths = gameAttributes.getShipParameters();
}
BattleShip::Player& BattleShip::Player::getOpponent() {
return *opponent;
}
const BattleShip::Player& BattleShip::Player::getOpponent() const {
return *opponent;
}
void BattleShip::Player::setOpponent(BattleShip::Player& opponent) {
(*this).opponent = &opponent;
}
void BattleShip::Player::setName(const std::string& name) {
(*this).playername = name;
}
const BattleShip::Board& BattleShip::Player::getBoard() const {
return board;
}
bool BattleShip::Player::operator==(const Player& rhs) const {
return ((*this).getId() == rhs.getId());
}
const std::string& BattleShip::Player::getName() const {
return playername;
}
BattleShip::Board& BattleShip::Player::getBoard() {
return board;
}
bool BattleShip::Player::operator!=(const Player& rhs) const {
return ((*this).getId() != rhs.getId());
}
bool BattleShip::Player::StatusSunk() const {
for (auto const &itr: shipHealths) {
if (itr.second != 0) {
return false;
}
}
return true;
}
BattleShip::DeclareResult BattleShip::Player::fire(int row, int column){
std::map<char, int> preAttackHealth = getOpponent().shipHealths;
auto preIt = preAttackHealth.begin();
AttackMove attack(*this, row, column);
attack.shipHealth((*this).getOpponent(), view);
auto it=getOpponent().shipHealths.begin();
while(it!=getOpponent().shipHealths.end() && preIt!=preAttackHealth.end()){
if((*it).second!=(*preIt).second){
return DeclareResult(true, (*it).second == 0, (*it).first);
}
++it;
++preIt;
}
return {false, false, '*'};
}
bool BattleShip::Player::hit(char shipChar) {
return (shipHealths.at(shipChar) != 3);
}
const int BattleShip::Player::getId() const {
return id;
}
//Ship& Player::operator()(int x, int y) {
// return getBoard()(y, x);
//}
//
//Player& Player::operator=(const Player &rhs) {
// if(this != &rhs) {
// playerType = rhs.playerType;
// id = rhs.id;
// turn = rhs.turn;
// playerBoard = rhs.playerBoard;
// }
// return *this;
//}
//
//Player::Player(const Player &rhs) {
// playerType = rhs.playerType;
// id = rhs.id;
// turn = rhs.turn;
// playerBoard = rhs.playerBoard;
//}
//
//bool Player::operator==(const Player&rhs) {
// if((playerBoard != rhs.playerBoard) || (playerType != rhs.playerType) || (id != rhs.id) || (turn != rhs.turn)) {
// return false;
// }
// return true;
//}
//
//bool Player::operator!=(const Player& rhs) {
// return !(*this == rhs);
//}