-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.cpp
181 lines (167 loc) · 5.65 KB
/
Player.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
#include "Player.h"
//motedy publiczne
Player::Player(int _id)
{
id = _id;
gold = 900;
name = "P" +std::to_string(_id+1);
}
void Player::buyUnit(Profession* _unit,int _cost)
{
if (id == 1) _unit->setDisplayChar(tolower(_unit->getDisplayChar()));
units.push_back(_unit);
gold = gold - _cost;
}
std::vector<Profession*>& Player::getUnits()
{
return units;
}
int Player::getID()
{
return id;
}
int Player::getGold()
{
return gold;
}
void Player::doSomethingWithUnit(Profession* _myUnit, char _posData[][BOARD_SIZE], Profession* _boardData[][BOARD_SIZE], std::vector<Profession*> _myPlayerUnits, std::vector<Profession*> _enemyPlayerUnits, std::vector<std::string>& _gameLOG,bool& _gameON)
{
int choice = 0;
bool madeValidMove = false;
std::cout <<name<< ": Unit: " << _myUnit->getName() << " on coords: " << _myUnit->getPosition() << std::endl;// _myUnit->getPosition().first << "," << _myUnit->getPosition().second << std::endl; //sprobowac napisac operator do wysietlania paira
std::cout << "1-> Move \n2-> Attack \n3-> Use superpower\n4-> Wait\n99->Abort playing\n";
while (!madeValidMove)
{
choice = getValidInput<int>();
switch (choice)
{
case 1:
madeValidMove = moveUnit(_myUnit, _posData, _gameLOG);
break;
case 2:
madeValidMove = attackUnit(_myUnit, _posData, _boardData, _gameLOG);
break;
case 3:
madeValidMove = useSuperPower(_myUnit, _myPlayerUnits, _enemyPlayerUnits, _posData, _gameLOG);
break;
case 4:
madeValidMove = true;
_gameLOG.push_back(name + ": " + _myUnit->getName() + " " + _myUnit->getDisplayCoords(_myUnit->getPosition()) + " didnt take any action\n");
break;
case 99:
_gameON = false;
_gameLOG.push_back("Game aborted\n");
madeValidMove = true;
break;
default:
std::cout << "Invalid choice, try again\n";
break;
}
}
}
//metody prywatne
bool Player::moveUnit(Profession* _myUnit, char _posData[][BOARD_SIZE], std::vector<std::string>& _gameLOG)
{
bool finished = false;
std::pair<int, int>startCoords = _myUnit->getPosition();
int currX = startCoords.first;
int currY = startCoords.second;
_myUnit->findPossibleMoves(_posData);
std::cout << "Use arrows to move your unit\nPress enter when finished\n\n";
while (!finished)
{
if (_kbhit())
{
switch (_getch())
{
case KEY_UP:
if (_myUnit->checkIfValidMove(std::make_pair(currX, currY - 1)))
{
currY -= 1;
_myUnit->setPosition(std::make_pair(currX, currY));
std::cout << "\033[F"; //cofam linie w coucie
std::cout << "Your unit is now on: "<<_myUnit->getPosition()<<"\n";
}
break;
case KEY_DOWN:
// std::cout << "DOWN!\n";
if (_myUnit->checkIfValidMove(std::make_pair(currX, currY + 1)))
{
currY += 1;
_myUnit->setPosition(std::make_pair(currX, currY));
std::cout << "\033[F"; //cofam linie w coucie
std::cout << "Your unit is now on: " << _myUnit->getPosition() << "\n";
}
break;
case KEY_LEFT:
//std::cout << "LEFT!\n";
if (_myUnit->checkIfValidMove(std::make_pair(currX-1, currY )))
{
currX -= 1;
_myUnit->setPosition(std::make_pair(currX, currY));
std::cout << "\033[F"; //cofam linie w coucie
std::cout << "Your unit is now on: " << _myUnit->getPosition() << "\n";
}
break;
case KEY_RIGHT:
// std::cout << "RIGHT!\n";
if (_myUnit->checkIfValidMove(std::make_pair(currX+1, currY)))
{
currX += 1;
_myUnit->setPosition(std::make_pair(currX, currY));
std::cout << "\033[F"; //cofam linie w coucie
std::cout << "Your unit is now on: " << _myUnit->getPosition() << "\n";
}
break;
case '\r':
finished = true;
break;
}
}
}
if (startCoords == _myUnit->getPosition())
{
std::cout << "You are on the same position.\nChoose action again\n";
return false;
}
_gameLOG.push_back(name + ": " + _myUnit->getName() + " moved from " + _myUnit->getDisplayCoords(startCoords) + " to " + _myUnit->getDisplayCoords(_myUnit->getPosition()) + "\n");
return true;
}
bool Player::attackUnit(Profession* _myUnit, char _posData[][BOARD_SIZE], Profession* _boardData[][BOARD_SIZE], std::vector<std::string>& _gameLOG)
{
bool isThereUnitToAttack = false;
isThereUnitToAttack = _myUnit->findPossibleCordsToAttack(_posData,id);
if (!isThereUnitToAttack)
{
std::cout << "There is no unit to attack in your range!\nChoose action again\n";
return false;
}
_myUnit->displayPossibleAttack();
int idToAttack = -2;
while (true)
{
std::cout << "Enter ID to attack or -1 to go back\n";
idToAttack=getValidInput<int>();
if (idToAttack == -1) return false;
if (_myUnit->checkIfValidIdToAttack(idToAttack))
{
int xCordToAttack = _myUnit->getPossibleAttackCords(idToAttack).first;
int yCordToAttack = _myUnit->getPossibleAttackCords(idToAttack).second;
_myUnit->attack(_boardData[xCordToAttack][yCordToAttack]);
_gameLOG.push_back(name + ": " + _myUnit->getName()+" "+ _myUnit->getDisplayCoords(_myUnit->getPosition()) +" attacked "+ _boardData[xCordToAttack][yCordToAttack]->getName() + " "+_myUnit->getDisplayCoords(_boardData[xCordToAttack][yCordToAttack]->getPosition())+"\n");
return true;
}
std::cout << "You cant attack unit with this id, try again\n";
}
}
bool Player::useSuperPower(Profession* _myUnit, std::vector<Profession*> _myPlayerUnits, std::vector<Profession*> _enemyPlayerUnits, char _posData[][BOARD_SIZE], std::vector<std::string>& _gameLOG)
{
if (_myUnit->getCurrentMana() < _myUnit->getStat(MANA_MAX))
{
std::cout << "Not enough mana\nChoose action again\n";
return false;
}
bool temp= _myUnit->useSuperPower(_myPlayerUnits, _enemyPlayerUnits, _posData,_gameLOG);
if (temp == false)std::cout << "Your Super Power cannot be used.\nChoose action again\n";
return temp;
}