-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnect4Board.cpp
More file actions
163 lines (142 loc) · 4.41 KB
/
Connect4Board.cpp
File metadata and controls
163 lines (142 loc) · 4.41 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
#include <iostream>
#include <array>
using namespace std;
class Connect4Board {
public:
// default constructor
Connect4Board() {
boardArr = {0, 0, 0, 0, 0, 0, 0, 0, 0};
x_turn = true;
gameOver = false;
winner = -1;
}
private:
array<int, 9> boardArr;
bool x_turn;
int winner;
bool gameOver;
bool outOfBounds(int x, int y) const { return (x < 0 || x > 2 || y < 0 || y > 2); }
bool outOfBounds(int n) const { return (n < 1 || n > 9); }
bool find3Consecutive() {
for (int row = 0; row < 3; row++) {
if ((getPiece(row, 0) == getPiece(row, 1)) && (getPiece(row, 0) == getPiece(row, 2))) {
if (getPiece(row, 0) != 0) {
winner = getPiece(row, 0);
gameOver = true;
return true;
}
}
}
for (int col = 0; col < 3; col++) {
if ((getPiece(0, col) == getPiece(1, col)) && (getPiece(0, col) == getPiece(2, col))) {
if (getPiece(0, col) != 0) {
winner = getPiece(0, col);
gameOver = true;
return true;
}
}
}
if ((getPiece(0, 0) == getPiece(1, 1)) && (getPiece(0, 0) == getPiece(2, 2))) {
if (getPiece(0, 0) != 0) {
winner = getPiece(0, 0);
gameOver = true;
return true;
}
}
if ((getPiece(0, 2) == getPiece(1, 1)) && (getPiece(0, 2) == getPiece(2, 0))) {
if (getPiece(0, 2) != 0) {
winner = getPiece(0, 2);
gameOver = true;
return true;
}
}
return false;
}
public:
bool setPiece(int n, int p) {
if (!outOfBounds(n) && getPiece(n) == 0) {
if ((p == 1) || (p == 2)) {
boardArr[n - 1] = p;
return true;
}
}
return false;
}
bool setPiece(int x, int y, int p) {
if (!outOfBounds(x, y) && getPiece(x, y) == 0) {
if ((p == 1) || (p == 2)) {
boardArr[x + (3 * y)] = p;
return true;
}
}
return false;
}
Piece getPiece(int n) const {
if (outOfBounds(n)) return Invalid;
else return boardArr[n - 1];
}
Piece getPiece(int x, int y) const {
if (outOfBounds(x, y)) return Invalid;
else return boardArr[x + (3 * y)];
}
bool isGameOver() {
if (gameOver || find3Consecutive()) {
return true;
} else {
for (int n = 1; n <= 9; n++) {
if (getPiece(n) == 0) return false;
}
return true;
}
}
Piece getWinner() const { return winner; }
void reset() {
boardArr = {0, 0, 0, 0, 0, 0, 0, 0, 0};
x_turn = true;
gameOver = false;
winner = I;
}
bool isValidMove(int x, int y) const { return getPiece(x, y) == E; }
bool isValidMove(int n) const { return getPiece(n) == E; }
char printPiece(int x, int y) const {
Piece p = getPiece(x, y);
if (p == 1) return 'X';
else if (p == 2) return 'O';
else if (p == -1) return 'I';
else return ' ';
}
void print() const {
// for (int i = 0; i < leftBuffer; i++) cout << " ";
cout << " " << endl;
// for (int i = 0; i < leftBuffer; i++) cout << " ";
cout << " | | " << endl;
// for (int i = 0; i < leftBuffer; i++) cout << " ";
cout << " " << printPiece(0, 0) << " |";
cout << " " << printPiece(1, 0) << " |";
cout << " " << printPiece(2, 0) << " " << endl;
// for (int i = 0; i < leftBuffer; i++) cout << " ";
cout << " | | " << endl;
// for (int i = 0; i < leftBuffer; i++) cout << " ";
cout << "-----+-----+-----" << endl;
// for (int i = 0; i < leftBuffer; i++) cout << " ";
cout << " | | " << endl;
// for (int i = 0; i < leftBuffer; i++) cout << " ";
cout << " " << printPiece(0, 1) << " |";
cout << " " << printPiece(1, 1) << " |";
cout << " " << printPiece(2, 1) << " " << endl;
// for (int i = 0; i < leftBuffer; i++) cout << " ";
cout << " | | " << endl;
// for (int i = 0; i < leftBuffer; i++) cout << " ";
cout << "-----+-----+-----" << endl;
// for (int i = 0; i < leftBuffer; i++) cout << " ";
cout << " | | " << endl;
// for (int i = 0; i < leftBuffer; i++) cout << " ";
cout << " " << printPiece(0, 2) << " |";
cout << " " << printPiece(1, 2) << " |";
cout << " " << printPiece(2, 2) << " " << endl;
// for (int i = 0; i < leftBuffer; i++) cout << " ";
cout << " | | " << endl;
// for (int i = 0; i < leftBuffer; i++) cout << " ";
cout << " " << endl;
}
};