-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect4.js
153 lines (153 loc) · 4.14 KB
/
connect4.js
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
class Connect4 {
constructor(cols, rows, width, height, botPlayer, botDepth) {
this.cols = cols
this.rows = rows
this.width = width
this.height = height
this.rectSize = {
x: this.width / this.cols,
y: this.height / this.rows
}
this.turn = true
this.isOver = false
this.gameoverText = null
this.showGameover = true
// bool, true is p1, false is p2
this.botPlayer = botPlayer
this.botDepth = botDepth
this.board = []
//p1 token is true, p2 token is false
for (let y = 0; y < rows; y++) {
this.board[y] = []
for (let x = 0; x < cols; x++) {
this.board[y][x] = null
}
}
}
inBoard(x, y) {
return 0 <= x && x < this.cols && 0 <= y && y < this.rows
}
lowestAvailableSpace(x) {
for (let y = 0; y < this.rows; y++) {
if (this.board[y][x] != null) {
return y - 1
} else if (y === this.rows - 1) {
return this.rows - 1
}
}
}
moveIsValid(x) {
if (this.board[0][x] === null) {
return true
}
return false
}
move(x) {
this.board[this.lowestAvailableSpace(x)][x] = this.turn
this.turn = !this.turn
}
undoMove(x) {
this.board[this.lowestAvailableSpace(x) + 1][x] = null
this.turn = !this.turn
}
botMove() {
let move = minimax(this.botDepth, this.botDepth, this, this.botPlayer, Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY)[1]
this.move(move)
return move
}
generateMoves() {
let ret = []
for (let x = 0; x < this.cols; x++) {
if (this.moveIsValid(x)) {
ret.push(x)
}
}
return ret
}
over(lastMoveX) {
let center = {
x: lastMoveX,
y: this.lowestAvailableSpace(lastMoveX) + 1
}
for (let i = 0; i < 4; i++) {
if (
this.inBoard(center.x, center.y - i) &&
this.inBoard(center.x, center.y - i + 3) &&
checkLine(this.board[center.y - i][center.x],
this.board[center.y - i + 1][center.x],
this.board[center.y - i + 2][center.x],
this.board[center.y - i + 3][center.x])
) {
return [true, this.board[center.y][center.x], [
[center.x, center.y - i],
[center.x, center.y - i + 1],
[center.x, center.y - i + 2],
[center.x, center.y - i + 3]
]]
}
}
for (let i = 0; i < 4; i++) {
if (
this.inBoard(center.x - i, center.y + i) &&
this.inBoard(center.x - i + 3, center.y + i - 3) &&
checkLine(this.board[center.y + i][center.x - i],
this.board[center.y + i - 1][center.x - i + 1],
this.board[center.y + i - 2][center.x - i + 2],
this.board[center.y + i - 3][center.x - i + 3])
) {
return [true, this.board[center.y][center.x], [
[center.x - i, center.y + i],
[center.x - i + 1, center.y + i - 1],
[center.x - i + 2, center.y + i - 2],
[center.x - i + 3, center.y + i - 3]
]]
}
}
for (let i = 0; i < 4; i++) {
if (
this.inBoard(center.x - i, center.y) &&
this.inBoard(center.x - i + 3, center.y) &&
checkLine(
this.board[center.y][center.x - i],
this.board[center.y][center.x - i + 1],
this.board[center.y][center.x - i + 2],
this.board[center.y][center.x - i + 3]
)
) {
return [true, this.board[center.y][center.x], [
[center.x - i, center.y],
[center.x - i + 1, center.y],
[center.x - i + 2, center.y],
[center.x - i + 3, center.y]
]]
}
}
for (let i = 0; i < 4; i++) {
if (
this.inBoard(center.x - i, center.y - i) &&
this.inBoard(center.x - i + 3, center.y - i + 3) &&
checkLine(
this.board[center.y - i][center.x - i],
this.board[center.y - i + 1][center.x - i + 1],
this.board[center.y - i + 2][center.x - i + 2],
this.board[center.y - i + 3][center.x - i + 3])
) {
return [true, this.board[center.y][center.x], [
[center.x - i, center.y - i],
[center.x - i + 1, center.y - i + 1],
[center.x - i + 2, center.y - i + 2],
[center.x - i + 3, center.y - i + 3]
]]
}
}
for (let row of this.board) {
if (!row.every(x => x != null)) {
return [false, null]
}
}
return [true, null, null]
}
getColumn(mouseX) {
return Math.floor(mouseX / this.rectSize.x)
}
}