-
Notifications
You must be signed in to change notification settings - Fork 0
/
ai.js
186 lines (167 loc) · 5.21 KB
/
ai.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
function randomChoice(board){
let available = []
for (i = 0; i < COLUMNS; i++){
if (board[i].length < 6){
available.push(i);
}
}
return random(available);
return 6;
}
function minimax(board, depth, isMaximisingPlayer, alpha, beta, first = true){
// console.log(board, depth, isMaximisingPlayer)
let available = []
for (i = 0; i < COLUMNS; i++){
if (board[i].length < 6){
available.push(i);
}
}
let thisTurn = isMaximisingPlayer ? "red": "blue"
if (depth == 0 || checkBoard(board)){
let score = evaluateBoard(board, thisTurn)
// print(thisTurn, score,"score")
return score
}
if (isMaximisingPlayer){
let maxEval = -Infinity;
let bestColumn;
for (let column of available){
// console.log(column, thisTurn)
board[column].push(thisTurn)
let eval = minimax(board, depth - 1, false, alpha, beta, false)
maxEval = max(eval, maxEval);
if (eval == maxEval){
bestColumn = column
}
alpha = max(alpha, eval);
if (beta <= alpha){
break;
}
board[column].pop()
}
// print(maxEval,bestColumn, "max")
if (first){
return bestColumn
} else {
return maxEval
}
}
else{
let minEval = Infinity;
let bestMinColumn;
for (let column of available){
// console.log(column, thisTurn)
board[column].push(thisTurn)
let eval = minimax(board, depth - 1, true,alpha, beta, false)
minEval = min(eval, minEval);
if (eval == minEval){
bestMinColumn = column
}
beta = min(beta, eval);
if (beta <= alpha){
break;
}
board[column].pop()
}
// print(minEval, bestMinColumn, "min")
if (first){
return bestMinColumn;
} else {
return minEval
}
}
}
function evaluateBoard(board, turn){
let out = 0;
for (let column = 0; column < board.length; column++){
for (let piece = 0; piece < board[column].length; piece++){
out += evaluatePiece(board, column, piece, turn);
}
}
// console.log(out)
return out;
}
function evaluatePiece(board, x, y , nextMove){
let color = board[x][y] == "red" ? 1 : -1;
let oneMultiplier = 1;
let twoMultiplier = 2;
let threeMultiplier = 5;
let winMultiplier = 10000
let possible = [1, 1, 1, 1, 1, 1, 1];
let underneath = [false, false, false, false, false, false, false]
if (x <= COLUMNS - 4){
for (let i = 1; i <= 3; i++){
if (directionalCheck(board, x, y, possible, x + i, y, 0)){
underneath[0] = true;
}
if (directionalCheck(board, x, y, possible, x + i, y + i, 1)){
underneath[1] = true;
}
if (directionalCheck(board, x, y, possible, x + i, y - i, 2)){
underneath[2] = true;
}
}
} else {
possible = [0, 0, 0, 1, 1, 1, 1];
}
if (y <= ROWS - 4){
for (let i = 1; i <= 3; i++){
if (directionalCheck(board, x, y, possible, x, y + i, 3)){
underneath[3] = true;
}
}
} else {
possible[3] = 0;
}
if (x >=3){
for (let i = 1; i <= 3; i++){
if (directionalCheck(board, x, y, possible, x - i, y, 4)){
underneath[4] = true;
}
if (directionalCheck(board, x, y, possible, x - i, y + i, 5)){
underneath[5] = true;
}
if (directionalCheck(board, x, y, possible, x - i, y - i, 6)){
underneath[6] = true;
}
}
} else {
for (let i =4; i <= 6; i++){
possible[i] = 0
}
}
let out = 0;
for (let [index, number] of possible.entries()){
if (number === 1){
out += oneMultiplier * color;
} else if (number === 2){
out += twoMultiplier * color;
} else if (number === 3){
if (underneath[index] && board[x][y] == nextMove){
out += winMultiplier * color / 4
} else {
out += threeMultiplier * color;
}
} else if (number === 4){
out += winMultiplier * color
}
}
// print(x, y, possible, underneath, nextMove, out)
return out
}
function directionalCheck(board, x, y, possible, dx, dy, checkNo){
if (possible[checkNo]){
if (!board[dx][dy] && !outside(dx, dy)){
return board[dx].length === dy;
} else if (board[dx][dy] !== board[x][y] || outside(dx, dy)){
possible[checkNo] = 0
} else {
possible[checkNo]++
}
}
}
function outside(x, y){
let evalX = x < 0 || x >= COLUMNS;
let evalY = y < 0 || y >= ROWS;
return evalX || evalY
}