-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwo-Player-KKT-Game.cpp
More file actions
275 lines (229 loc) · 7.43 KB
/
Copy pathTwo-Player-KKT-Game.cpp
File metadata and controls
275 lines (229 loc) · 7.43 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
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
#include<iostream>
#include<string>
#include<utility>
#include<cstdlib>
#include<ctime>
using namespace std;
const int SIDE=3;
const char PLAYER1='X' , PLAYER2='O';
string nameP1,nameP2;
//Functions Declarations
void beginKKT1(char board[][SIDE],char turn);
void playKKT(char board[][SIDE],char &whoseTurn);
char toss();
void showInstructions();
void showBoard(char board[][SIDE]);
void initialise(char board[][SIDE]);
bool rowCrossed(char board[][SIDE]);
bool columnCrossed(char board[][SIDE]);
bool isStalemate(char board[][SIDE]);
bool gameOver(char board[][SIDE]);
void declareWinner(char whoseTurn);
bool verifyInput(char board[][SIDE],int x1,int y1,int x2,int y2,char PLAYER);
//Main Code
int main(){
//Here Goes Main function of Person to Person (P2P) game
cout << "Enter name of first Player: ";
cin >>nameP1;
cout << "Enter name of Second Player: ";
cin >>nameP2;
// Let's do toss
showInstructions();
char turn=toss();
//Place Pieces X's & O's in board
cout<<"Place you'r 3 pieces in 3 desired locations,\n Good Luck guys! :)\n";
char board[SIDE][SIDE];
initialise(board);
//to put 3 O's & X's into board
beginKKT1(board,turn);
//Real game start here
playKKT(board,turn);
//declare who's the winner nameP1 or nameP2
declareWinner(turn);
return (0);
}
//Function Definitions
void beginKKT1(char board[][SIDE],char turn)
{
//Initially the board is empty
string curName;
if(turn==PLAYER1){
curName=nameP1;
}else{
curName=nameP2;
}
int start=6;//3 pieces each , player1 & player2
int a;
while(start--){
again:
cout<<curName<<" Enter the cell NUMBER in range [1-9] to place '"<<turn<<"' :\n";
cin>>a;
a=a-1;
if(a<0 || a>8 || board[a/3][a%3]!=' '){
cout<<"Lol!, that cell location is not in range oR its already filled. Try again:\n";
cin.clear();
cin.ignore(10000,'\n');
goto again;
}else{
board[a/3][a%3]=turn;
}
if(turn==PLAYER1){
curName=nameP2;
turn=PLAYER2;
}else{
curName=nameP1;
turn=PLAYER1;
}
showBoard(board);
}
return ;
}
void playKKT(char board[][SIDE],char &whoseTurn)
{
//
int a,b;
int x1,y1,x2,y2;
string curName;
bool wrongInput;
// Keep playing till the game is over
while (gameOver(board)==false){
if(whoseTurn==PLAYER1){
curName=nameP1;
}else{
curName=nameP2;
}
wrongInput=true;
while(wrongInput){
cout<<curName<<" Enter the respective initial & final cell NUMBERs in range [1-9] to move piece '"<<whoseTurn<<"' :";
cin>>a>>b;
a--;b--;
if(a>=0 && b>=0 && b<9 && a<9){
y1=a/3;
x1=a%3;
x2=b%3;
y2=b/3;
if(verifyInput(board,x1,y1,x2,y2,whoseTurn)){
swap(board[y2][x2],board[y1][x1]);
if(whoseTurn==PLAYER1){
whoseTurn=PLAYER2;
}else{
whoseTurn=PLAYER1;
}
}else{
cout << "Oops, thats an illegal move , try again\n";
cin.clear();//clearing invalid stream of input
cin.ignore(10000,'\n');//without these , program will skip cin statement in next iteration to avoid this , use cin.ignore() function
continue;
}
}else{
cout<<"Oops ,that moves are out of range \n";
cin.clear();
cin.ignore(10000,'\n');
continue;
}
showBoard(board);
//let's check , will this move make stalemate
if(isStalemate(board)==true){
cout<<curName<<" You stalemate Opponent So Play again ,Opponent don't have any legal moves rn :):\n";
continue;
}
wrongInput=false;
}
}
return ;
}
char toss(){
srand(time(0));//to avoid getting same value
int toss=rand()%10;//generates random value of range 0 to 9 (each has 50% chance of win)
if(toss>4){
swap(nameP1,nameP2);
}
cout<<nameP1 << " has won the toss,Game will start with you,Take three 'X' pieces"<< endl;
cout<<nameP2<< " here you go , take three 'O' pieces\n\n";
return PLAYER1;
}
void showBoard(char board[][SIDE])
{
//Printing game status
printf("\n\n");
printf("\t\t\t %c | %c | %c \n",board[0][0],board[0][1],board[0][2]);
printf("\t\t\t------------\n");
printf("\t\t\t %c | %c | %c \n",board[1][0],board[1][1],board[1][2]);
printf("\t\t\t------------\n");
printf("\t\t\t %c | %c | %c \n\n",board[2][0],board[2][1],board[2][2]);
return;
}
void initialise(char board[][SIDE]){
//creating matrix(array) to play game
for(int i=0;i<SIDE;i++){
for(int j=0;j<SIDE;j++){
board[i][j]=' ';
}
}
showBoard(board);
return ;
}
bool gameOver(char board[][SIDE]){
//check if gameover or not
return (rowCrossed(board) || columnCrossed(board));
}
bool rowCrossed(char board[][SIDE]){
for (int i = 0; i < SIDE; i++) {
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ')
return (true);
}
return (false);
}
bool columnCrossed(char board[][SIDE]){
for (int i = 0; i < SIDE; i++) {
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
return (true);
}
return (false);
}
bool isStalemate(char board[][SIDE]){
//stalemate means , one player can't move any of his pieces
if(board[0][0]==board[1][1] && board[1][1]==board[2][2]){
if(board[0][1]==board[0][2] && board[0][2]==board[1][2]){
return true;
}
}else if(board[0][2]==board[1][1] && board[1][1]==board[2][0]){
if(board[0][0]==board[0][1] && board[0][0]==board[1][0]){
return true;
}
}
return (false);
}
void declareWinner(char whoseTurn){
if (whoseTurn == PLAYER1)
cout <<nameP2<< " has won the game\n";
else
cout <<nameP1<< " has won the game\n";
return;
}
bool verifyInput(char board[][SIDE],int x1,int y1,int x2,int y2,char PLAYER){
//Lets check if given move is valid or not
if(board[y2][x2]==' ' && board[y1][x1]==PLAYER){
if(((x1==x2) && abs(y2-y1)==1) || (y1==y2 && abs(x2-x1)==1)){
return true;
}else{
return false;
}
}
return (false);
}
void showInstructions()
{
printf("\n===\t===\t===\t===\t===\t===\t===\t===\t===\t===\t===\t\n\n");
printf("\t\t\t Kit-Kat-Toe Instructions \n\n");
printf("->You can fill 1st 3 pieces in desired location and later you just have move the pieces\n");
printf("->Choose a cell numbered from 1 to 9 as below and play\n");
printf("->whoever first makes either row crossed or column crossed with their 3 pawns, he/she is the winner\n");
printf("-> Apart from player names , all inputs should be in intergers from 1 - 9\n\n");
printf("\t\t\t 1 | 2 | 3 \n");
printf("\t\t\t------------\n");
printf("\t\t\t 4 | 5 | 6 \n");
printf("\t\t\t------------\n");
printf("\t\t\t 7 | 8 | 9 \n\n");
printf("\n===\t===\t===\t===\t===\t===\t===\t===\t===\t===\t===\t\n\n");
}