-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.cpp
309 lines (258 loc) · 7.64 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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/********************************************************************************************
*********************************************************************************************
Aaron's Tic-Tac-Toe Clone
A 2 player verison of Tic-Tac-Toe game that's played on a console screen.
Copyright (C) 2012 Aaron Gagern
This file is part of Aaron's Tic-Tac-Toe Clone.
Aaron's Tic-Tac-Toe Clone is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Aaron's Tic-Tac-Toe Clone is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Aaron's Tic-Tac-Toe Clone. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************************
********************************************************************************************/
#include "Player.h"
int Player::numOfPlayers_ = 0;
int Player::otherPlayerPiece_ = 0;
const std::string Player::noPlayerPiece = "noPlayerPiece";
const std::string Player::oPlayerPiece = "oPlayerPiece";
const std::string Player::xPlayerPiece = "xPlayerPiece";
const std::string Player::varError = "Player has not been initialized yet,\nplease run initialization function before use.";
const std::string Player::badMoveErrorSound = "badMoveErrorSound";
const std::string Player::fatalErrorSound = "fatalErrorSound";
const std::string Player::clickSound = "clickSound";
Player::Player() throw()
: textColor(), score_(0), madeMove_(false), playerInitialized_(false), win_(false), resetPlayerFunctionCalled_(false)
{
if(numOfPlayers_ == 0) //if numOfPlayers hasn't been used before
numOfPlayers_ = 1;
else //if numOfPlayers has been used before
numOfPlayers_++;
id_ = numOfPlayers_;
AssignPlayerPiece();
}
void Player::InitializePlayer(int boundsLimit)
{
(*this).boundsLimit_ = boundsLimit;
//Clear the screen
system("cls");
//Ask player for name
//Had to put it here because I need the ID for the prompt...
std::string n;
std::cout<<"Player "<<id_<<": What is your name?"<<std::endl;
std::cin>>n;
SoundEngine::GetInstance()->PlaySoundFromQueue(clickSound);
//Assigning string value n to name
name_ = n;
textColor.DecidePlayerScreenColor();
//Player has now been initialized
playerInitialized_ = true;
}
void Player::AssignPlayerPiece()
{
//Decide on piece for player
//1 = O
//2 = X
if(otherPlayerPiece_ == 0) //if otherPlayerPiece hasn't been filled yet, which means that player 1 hasn't been assigned a piece
PieceGen();
else
if(otherPlayerPiece_ == 1)
playerPiece_ = 2;
else
playerPiece_ = 1;
resetPlayerFunctionCalled_ = false;
}
//Make sure to call this after AssignPlayerPiece is called. This is very important!!!
void Player::ResetPlayer(int boundsLimit)
{
madeMove_ = false;
playerPiece_ = 0;
move_ = 0;
//Set boundsLimit
(*this).boundsLimit_ = boundsLimit;
AssignPlayerPiece();
}
//This must be called before ResetPlayer has been called in order for this to work correctly
void Player::ResetPlayerPiece()
{
if(!resetPlayerFunctionCalled_) {
otherPlayerPiece_ = 0;
resetPlayerFunctionCalled_ = true;
}
else
throw Exception(err.Double_Function_Call);
}
void Player::PieceGen()
{
int temp = (rand()%40)+1;
if(temp >= 1 && temp <= 20)
playerPiece_ = otherPlayerPiece_ = 1;
if(temp >= 21 && temp <= 40)
playerPiece_ = otherPlayerPiece_ = 2;
}
void Player::DisplayName()
{
std::cout<<"Player "<<id_<<": "<<name_;
}
void Player::DisplayScore()
{
std::cout<<"Player "<<id_<<"'s score is: "<<score_;
}
void Player::UpdateScore()
{
score_++;
}
bool Player::MakeMove()
{
DisplayName();
std::string choice;
std::cout<<std::endl;
std::cout<<"Enter in your choice below...\n";
std::cout<<"Ex. 1,1 or q to quit the game"<<std::endl;
std::cin>>choice;
bool checkMove = false;
bool loop = false;
while(!loop) {
//Used unsigned int here because move.size is also an unsigned integer.
//Keeps warning messages about type mismatches from showing up in the compiler window...
for(unsigned int i = 0; i < choice.size(); i++)
if(choice[i] == 'q' || choice[i] == 'Q') {
move_ = -1;
loop = true; //Just in case
return false;
}
try {
checkMove = CheckMoveFormat(choice);
if(checkMove) {
move_ = ReformatMove(choice);
loop = true;
}
}
catch(Exception &e) {
SoundEngine::GetInstance()->PlaySoundFromQueue(badMoveErrorSound);
std::cout<<e.what()<<"\n";
std::cout<<"Please re-enter your choice\n";
std::cin>>choice;
}
}
madeMove_ = true;
return true;
}
bool Player::CheckMoveFormat(std::string choice)
{
const std::string toManyCharacters = "Your entry has to many characters, please try again...", toFewCharacters = "Your entry has to few characters, please try again...",
noComma = "Your entry must have a comma in it, please try again...";
const unsigned int sizeOfMove = 3;
const char middleCharacter = ',';
if(choice.size() > sizeOfMove)
throw Exception(err.Move_Format_Invalid, toManyCharacters);
else if(choice.size() < sizeOfMove)
throw Exception(err.Move_Format_Invalid, toFewCharacters);
else if(choice[1] != middleCharacter)
throw Exception(err.Move_Format_Invalid, noComma);
else
//Move format is good, return true
return true;
}
int Player::ReformatMove(std::string choice)
{
const int hundred = 100, ten = 10;
std::stringstream ss;
ss.clear();
int firstNumber, secondNumber;
ss<<choice[0];
ss>>firstNumber;
ss.clear();
ss<<choice[2];
ss>>secondNumber;
ss.clear();
firstNumber *= hundred;
secondNumber *= ten;
return (firstNumber + secondNumber);
}
const void Player::SetPlayerTextColor() const
{
textColor.SetTextToPlayerColor();
}
bool Player::HasPlayerMadeMove()
{
//Resets madeMove_ value only when its value is true
if(madeMove_) {
madeMove_ = false;
return true;
}
else
return false;
}
const int Player::GetID() const
{
if(playerInitialized_)
return id_;
else {
SoundEngine::GetInstance()->PlaySoundFromQueue(fatalErrorSound);
throw Exception(err.Invalid_Variable_Access, varError);
}
}
const int Player::GetPiece() const
{
if(playerInitialized_)
return playerPiece_;
else {
SoundEngine::GetInstance()->PlaySoundFromQueue(fatalErrorSound);
throw Exception(err.Invalid_Variable_Access, varError);
}
}
const std::string Player::GetName() const
{
if(playerInitialized_)
return name_;
else {
SoundEngine::GetInstance()->PlaySoundFromQueue(fatalErrorSound);
throw Exception(err.Invalid_Variable_Access, varError);
}
}
const int Player::GetScore() const
{
if(playerInitialized_)
return score_;
else {
SoundEngine::GetInstance()->PlaySoundFromQueue(fatalErrorSound);
throw Exception(err.Invalid_Variable_Access, varError);
}
}
const int Player::GetMove() const
{
if(playerInitialized_)
return move_;
else {
SoundEngine::GetInstance()->PlaySoundFromQueue(fatalErrorSound);
throw Exception(err.Invalid_Variable_Access, varError);
}
}
const bool Player::DidPlayerWin() const
{
if(playerInitialized_)
return win_;
else {
SoundEngine::GetInstance()->PlaySoundFromQueue(fatalErrorSound);
throw Exception(err.Invalid_Variable_Access, varError);
}
}
const int Player::GetPlayerTextColor() const
{
if(playerInitialized_)
return (textColor.GetTextColor());
else {
SoundEngine::GetInstance()->PlaySoundFromQueue(fatalErrorSound);
throw Exception(err.Invalid_Variable_Access, varError);
}
}
void Player::SetPlayerWon()
{
win_ = true;
}