-
Notifications
You must be signed in to change notification settings - Fork 0
/
Assignment 2b.cpp
150 lines (126 loc) · 4.23 KB
/
Assignment 2b.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
140
141
142
143
144
145
146
147
148
149
150
/*group members 1.IRADUKUNDA OLIVIER 222005508
2.UMUTESI ROSE 222002123
*/
/*2. Create a C++ program to implement the Power4 board game in the console
(https://en.wikipedia.org/wiki/Connect_Four)*/
#include <iostream>
#include <vector>
using namespace std;
const int ROWS = 6;
const int COLS = 7;
const char PLAYER1 = 'A';
const char PLAYER2 = 'B';
// Function to initialize the board
void initializeBoard(vector<vector<char> >& board) {
board = vector<vector<char> >(ROWS, vector<char>(COLS, ' '));
}
// Function to display the board
void displayBoard(const vector<vector<char> >& board) {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
cout << "| " << board[i][j] << " ";
}
cout << "|" << endl;
}
cout << "-----------------------------" << endl;
}
// Function to check if the move is valid
bool isValidMove(const vector<vector<char> >& board, int col) {
return col >= 0 && col < COLS && board[0][col] == ' ';
}
// Function to make a move
void makeMove(vector<vector<char> >& board, int col, char currentPlayer) {
for (int i = ROWS - 1; i >= 0; i--) {
if (board[i][col] == ' ') {
board[i][col] = currentPlayer;
break;
}
}
}
// Function to check for a winer
bool checkWin(const vector<vector<char> >& board, char currentPlayer) {
// Check for horizontal win
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS - 3; j++) {
if (board[i][j] == currentPlayer &&
board[i][j + 1] == currentPlayer &&
board[i][j + 2] == currentPlayer &&
board[i][j + 3] == currentPlayer) {
return true;
}
}
}
// Check for vertical winer
for (int i = 0; i < ROWS - 3; i++) {
for (int j = 0; j < COLS; j++) {
if (board[i][j] == currentPlayer &&
board[i + 1][j] == currentPlayer &&
board[i + 2][j] == currentPlayer &&
board[i + 3][j] == currentPlayer) {
return true;
}
}
}
// Check for diagonal winer (top-left to bottom-right)
for (int i = 0; i < ROWS - 3; i++) {
for (int j = 0; j < COLS - 3; j++) {
if (board[i][j] == currentPlayer &&
board[i + 1][j + 1] == currentPlayer &&
board[i + 2][j + 2] == currentPlayer &&
board[i + 3][j + 3] == currentPlayer) {
return true;
}
}
}
// Check for diagonal win (bottom-left to top-right)
for (int i = 3; i < ROWS; i++) {
for (int j = 0; j < COLS - 3; j++) {
if (board[i][j] == currentPlayer &&
board[i - 1][j + 1] == currentPlayer &&
board[i - 2][j + 2] == currentPlayer &&
board[i - 3][j + 3] == currentPlayer) {
return true;
}
}
}
return false;
}
// Function to check for a tie
bool checkTie(int moves) {
return moves == ROWS * COLS;
}
// Function to switch players
char switchPlayer(char currentPlayer) {
return (currentPlayer == PLAYER1) ? PLAYER2 : PLAYER1;
}
int main() {
vector<vector<char> > board;
initializeBoard(board);
int moves = 0;
char current_player = PLAYER1;
bool game_over = false;
while (!game_over) {
displayBoard(board);
int col;
do {
cout << "PLAYER " << current_player << ": ENTER IN COLUMN (1-7): ";
cin >> col;
col--;
} while (!isValidMove(board, col));
makeMove(board, col, current_player);
if (checkWin(board, current_player)) {
displayBoard(board);
cout << "CONGRATULATION PLAYER " << current_player << " , WINS!" << endl;
game_over = true;
}
moves++;
if (checkTie(moves)) {
displayBoard(board);
cout << "It's a tie!" << endl;
game_over = true;
}
// Switch player for the next turn
current_player = switchPlayer(current_player);
}
return 0;
}