This repository was archived by the owner on Jul 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
150 lines (123 loc) · 3.75 KB
/
main.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
var fen = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'
// rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
var worker = new Worker('worker.js')
var time_interval
var $timewhite = $('#timewhite')
var $timeblack = $('#timeblack')
var mouse_square
var board = null
var chess = new Chess(fen)
var whiteSquareGrey = '#a9a9a9'
var blackSquareGrey = '#696969'
function removeGreySquares () {
$('#board-container .square-55d63').css('background', '')
}
function greySquare (square) {
var $square = $('#board-container .square-' + square)
var background = whiteSquareGrey
if ($square.hasClass('black-3c85d')) {
background = blackSquareGrey
}
$square.css('background', background)
}
function onDragStart (source, piece) {
if ((chess.turn() === 'w' && piece.search(/^w/) === -1) ||
(chess.turn() === 'b' && piece.search(/^b/) === -1)) {
return false
}
removeGreySquares()
var moves = chess.moves({
square: source,
verbose: true
})
if (moves.length === 0) return
greySquare(source)
for (var i = 0; i < moves.length; i++) {
greySquare(moves[i].to)
}
if (chess.game_over()) return false
}
function onDrop (source, target) {
var move = chess.move({
from: source,
to: target,
promotion: 'q'
})
source_square = source
if (move === null) return 'snapback'
worker.postMessage({ type: 'Move', move, settings });
if (!settings.game_started) {
settings.game_started = true
time_interval = setInterval(() => {
chess.turn() == 'w' ? settings.time_white -= 100 : settings.time_black -= 100
$timewhite.html(ms_to_timer(settings.time_white))
$timeblack.html(ms_to_timer(settings.time_black))
if (settings.time_white <= 0) {
timeout(false)
}
if (settings.time_black <= 0) {
timeout(true)
}
}, 100)
}
}
function onMouseoverSquare (square, piece) {
mouse_square = square
}
function onSnapEnd(source, target) {
removeGreySquares()
}
var config = {
draggable: true,
position: fen,
orientation: 'white',
onDragStart: onDragStart,
onDrop: onDrop,
onMouseoverSquare: onMouseoverSquare,
onSnapEnd: onSnapEnd
}
board = Chessboard('board-container', config)
var data
var settings = {
depth: 3,
time_white: 60000 * 10,
time_black: 60000 * 10,
game_started: false
}
function timeout(winner) {
winner ? alert('Game over, won by time!') : alert('Game over, lost by time!')
settings.game_started = false
clearInterval(time_interval)
commandSetup()
}
var commandSetup = function() {
chess.load(fen)
board.position(chess.fen())
worker.postMessage({ type: 'Setup', fen: chess.fen() })
settings = {
depth: 3,
time_white: 60000 * 10,
time_black: 60000 * 10,
game_started: false
}
}
commandSetup()
worker.onmessage = function(message) {
data = message.data
eval('command' + data.type + '()')
}
var commandMove = function() {
chess.move(data.move)
board.position(chess.fen())
if (chess.game_over()) {
if (chess.in_stalemate()) { alert('Draw, stalemate!');settings.game_started = false;clearInterval(time_interval);commandSetup() }
if (chess.in_threefold_repetition()) { alert('Draw, threefold repetition!');settings.game_started = false;clearInterval(time_interval);commandSetup() }
if (chess.insufficient_material()) { alert('Draw, insufficient material!');settings.game_started = false;clearInterval(time_interval);commandSetup() }
if (chess.in_checkmate()) { alert(chess.turn() == 'w' ? 'Checkmate, computer wins' : 'Checkmate, you win');settings.game_started = false;clearInterval(time_interval);commandSetup() }
}
}
function ms_to_timer(ms) {
var minutes = Math.floor(ms / 60000)
var seconds = ((ms % 60000) / 1000).toFixed(0)
return minutes + ":" + (seconds < 10 ? '0' : '') + seconds;
}