-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
205 lines (192 loc) · 8.3 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
<!Doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Knight Moves</title>
<link rel="stylesheet" href="css/chessboard-1.0.0.min.css">
<link rel="stylesheet" href="css/style.css">
<script src="js/jquery-3.5.1.min.js"></script>
<script src="js/chessboard-1.0.0.min.js"></script>
<script src="js/game.js"></script>
<script src="js/utils.js"></script>
</head>
<body>
<div style="margin: auto; display: table;">
<h2 class="title">Knight and Queen Puzzle</h2>
<p class="description">Puzzle inspired by <a href="https://www.youtube.com/watch?v=SrQlpY_eGYU">Ben Finegold</a>.</p>
<div id="container">
<div id="main">
<div id="game">
<div id="board" style="width: 600px"></div>
<div id="settings">
<table>
<tr>
<td>
<div class="checkbox-wrapper">
<input id="randomize" type="checkbox" class="switch">
<br>
<label for="randomize">Randomize</label>
</div>
</td>
<td> <button id="reset">Reset</button> </td>
<td> <button id="hint">Hint</button> </td>
<td> <button id="toggleEditor">Board Editor</button> </td>
</tr>
</table>
</div>
</div>
<div id="editor" style="display:none">
<div id="editBoard" style="width:600px"></div>
<button id="editSubmit">submit</button>
<button id="editClear">clear</button>
<button id="editCancel">cancel</button>
</div>
</div>
<div id="side">
<div id="message"></div>
<hr>
<div id="progress">
<span class="remaining"></span>
</div>
<div id="history"></div>
</div>
</div>
<div id="footer">
Created by <a href="https://github.com/243f/knightmoves">243f</a>.
</div>
</div>
<script src="js/puzzle.js"></script>
<script type="text/javascript">
const $game = $("#game");
const $editor = $("#editor");
let startingPosition = {'d5': 'bQ', 'h8': 'wN'};
const url = new URL(document.URL);
const fen = url.searchParams.get('fen');
if (fen) {
startingPosition = ChessBoard.fenToObj(fen);
}
const config = {
draggable: true,
sparePieces: true,
dropOffBoard: "trash",
onDragStart: function (source, piece, position, orientation) {
if (piece[0] === 'w') {
if (piece[1] === 'K' || piece[1] === 'Q' || piece[1] === 'R' || piece[1] === 'B')
return false;
}
return true;
},
onDrop: function (source, target, piece, position) {
if (source === "spare" && piece === "wN") {
const l = Object.values(position).filter(x=>x==="wN").length;
if (l>1)
return 'snapback';
}
},
position: startingPosition
};
const board = new ChessBoard("editBoard", config);
$("#toggleEditor").click(()=> {
const hidden = $game.css("display") === "none";
const boardVisibility = hidden ? "block" : "none";
const editorVisibility = hidden ? "none" : "block";
$game.css("display", boardVisibility);
$editor.css("display", editorVisibility);
});
$("#editClear").click(() => {
board.position({'h8': 'wN'});
});
$("#editCancel").click(() => {
$game.css("display", "block");
$editor.css("display", "none");
});
$("#editSubmit").click(() => {
const url = new URL(document.location);
url.searchParams.delete('fen');
url.searchParams.append('fen', board.fen());
document.location = url.toString();
});
</script>
<script type="text/javascript">
function message(s) {
$("#message").html(`<p>${s}</p>`);
}
function renderHistory(history) {
const $history = $("#history");
let html = "<table>";
html += "<thead><tr>";
html += "<th></th>";
html += "<th>moves</th>";
html += "<th>best</th>";
html += "<th>time</th>";
html += "<th>target</th>";
html += "<th>hint</th>";
html += "</tr></thead>";
html += "<tbody>";
history.forEach(x=>{
html += "<tr>";
html += "<td>";
if (x.path.length === x.best_path.length) {
html += "⭐";
} else if (x.path.length === x.best_path.length+2) {
html += "✅";
}
html += "</td>";
html += `<td title="${x.path.join('->')}">${x.path.length-1}</td>`;
html += `<td title="${x.best_path.join('->')}">${x.best_path.length-1}</td>`;
html += `<td>${(x.time/1000).toFixed(1)}</td>`;
html += `<td>${x.target}</td>`;
html += "<td>";
html += x.usedHint ? 'used hint' : '';
html += "</td>";
html += "</tr>";
});
html += "</tbody>";
html += "</table>";
$history.html(html);
}
function restart() {
let startingPosition = {'d5': 'bQ', 'h8': 'wN'};
const $progress = $('#progress');
const url = new URL(document.URL);
const fen = url.searchParams.get('fen');
if (fen) {
startingPosition = ChessBoard.fenToObj(fen);
}
const config = {
startingPosition: startingPosition,
randomize: $("#randomize").prop("checked"),
onTargetReached: (state, history) => {
if (state.path.length === state.best_path.length) {
message(`⭐ Perfect!<br> ${state.time/1000} seconds`);
} else if (state.path.length <= state.best_path.length+2) {
message(`✅ Excellent!<br> ${state.time/1000} seconds`);
} else {
message(`✅ Good!<br> ${state.time/1000} seconds`);
}
/* update progress */
const remaining = puzzle.remaining().length+1;
$progress.find('.remaining').text(remaining+' remaining.');
renderHistory(history);
},
onComplete: (history) => {
const time = history.map(x=>x.time).reduce((a,b)=>a+b)/1000;
const minutes = Math.floor(time/60);
const seconds = (time-60*minutes).toFixed(1);
$progress.find('.remaining').text('0 remaining.');
message(`${minutes} minutes ${seconds} seconds`);
}
};
puzzle.restart(config);
}
function hint() {
puzzle.hint(true);
}
const puzzle = new Puzzle("#board");
$("#reset").click(restart);
$("#hint").click(hint);
restart();
message("Good luck!");
</script>
</body>
</html>