forked from donbasta/halma-AI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
322 lines (284 loc) · 14.9 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Halma</title>
<link rel="stylesheet" href="style/style.css">
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css"
/>
</head>
<body>
<div class="select-game-modal">
<div class="select-game">
<h3>Game Mode</h3>
<form class="radio-container">
<h4>B - Size : </h4>
<div id="size-selection">
<input type="radio" name="b-size" id="b-8" value="8" checked="checked">
<label for="b-8">8</label>
<input type="radio" name="b-size" id="b-10" value="10">
<label for="b-10">10</label>
<input type="radio" name="b-size" id="b-16" value="16">
<label for="b-16">16</label>
</div>
</form>
<form class="radio-container">
<h4>T - Limit (in seconds): </h4>
<input type="number" name="t-limit" id="t-limit" value="10" min="1">
</form>
<form class="radio-container">
<h4>Select Player Color : </h4>
<div id="player-selection">
<input type="radio" name="h-player" id="h-merah" value="1" checked="checked">
<label for="h-merah">Merah</label>
<input type="radio" name="h-player" id="h-hijau" value="0">
<label for="h-hijau">Hijau</label>
<input type="radio" name="h-player" id="h-no-player" value="-1">
<label for="h-hijau">No Player</label>
</div>
</form>
<form class="radio-container">
<h4>Select Bot Type : </h4>
<div id="bot-selection">
<input type="radio" name="bot-type" id="minimax" value="minimax" checked="checked">
<label for="h-merah">Minimax</label>
<input type="radio" name="bot-type" id="minimax-local-search" value="minimax-local-search">
<label for="h-hijau">Minimax + Local Search</label>
</div>
</form>
<form class="radio-container play">
<input type="button" id="play-button" value="Play">
</form>
</div>
</div>
<div class="select-game-modal" style="z-index: 999">
<div class="select-game">
<h1>CONGRATULATIONS</h1>
<p id="player-wins">Player wins!</p>
<p id="bot-move-count">Bot Move Count : </p>
</div>
</div>
<div class="container">
<div class="title">
<h1>Halma</h1>
</div>
<div class="countdown">
<h2><span id="player-turn-indicator">Red</span> Player's turn</h2>
<h2><span id="countdown-timer">0</span> seconds left</h2>
</div>
<div class="content">
<div id="board">
</div>
</div>
<footer>
Created with ❤ by Kelompok EUY
</footer>
</div>
<script src="js/Util.js"></script>
<script src="js/GameState.js"></script>
<script src="js/Minmax.js"></script>
<script>
var forms = null
var bSize = null
var tLImit = null
var playerMode = null
let gameState = null
let userClicked = null
let turn = 1
let botMoveCount = 0
let timeout = null
let interval = null
/* Selected Cell */
// let selectedCell = null
const createBoard = (mat, clickmat, selectedCell, gameState, siz, playerMode, botType) => {
const board = document.getElementById("board")
if(siz == 8) {
board.classList.add("board-8x8")
} else if(siz == 10) {
board.classList.add("board-10x10")
} else {
board.classList.add("board-16x16")
}
for(let i = 0; i < siz; i++) {
for(let j = 0; j < siz; j++) {
let invert = false
if((i + j) % 2 == 1) {
invert = true
}
const cell = createCell(mat[i][j], invert, siz)
const id = "id_" + i.toString(10) + "_" + j.toString(10)
cell.setAttribute("id", id)
board.appendChild(cell)
invert = !invert
cell.addEventListener("click", async (e) => {
if(clickmat[i][j] == 1) {
for(let a = 0; a < siz; a++) {
for(let b = 0; b < siz; b++) {
clickmat[a][b] = 0
}
}
clickmat[i][j] = 3
const overlay = document.createElement("div")
overlay.setAttribute("class", "selected-cell")
cell.appendChild(overlay)
const availablePos = findAvailablePositions(i, j, mat)
availablePos.forEach((value) => {
clickmat[value[0]][value[1]] = 2
let curCell = getCell(value[0], value[1])
const overlay = document.createElement("div")
overlay.setAttribute("class", "avail")
curCell.appendChild(overlay)
})
selectedCell = [i, j, mat[i][j]]
} else if(clickmat[i][j] == 2 || clickmat[i][j] == 3) {
moveToCell(mat, siz, selectedCell[0], selectedCell[1], i, j)
if(clickmat[i][j] == 2) {
mat[i][j] = selectedCell[2]
mat[selectedCell[0]][selectedCell[1]] = 0
}
selectedCell = null
if (clickmat[i][j] !== 3) {
console.log("turn : " + turn.toString())
// console.log(clickmat[i][j])
// cek state terminal
clearTimeout(timeout)
clearInterval(interval)
let terminalStatus = terminalState(gameState)
console.log("botMoveCount : " + botMoveCount)
if(terminalStatus == 1) {
document.getElementsByClassName("select-game-modal")[1].setAttribute("style", "display: block; z-index: 999;")
document.getElementById("player-wins").innerHTML = "Player one wins!"
document.getElementById("bot-move-count").innerHTML = "Bot move count: " + botMoveCount
} else if(terminalStatus == 2) {
document.getElementsByClassName("select-game-modal")[1].setAttribute("style", "display: block; z-index: 999;")
document.getElementById("player-wins").innerHTML = "Player two wins!"
document.getElementById("bot-move-count").innerHTML = "Bot move count: " + botMoveCount
} else {
// kalo tidak terminal lanjut
// await new Promise(r => setTimeout(r, 200))
if (playerMode !== -1) {
if (turn === 1) {
let timer = document.getElementById("countdown-timer")
timer.innerHTML = (tLimit / 1000).toString(10)
interval = setInterval(() => {
let timer = document.getElementById("countdown-timer")
timer.innerHTML = (parseInt(timer.innerHTML) - 1).toString(10)
}, 1000)
timeout = setTimeout(() => {
turn = turn ^ 1
botMove(gameState, botType)
}, tLimit)
} else {
botMove(gameState, botType)
botMoveCount++
}
} else {
gameState.player = turn + 1
if (turn === 1) {
botMove(gameState, "minimax")
//botMove(gameState, "minimax-local-search")
botMoveCount++
} else {
botMove(gameState, "minimax")
//botMove(gameState, "minimax-local-search")
botMoveCount++
}
}
/*if(playerMode === -1) {
for(let a = 0; a < siz; a++) {
for(let b = 0; b < siz; b++) {
if(mat[a][b] > 0) {
clickmat[a][b] = 1
}
}
}
} else {
if((playerMode == 1 && turn == 0) || (playerMode == 0 && turn == 0)){
for(let a = 0; a < siz; a++) {
for(let b = 0; b < siz; b++) {
if(mat[a][b] == 2) {
clickmat[a][b] = 1
}
}
}
} else {
for(let a = 0; a < siz; a++) {
for(let b = 0; b < siz; b++) {
if(mat[a][b] == 1) {
clickmat[a][b] = 1
}
}
}
}
} */
turn = (turn ^ 1);
if(playerMode === 1){
document.getElementById("player-turn-indicator").innerText = turn === 0 ? "Red" : "Green"
} else {
document.getElementById("player-turn-indicator").innerText = turn === 1 ? "Red" : "Green"
}
}
}
for(let a = 0; a < siz; a++) {
for(let b = 0; b < siz; b++) {
if(mat[a][b] > 0) {
clickmat[a][b] = 1
}
}
}
document.querySelectorAll('.avail').forEach((node) => {
node.remove()
})
// await new Promise(r => setTimeout(r, 1000))
}
})
}
}
}
playButton = document.getElementById("play-button").addEventListener('click', () => {
forms = document.getElementsByClassName("radio-container")
bSize = Array.from(document.getElementById("size-selection").childNodes).filter((child) => child.checked)[0].value
tLimit = parseInt(document.getElementById("t-limit").value) * 1000
playerMode = parseInt(Array.from(document.getElementById("player-selection").childNodes).filter((child) => child.checked)[0].value)
botType = Array.from(document.getElementById("bot-selection").childNodes).filter((child) => child.checked)[0].value
// console.log(playerMode)
botTurn = playerMode === 0 || playerMode === -1 ? 1 : 2
console.log(botTurn)
gameState = new GameState(null, bSize, botTurn)
// botMove(gameState, botType)
createBoard(gameState.board, gameState.clickState, gameState.selectedCell, gameState, bSize, playerMode, botType)
if (playerMode === 0 || playerMode === 1) {
if(playerMode === 1){
document.getElementById("player-turn-indicator").innerText = "Red"
} else {
document.getElementById("player-turn-indicator").innerText = "Green"
}
turn = 0
let timer = document.getElementById("countdown-timer")
timer.innerHTML = (tLimit/1000).toString(10)
interval = setInterval(() => {
let timer = document.getElementById("countdown-timer")
timer.innerHTML = (parseInt(timer.innerHTML) - 1).toString(10)
}, 1000)
timeout = setTimeout(() => {
turn = turn ^ 1
botMove(gameState, botType)
}, tLimit)
//botMove(gameState, botType)
}
if (playerMode === -1) {
botMove(gameState, "minimax")
// botMove(gameState, "minimax-local-search")
}
// console.log('udah')
document.getElementsByClassName("select-game-modal")[0].setAttribute("style", "display: none;")
document.getElementsByClassName("select-game-modal")[1].setAttribute("style", "display: none;")
})
</script>
</body>
<script>
</script>
</html>