-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboard.cpp
More file actions
106 lines (93 loc) · 2.48 KB
/
board.cpp
File metadata and controls
106 lines (93 loc) · 2.48 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
#include "board.h"
#include <iostream>
Board::Board(){
//Initialize the board
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 7; j++) {
board[i][j] = 0;
}
}
}
void Board::printBoard(){
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 7; j++) {
std::cout << board[i][j] << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;
}
bool Board::makeMove(int col, int player) {
// Find the lowest empty row in the column and play the move there
for (int i = 5; i >= 0; i--) {
if (board[i][col] == 0) {
board[i][col] = player;
return true;
}
}
return false;
}
// Check if player won
bool Board::checkWin(int player) {
// Check horizontal
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 4; j++) {
if (board[i][j] == player &&
board[i][j+1] == player &&
board[i][j+2] == player &&
board[i][j+3] == player) {
return true;
}
}
}
// Check vertical
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 7; j++) {
if (board[i][j] == player &&
board[i+1][j] == player &&
board[i+2][j] == player &&
board[i+3][j] == player) {
return true;
}
}
}
// Check diagonal (top-left to bottom-right)
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
if (board[i][j] == player &&
board[i+1][j+1] == player &&
board[i+2][j+2] == player &&
board[i+3][j+3] == player) {
return true;
}
}
}
// Check diagonal (top-right to bottom-left)
for (int i = 0; i < 3; i++) {
for (int j = 3; j < 7; j++) {
if (board[i][j] == player &&
board[i+1][j-1] == player &&
board[i+2][j-2] == player &&
board[i+3][j-3] == player) {
return true;
}
}
}
return false;
}
bool Board::isFull(){
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 7; j++) {
if(board[i][j] == 0){
return false;
}
}
}
return true;
}
int Board::getCell(int row, int col) {
return board[row][col];
}
void Board::setCell(int row, int col, int value) {
board[row][col] = value;
}