-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnect4Game.cpp
More file actions
108 lines (94 loc) · 3.13 KB
/
connect4Game.cpp
File metadata and controls
108 lines (94 loc) · 3.13 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
#include <iostream>
#include <vector>
using namespace std;
const int ROWS = 6;
const int COLS = 7;
const char PLAYER1 = 'X';
const char PLAYER2 = 'O';
void printBoard(const vector< vector<char>>& board) {
cout << "\n";
for (int row = 0; row < ROWS; ++row) {
for (int col = 0; col < COLS; ++col) {
cout << "| " << board[row][col] << " ";
}
cout << "|\n";
}
for (int col = 0; col < COLS; ++col) {
cout << "----";
}
cout << "-\n";
for (int col = 1; col <= COLS; ++col) {
cout << " " << col << " ";
}
cout << "\n\n";
}
bool isValidMove(const vector< vector<char>>& board, int col) {
return col >= 0 && col < COLS && board[0][col] == ' ';
}
bool makeMove( vector< vector<char>>& board, int col, char player) {
for (int row = ROWS - 1; row >= 0; --row) {
if (board[row][col] == ' ') {
board[row][col] = player;
return true;
}
}
return false;
}
bool checkWin(const vector< vector<char>>& board, char player) {
// Check horizontal, vertical, and two diagonal directions
for (int row = 0; row < ROWS; ++row) {
for (int col = 0; col < COLS; ++col) {
if (board[row][col] == player) {
if (col + 3 < COLS && board[row][col + 1] == player &&
board[row][col + 2] == player && board[row][col + 3] == player)
return true;
if (row + 3 < ROWS && board[row + 1][col] == player &&
board[row + 2][col] == player && board[row + 3][col] == player)
return true;
if (row + 3 < ROWS && col + 3 < COLS && board[row + 1][col + 1] == player &&
board[row + 2][col + 2] == player && board[row + 3][col + 3] == player)
return true;
if (row + 3 < ROWS && col - 3 >= 0 && board[row + 1][col - 1] == player &&
board[row + 2][col - 2] == player && board[row + 3][col - 3] == player)
return true;
}
}
}
return false;
}
bool isDraw(const vector< vector<char>>& board) {
for (int col = 0; col < COLS; ++col) {
if (board[0][col] == ' ') {
return false;
}
}
return true;
}
int main() {
vector< vector<char>> board(ROWS, vector<char>(COLS, ' '));
char currentPlayer = PLAYER1;
while (true) {
printBoard(board);
int col;
cout << "Player " << currentPlayer << ", enter column (1-" << COLS << "): ";
cin >> col;
col -= 1; // Convert to 0-indexed
if (!isValidMove(board, col)) {
cout << "Invalid move. Try again.\n";
continue;
}
makeMove(board, col, currentPlayer);
if (checkWin(board, currentPlayer)) {
printBoard(board);
cout << "Player " << currentPlayer << " wins!\n";
break;
}
if (isDraw(board)) {
printBoard(board);
cout << "It's a draw!\n";
break;
}
currentPlayer = (currentPlayer == PLAYER1) ? PLAYER2 : PLAYER1;
}
return 0;
}