-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.cpp
295 lines (271 loc) · 9.98 KB
/
game.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
/* Written by: Mikael Jakhelln, HIOA-studentnr: s169961
Tic-Tac-Toe, n pieces on a row to win, with n sized gameboard.
*/
#include "gameboard.cpp"
#include "score.cpp"
#include "player.cpp"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <cstdlib> //is this the same as stdlib?
#include <ctime>//for random and wait
using namespace std;
namespace tictactoe{
class game{
private:
bool willwait; //used to check if it should wait for user keypress, when playing bot vs bot
/* // old intinput
int intinput() //gets an int from the user
{
int x;
cin >> x;
return x;
}
*/
int intinput()
{
//better inputmethod, almost done
int returnint = 0;
int intmax = 2147483647;
bool isint = false;
char input[10]; //get input into array
cin >> input;
int inputcount = 0;
for(int i = 0; i<10; i++) //loop trough array
{
if(!isdigit(input[i]))//char isnt a digit
{
break; //if a character in input isnt a digit
}
else //char is a digit
{
isint = true;
}
inputcount++;
}
while(isint != true) //input isn't an int, ask user to type it in again
{
cout << "thats not a number, try again:";
cin >> input;
for(int i = 0; i<10; i++)
{
if(!isdigit(input[i]))//char isnt a digit
{
break; //if a character in input isnt a digit
}
else //char is a digit
{
isint = true;
}
inputcount++;
}
}
if(isint == false) //if it still isnt an int, just exit
{
cout << "YOU BROKE SOMETHING! terminating program" << endl;
exit(EXIT_FAILURE);
}
returnint = atoi(input);
while(returnint > intmax || returnint < 0)
{
cout <<"that number is way to big! try again:";
returnint = intinput();
}
while(returnint > intmax || returnint < 0)
{
cout <<"the number is too small! try again:";
returnint = intinput();
}
return returnint;
}
void wait() //waiting for 1 second, so the user can follow bot vs bot games
{
if(willwait == true) //waits only if willwait is true
{
//cout <<"waiting so the USER! can see whats happening!"<<endl<<endl;
clock_t temp;
temp = clock () + 1 * CLOCKS_PER_SEC ;
while (clock() < temp) {}
}
}
public:
game()
{
//game runs in here, runs while anyonewonyet returns 0, also lets the controlling entity choose to play again
//first choose gameboard size
cout << "choose gameboard size, from 3 and up:";
int size=0;
size = intinput();
while(size < 3)
{
cout << "that is too small, choose another boardsize:" << endl;
size = intinput();
}
//initialize gameboard, and make objects for players
gameboard board = gameboard(size); //this is the gameboard, initialized to the chosen size.
score score1 = score(); //check score with this with
player * player1;//two players, will be initialized once the almighty user has chosen number of human players
player * player2;
//choose who to start
srand(time(NULL));//seed random from system time
int whostarts = (rand() %2)+1; //pick random number between 1 and 2
//cout << "DEBUG: game: Whostarts: Player" << whostarts << endl;
int humans = 0; //initializes humans to 0 by default
cout << "Choose number of humans players: ";
humans = intinput();
while(humans >2 || humans < 0)
{
cout << "invalid number of human players, choose again" << endl;
humans = intinput();
}
cout << "Human players: " << humans << endl;
//init players
if(humans == 0) //two bots, MACHINE SHOWDOWN FTW!
{
willwait = true; //wait between turns
if(whostarts == 1) //player 1 starts
{
player1 = new bot(1, false); //bot(players piece = X, ishuman = false)
player2 = new bot(2, false); //bot(players piece = O, ishuman = false)
}
else
{
player1 = new bot(2, false); //bot(players piece = X, ishuman = false)
player2 = new bot(1, false); //bot(players piece = O, ishuman = false)
}
//ask user for bot difficulty
cout << "set difficulty for bot " << player1->getplayerpiece() << ":";
bool p1diff = player1->setdifficulty(intinput());
cout << "DEBUG: difficulty successfully set?:"<<p1diff <<endl;
cout << "set difficulty for bot " << player2->getplayerpiece() << ":";
bool p2diff = player2->setdifficulty(intinput());
cout << "DEBUG: difficulty successfully set?:"<<p2diff <<endl;
}
else if(humans == 1) // human vs computer
{
willwait = false; //dont wait between turns
if(whostarts == 1)//human starts as player X
{
player1 = new human(1, true); //human(players piece = X, ishuman = true)
player2 = new bot(2, false); //bot(players piece = O, ishuman = false)
cout << "set difficulty for bot " << player1->getplayerpiece() << ":";
bool p2diff = player2->setdifficulty(intinput());
}
else //human is player O
{
player1 = new human(2, true); //human(players piece = X, ishuman = true)
player2 = new bot(1, false); //bot(players piece = O, ishuman = false)
cout << "set difficulty for bot " << player2->getplayerpiece() << ":";
bool p2diff = player2->setdifficulty(intinput());
}
}
else if(humans == 2)
{
willwait = false; //dont wait for humans
if(whostarts == 1)
{
player1 = new human(1, true); //two Humans
player2 = new human(2, true); //human(players piece = X, ishuman = true)
}
else
{
player1 = new human(2, true); //two Humans
player2 = new human(1, true); //human(players piece = X, ishuman = true)
}
}
else //somehow humans are fucked up
{
cout << "OMG OMG OMG, somehow you managed to break something" << endl;
exit(EXIT_FAILURE); //exits program
}
//prints out who starts
//cout << "Whostarts: Player" << whostarts << endl;
int x = player1->getplayernumber();
char y = player1->getplayerpiece();
int z = player1->getdifficulty();
cout << "player1: playernumber,playerpiece,difficulty: " << x << ",[" << y << "],"<< z <<endl;
x = player2->getplayernumber();
y = player2->getplayerpiece();
z = player2->getdifficulty();
cout << "player2: playernumber,playerpiece,difficulty: " << x << ",["<< y << "]," << z << endl;
board.printboardinfo(); //prints out the gameboard, with some additional info explaining what rows and columns are
//the actual game loop stuff starts here, sorry
wait(); //might wait here
int gameover = 0; //used to check the gamestate, at the start of the game, nobody has won yet.
while(gameover == 0)
{
//the starting player X's turn
bool Xmove = false;
cout << "player X, make a move!" << endl;
while(Xmove != true)
{
player * thisplayer;
vector<int> coords(2); coords[0] = size+1; coords[1] = size+1; //temp vector for return values of player
//initd to something outside the gameboard, so it definately wont work
if(player1->getplayernumber() == 1)
{
thisplayer = player1;
coords = thisplayer->getcoords(size, board.board); //gets some coords from the bot
//cout << "DEBUG: game(): player1: playerpiece,row,col are: "<< thisplayer->getplayerpiece() << "," << coords[0] << "," << coords[1]<<endl;
Xmove = board.makemove(thisplayer->getplayernumber(), coords[0], coords[1]);
}
else //player2.playernumber == 2
{
thisplayer = player2;
coords = thisplayer->getcoords(size, board.board); //gets some coords from the bot
//cout << "DEBUG: game(): player2: playerpiece,row,col are: "<< thisplayer->getplayerpiece() << "," << coords[0] << "," << coords[1]<<endl;
Xmove = board.makemove(thisplayer->getplayernumber(), coords[0], coords[1]);
}
if(Xmove == false) cout << "DEBUG: game: Xmove == false" << endl; //the bot doesnt need to see this
}
//end of playerX's turn
board.printboard(); //prints the gameboard
wait(); //might wait here
//check score
gameover = score1.anybodywinyet(size, board.board);
if(gameover != 0) break; //if somebody won, or its a draw, break this while loop
//player(O)'s turn
bool Omove = false;
while(Omove != true)
{
player * thisplayer;
vector<int> coords(2); coords[0] = size+1; coords[1] = size+1; //initialized to something outside the gameboard..
if(player1->getplayernumber() == 2)
{
thisplayer = player1;
coords = thisplayer->getcoords(size, board.board); //gets some coords from the bot
//cout << "DEBUG: game(): player1: playerpiece,row,col are: "<< thisplayer->getplayerpiece() << "," << coords[0] << "," << coords[1]<<endl;
Omove = board.makemove(thisplayer->getplayernumber(), coords[0], coords[1]);
}
else //player2.playernumber == 2
{
thisplayer = player2;
coords = thisplayer->getcoords(size, board.board); //gets some coords from the bot
//cout << "DEBUG: game(): player1: playerpiece,row,col are: "<< thisplayer->getplayerpiece() << "," << coords[0] << "," << coords[1]<<endl;
Omove = board.makemove(thisplayer->getplayernumber(), coords[0], coords[1]);
}
if(Omove == false) cout << "DEBUG: game: Omove == false" << endl; //the bot doesnt need to see this
}
//end of playerO's turn
wait(); //might wait here
board.printboard(); //prints the gameboard
gameover = score1.anybodywinyet(size, board.board);
if(gameover != 0) break; //if somebody won, or its a draw, break this while loop
}
gameover = score1.anybodywinyet(size, board.board); //the result of the game
if(gameover == 1){cout << "player X won" << endl;}//duh!
if(gameover == 2){cout << "player O won" << endl;}//duh!
if(gameover == 3){cout << "its a draw, good job" << endl;}//duh!
//play another game?
cout << "play another game? type y or Y for another game, anything else to exit" << endl;
char again;
cin >> again;
if(again == 'y' || again == 'Y')//reset board, and start the game.
{
game();
}
//debugline end ftw! */
}//end of function game
}; //end of classdefinition game
} //end of namespace tictactoe