forked from denialromeo/4462-chess-problems
/
display.js
136 lines (127 loc) · 5.15 KB
/
display.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
const Chess = require("chess.js")
const URI = require("urijs")
const $ = require("jquery")
const { ChessBoard } = require("./chessboard/chessboard.js")
const { problems } = require("./problems.json")
const random = require("./random.js")
const { enableScroll, disableScroll } = require("./toggle-scrollbar.js")
let url_parameters = new URI(window.location.href).search(true)
function unhighlight() {
$("#board .square-55d63").css("background", "")
}
function highlight(square) {
const squareEl = $("#board .square-" + square)
const background = squareEl.hasClass("black-3c85d") ? "#696969" : "#a9a9a9"
squareEl.css("background", background)
}
function parse_move(move) {
let [source, target] = move.split("-")
let promotion = target.length == 2 ? "q" : target[2]
target = target.slice(0,2)
return [source, target, promotion]
}
var game
var correct_moves
function make_move() {
let [source, target, promotion] = parse_move(correct_moves[0])
game.move({"from": source, "to": target, "promotion": promotion})
board.move(source + "-" + target)
correct_moves.shift()
}
const board = ChessBoard("board", {
draggable: true,
dropOffBoard: "snapback",
onDragStart: function(src, tgt, position, orientation) { disableScroll() },
onDrop: function(src, tgt) {
enableScroll()
if (game.in_checkmate()) {
return "snapback"
}
let [source, target, promotion] = parse_move(correct_moves[0])
if (correct_moves.length == 1) {
const sim_game = new Chess(game.fen())
sim_game.move({"from": src, "to": tgt, "promotion": promotion})
if (!sim_game.in_checkmate()) {
return "snapback"
}
else {
game.move({"from": src, "to": tgt, "promotion": promotion})
correct_moves.shift()
}
}
else {
if (src !== source || tgt !== target) {
return "snapback"
}
game.move({"from": source, "to": target, "promotion": promotion})
correct_moves.shift()
setTimeout(make_move, 500)
}
if (game.in_checkmate()) {
$("#hint-btn").css("display", "none")
$("#next-btn").css("display", "")
document.querySelector("#next-btn").onclick = function() {
const current_problem_id = document.querySelector("#problem-num").innerHTML;
if ("o" in url_parameters && current_problem_id != 4462) {
next(problems[current_problem_id])
pushstate()
} else {
next()
if (history && history.replaceState && "id" in url_parameters) {
delete url_parameters["id"]
history.replaceState(url_parameters, "", new URI(window.location.href).search(url_parameters).toString())
}
}
}
document.querySelector("#problem-title").innerHTML = document.querySelector("#problem-title").innerHTML.split("-")[0] + " - Solved!"
}
},
onMoveEnd: function() {
board.position(game.fen())
},
onSnapEnd: function() { board.position(game.fen()); unhighlight() }
})
function next(problem=random.choice(problems), useAnimation=true) {
$("#next-btn").css("display", "none")
$("#hint-btn").css("display", "")
const problem_type = "Checkm" + problem.type.slice(1) + " Move" + (problem.type.endsWith("One") ? "" : "s")
var problem_title = `${problem_type} - ${problem.first}`
document.title = `#${problem.problemid}`
if ("o" in url_parameters) { problem_title = `#${problem.problemid} ${problem_title}`}
document.querySelector("#problem-title").innerHTML = problem_title
document.querySelector("#problem-num").innerHTML = `${problem.problemid}`
document.querySelector("#problem-link").href = "o" in url_parameters ? `?o&id=${problem.problemid}` : `?id=${problem.problemid}`
game = new Chess(problem.fen)
board.position(problem.fen, useAnimation)
correct_moves = problem.moves.split(";")
document.querySelector("#hint-btn").onclick = function() {
let [source, target, promotion] = parse_move(correct_moves[0])
highlight(source)
highlight(target)
}
}
function init() {
const problem = ("id" in url_parameters && url_parameters["id"] <= 4462 && url_parameters["id"] > 0) ? problems[url_parameters["id"] - 1] : random.choice(problems)
next(problem)
pushstate()
}
function pushstate() {
if (history && history.pushState && "o" in url_parameters) {
const current_problem_id = document.querySelector("#problem-num").innerHTML
url_parameters["id"] = current_problem_id
if (history.state && history.state["id"] == current_problem_id) {
return
}
history.pushState(url_parameters, "", new URI(window.location.href).search(url_parameters).toString())
}
}
window.onpopstate = function(event) {
if (event.state) {
url_parameters["id"] = event.state["id"]
next(problems[url_parameters["id"] - 1], false)
}
}
// Exports
Object.assign(exports, {
init: init
})