-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHard_Prob219.cpp
More file actions
150 lines (129 loc) · 4.85 KB
/
Hard_Prob219.cpp
File metadata and controls
150 lines (129 loc) · 4.85 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
/* Connect 4 is a game where opponents take turns dropping red or black discs into a 7 x 6 vertically suspended grid.
The game ends either when one player creates a line of four consecutive discs of their color (horizontally, vertically, or diagonally), or when there are no more spots left in the grid.
Design and implement Connect 4*/
#include <iostream>
#include <vector>
using namespace std;
const int ROWS = 6;
const int COLUMNS = 7;
class ConnectFour {
public:
ConnectFour() {
board.resize(ROWS, vector<char>(COLUMNS, ' '));
currentPlayer = 'X';
gameOver = false;
}
void play() {
while (!gameOver) {
displayBoard();
int column;
cout << "Player " << currentPlayer << ", enter your move (1-7): ";
cin >> column;
if (isValidMove(column)) {
column--;
move(column);
if (isWin()) {
displayBoard();
cout << "Player " << currentPlayer << " wins!" << endl;
gameOver = true;
} else if (isBoardFull()) {
displayBoard();
cout << "It's a draw!" << endl;
gameOver = true;
} else {
togglePlayer();
}
} else {
cout << "Invalid move. Try again." << endl;
}
}
}
private:
vector<vector<char>> board;
char currentPlayer ;
bool gameOver ;
void displayBoard() {
for (int row=ROWS-1; row>=0; row--) {
cout << "| ";
for (int col=0; col<COLUMNS; col++) {
cout << board[row][col] << " ";
}
cout << "|" << endl;
}
}
bool isValidMove(int col) {
return (col <= COLUMNS && col >= 1 && board[ROWS-1][col-1] == ' ');
}
void move(int column) {
for (int row=0; row<ROWS; row++) {
if (board[row][column] == ' ') {
board[row][column] = currentPlayer;
break;
}
}
}
bool isBoardFull() {
bool isFull = true;
for (int row=0; row<ROWS; row++) {
for (int column=0; column<COLUMNS; column++) {
if (board[row][column] == ' '){
isFull = false;
break;
}
}
}
return isFull;
}
void togglePlayer() {
if (currentPlayer == 'X') {
currentPlayer = 'O';
} else {
currentPlayer = 'X';
}
}
bool isWin() {
// Check horizontal
for (int row = 0; row < ROWS; row++) {
for (int column = 0; column < COLUMNS - 3; column++) {
if (board[row][column] == currentPlayer && board[row][column + 1] == currentPlayer &&
board[row][column + 2] == currentPlayer && board[row][column + 3] == currentPlayer) {
return true;
}
}
}
// Check vertical
for (int row = 0; row < ROWS - 3; row++) {
for (int column = 0; column < COLUMNS; column++) {
if (board[row][column] == currentPlayer && board[row+1][column] == currentPlayer &&
board[row+2][column] == currentPlayer && board[row+3][column] == currentPlayer) {
return true;
}
}
}
// Check diagonal (bottom-right to top-left)
for (int row = 0; row < ROWS - 3; row++) {
for (int col = 0; col < COLUMNS - 3; col++) {
if (board[row][col] == currentPlayer && board[row + 1][col + 1] == currentPlayer &&
board[row + 2][col + 2] == currentPlayer && board[row + 3][col + 3] == currentPlayer) {
return true;
}
}
}
// Check diagonal (bottom-left to top-right)
for (int row = 3; row < ROWS; row++) {
for (int col = 0; col < COLUMNS - 3; col++) {
if (board[row][col] == currentPlayer && board[row - 1][col + 1] == currentPlayer &&
board[row - 2][col + 2] == currentPlayer && board[row - 3][col + 3] == currentPlayer) {
return true;
}
}
}
return false;
}
};
int main(int argc, char *argv[])
{
ConnectFour game;
game.play();
return 0;
}