-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.cpp
139 lines (117 loc) · 2.83 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
#include <iostream>
using namespace std;
void docs();
class game
{
public:
char arr[7][7];
void board(char [7][7]);
int check(char [7][7], int);
int draw(char [7][7]);
};
int main()
{
int a,b;
game g;
docs();
for (int i=0; i<7; i++) {
cout << "\t";
for (int j=0; j<7; j++) {
g.arr[i][j] = '*'; //Initialize the whole Board
cout << g.arr[i][j] << " ";
}
cout << endl;
}
cout << endl;
while (true) {
cout << "Player 1's Turn: ";
error1:
cin >> a >> b;
if (g.arr[a][b] == '*') {
g.arr[a][b] = 'X';
g.board(g.arr);
if (g.check(g.arr, 'X') == true) {
cout << "Player 1 Won! \n\n";
break;
}
if (g.draw(g.arr) == true) {
cout << "Its a Draw! \n\n";
break;
}
}
else {
cout << "Its already Occupied, Pick Somewhere else: ";
goto error1;
}
cout << "Player 2's Turn: ";
error2:
cin >> a >> b;
if (g.arr[a][b] == '*') {
g.arr[a][b] = 'O';
g.board(g.arr);
if (g.check(g.arr, 'O') == true) {
cout << "Player 2 Won! \n\n";
break;
}
if (g.draw(g.arr) == true) {
cout << "Its a Draw! \n\n";
break;
}
}
else {
cout << "Its already Occupied, Pick Somewhere else: ";
goto error2;
}
}
return 0;
}
void docs()
{
cout << endl
<< "Welcome to the Connect-4 Board-Game. A Two-Player Turn-based Game. \n"
<< " The Player 1 is assigned 'X'. \n"
<< " The Player 2 is assigned 'O'. \n"
<< endl;
cout << "Pick the Specified Place according to the Index of the Row and Column. \n"
<< "The First Player to Occupy any of the Three consecutive Places, would Win. \n"
<< "You can Occupy either Horizontally, Vertically, or Diagonally. \n"
<< "The Override is not Allowed. \n"
<< endl;
}
void game::board(char arr[7][7])
{
cout << endl;
for (int i=0; i<7; i++) {
cout << "\t";
for (int j=0; j<7; j++)
cout << arr[i][j] << " ";
cout << endl;
}
cout << endl << endl;
}
int game::check(char arr[7][7], int p)
{
for (int i=0; i<7; i++) {
for (int j=0; j<7; j++) {
if (arr[i][j] == p && arr[i][j+1] == p && arr[i][j+2] == p && arr[i][j+3] == p)
return true; //Horizonatlly
else if (arr[i][j] == p && arr[i + 1][j] == p && arr[i + 2][j] == p && arr[i + 3][j] == p)
return true; //Vertically
else if (arr[i][j] == p && arr[i + 1][j + 1] == p && arr[i + 2][j + 2] == p && arr[i + 3][j + 3] == p)
return true; //Diagonally-Right
else if (arr[i][j] == p && arr[i + 1][j - 1] == p && arr[i + 2][j - 2] == p && arr[i + 3][j - 3] == p)
return true; //Diagonally-Left
}
}
return false;
}
int game::draw(char arr[7][7])
{
for (int i=0; i<7; i++) {
for (int j=0; j<7; j++) {
if (arr[i][j] == '*')
return false;
}
}
return true;
}