-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
340 lines (303 loc) · 11.2 KB
/
index.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
document.addEventListener('DOMContentLoaded', ()=>{
let gameboard = [[],[],[],[],[],[],[]]
let playersTurn = true || false
let gameover = false
let whoWon = 1 || 2
let initalizeGameboard = (g) => {
for(let j=0; j< g.length; j++){
for(let i=0;i<7;i++){
g[j].push(0)
}
}
return g
}
let addPiece = (turn, board, location) => {
spot = convertToNumber(turn)
//read 2d array bottom up
for(let i = board.length-1; i > -1; i--){
//if 0 place turn
if (board[i][location] === 0){
board[i][location] = spot
gameover = winCondition(board, spot)
return board
}
}
return -1
//if no -1 then return error?
}
let flip = () => {
if(playersTurn) playersTurn = false
else playersTurn = true
}
let convertToNumber = (turn) =>{
if(turn === true) return 1
else return 2
}
let coinflip = () =>{
if(getRandomInt(100)%2===0) return true
else return false
}
function getRandomInt(max) {
return Math.floor(Math.random() * max);
}
let evaluateGameboard = (gameboard, piece) =>{
let score = 0
otherpiece = 0
if(piece === 2) otherpiece = 1
else otherpiece = 2
function Counter(segment, piece){
let number = 0
for(let i of segment){
if(i === piece) number++
}
return number
}
if(Counter(gameboard, piece) === 4) score = score + 100
else if(Counter(gameboard, piece) === 3 && Counter(gameboard, 0) === 1) score = score + 5
else if(Counter(gameboard, piece) === 2 && Counter(gameboard, 0) === 2) score = score + 2
if(Counter(gameboard, otherpiece) === 3 && Counter(gameboard, 0) === 1) score = score - 4
return score
}
let scorePosition = (board, piece) =>{
let score = 0
for(i=0;i<board.length-3;i++){
for(j=0;j<board[i].length;j++){
score = score + evaluateGameboard([board[i][j],board[i+1][j],board[i+2][j],board[i+3][j]], piece)
}
}
for(i=0;i<board.length;i++){
for(j=0;j<board[i].length-3;j++){
score = score + evaluateGameboard([board[i][j],board[i][j+1],board[i][j+2],board[i][j+3]], piece)
}
}
for(i=0;i<board.length-3;i++){
for(j=0;j<board[i].length-3;j++){
score = score + evaluateGameboard([board[i][j],board[i+1][j+1],board[i+2][j+2],board[i+3][j+3]], piece)
}
}
for(i=3;i<board.length;i++){
for(j=0;j<board[i].length-3;j++){
score = score + evaluateGameboard([board[i][j],board[i-1][j+1],board[i-2][j+2],board[i-3][j+3]], piece)
}
}
return score
}
let winCondition = (board, piece) =>{
//use try except indexError instead
for(let i=0;i<board.length-3;i++){
for(let j=0;j<board[i].length;j++){
if(board[i][j] === piece && board[i+1][j] === piece && board[i+2][j] === piece && board[i+3][j] === piece) {
whoWon = piece
return true
}
}
}
for(let i=0;i<board.length;i++){
for(let j=0;j<board[i].length-3;j++){
if(board[i][j] === piece && board[i][j+1] === piece && board[i][j+2] === piece && board[i][j+3] === piece){
whoWon = piece
return true
}
}
}
for(let i=0;i<board.length-3;i++){
for(let j=0;j<board[i].length-3;j++){
if(board[i][j] === piece && board[i+1][j+1] === piece && board[i+2][j+2] === piece && board[i+3][j+3] === piece) {//
whoWon = piece
return true
}
}
}
for(let i=3;i<board.length;i++){
for(let j=0;j<board[i].length-3;j++){
if(board[i][j] === piece && board[i-1][j+1] === piece && board[i-2][j+2] === piece && board[i-3][j+3] === piece){
whoWon = piece
return true
}
}
}
}
let getValidLocations = (board) => {
let validlocations = []
for(column in board){//iterate through columns
for(let i = board.length-1; i > -1; i--){ //look through rows bottom up
if (board[i][column] === 0){ //location is valid
validlocations.push(parseInt(column))//add location to the list
break
}
}
}
return validlocations
}
let isValidLocation = (board, col) =>{
//read 2d array bottom up
for(let i = board.length-1; i > -1; i--){
if (board[i][col] === 0)return true
}
return false
}
let isTeminalNode = (board) => {
return winCondition(board, convertToNumber(true)) || winCondition(board,convertToNumber(false)) || getValidLocations.length === 0
}
let deepCopy = (board) => {
return JSON.parse(JSON.stringify(board))
}
let minimax = (originalgameboard, depth, maximizingPlayer)=>{
let board = deepCopy(originalgameboard)
let validLocations = getValidLocations(board)
let is_terminal = isTeminalNode(board)
if(depth === 0 || is_terminal){
if(is_terminal){
if(winCondition(board,convertToNumber(maximizingPlayer)))return [null, Infinity]
else if(winCondition(board,convertToNumber(!maximizingPlayer)))return [null, -Infinity]
else return [null, 0]
}
else{
return [null, scorePosition(board, !maximizingPlayer)]
}
}
if(maximizingPlayer){
let value = -Infinity
let column = validLocations[getRandomInt(validLocations.length)]
for (col in validLocations){
let copy = deepCopy(board)
addPiece(maximizingPlayer,board, column)
newScore = minimax(copy, depth-1, !maximizingPlayer)[1]
if(newScore>value){
value = newScore
column = col
}
}
return [column, value]
}
else{
let value = Infinity
let column = validLocations[getRandomInt(validLocations.length)]
for (col in validLocations){
let copy = deepCopy(board)
addPiece(maximizingPlayer,board, column)
newScore = minimax(copy, depth-1, maximizingPlayer)[1]
if(newScore<value){
value = newScore
column = col
}
}
return [column, value]
}
}
let minimaxAB = (originalgameboard, depth, alpha, beta, maximizingPlayer)=>{
let board = deepCopy(originalgameboard)
let validLocations = getValidLocations(board)
let is_terminal = isTeminalNode(board)
if(depth === 0 || is_terminal){
if(is_terminal){
if(winCondition(board,convertToNumber(maximizingPlayer)))return [null, Infinity]
else if(winCondition(board,convertToNumber(!maximizingPlayer)))return [null, -Infinity]
else return [null, 0]
}
else{
return [null, scorePosition(board, !maximizingPlayer)]
}
}
if(maximizingPlayer){
let value = -Infinity
let column = validLocations[getRandomInt(validLocations.length)]
for (col in validLocations){
let copy = deepCopy(board)
addPiece(maximizingPlayer,board, column)
newScore = minimax(copy, depth-1, !maximizingPlayer)[1]
if(newScore>value){
value = newScore
column = col
}
alpha = Math.max(alpha, value)
if(alpha>=beta) break
}
return [column, value]
}
else{
let value = Infinity
let column = validLocations[getRandomInt(validLocations.length)]
for (col in validLocations){
let copy = deepCopy(board)
addPiece(maximizingPlayer,board, column)
newScore = minimax(copy, depth-1, maximizingPlayer)[1]
if(newScore<value){
value = newScore
column = col
}
beta = Math.min(beta, value)
if(alpha>=beta) break
}
return [column, value]
}
}
let magic = []
playersTurn = coinflip(playersTurn)
gameboard = initalizeGameboard(gameboard)
magic.push(deepCopy(gameboard))
let turn = 0
while(!gameover){
if(turn%2 == 0 && !gameover){
maxplayer = minimax(gameboard,6,true)
if(isValidLocation(gameboard, maxplayer[0])){
addPiece(true, gameboard, maxplayer[0])
if(winCondition(gameboard, maxplayer[0])){
gameover = true
}
magic.push(deepCopy(gameboard))//deep copy
turn++
}
}
else if(turn%2 == 1 && !gameover){
minplayer = minimax(gameboard,6,false)
if(isValidLocation(gameboard, minplayer[0])){
addPiece(false, gameboard, minplayer[0])
if(winCondition(gameboard, minplayer[0])){
gameover = true
}
magic.push(deepCopy(gameboard))//deep copy
turn++
}
}
}
console.log(magic)
//front end stuff...
let settingUpGameboard = (board) =>{
let viewGameboard = document.getElementsByClassName("gameboard")[0]
for(let i=0; i<board.length; i++){
let row = document.createElement('div')
row.classList.add('row')
viewGameboard.appendChild(row)
}
let rows = document.getElementsByClassName('row')
for(const row of rows){
for(let i = 0; i<board[0].length;i++){
let slot = document.createElement('div')
slot.classList.add('slot')
row.appendChild(slot)
}
}
}
let addToBoard = (board) =>{
let r = document.getElementsByClassName('row')
let slots = document.getElementsByClassName('slot')
let counter = 0
for(let i =0;i<board.length;i++){
for(let j = 0; j<board[0].length;j++){
if(board[i][j] === 0){
slots[counter].innerHTML = `🤡`
}
else if(board[i][j] === 1){
slots[counter].innerHTML =`😌`
}
else if(board[i][j] === 2){
slots[counter].innerHTML = `😠`
}
counter++
}
}
}
settingUpGameboard(gameboard)
addToBoard(gameboard)
})