-
Notifications
You must be signed in to change notification settings - Fork 0
/
chessquiz.js
109 lines (95 loc) · 3.2 KB
/
chessquiz.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
var count=0;
var fontSize=36;
function supports_html5_storage() {
try {
return 'localStorage' in window && window['localStorage'] !== null && localStorage !== undefined;
} catch (e) {
return false;
}
}
if(supports_html5_storage()) {
if(localStorage.lastBoard)
count = parseInt(localStorage.lastBoard);
if(localStorage.fontSize)
fontSize = parseInt(localStorage.fontSize);
function localStore(key, val) {
localStorage.setItem(key, val);
}
} else
function localStore(key, val) {
}
var chesstag='<chess cstyle="font-size:'+fontSize.toString()+'pt;" mode="color" dir="up">';
var border=false;
function changeFont(sz)
{
fontSize = fontSize + sz;
localStore('fontSize', fontSize);
chesstag = chesstag.replace(/(.*font-size:)\d+(pt.*)/,'$1'+fontSize+'$2');
ShowBoard();
}
function changeBorder(p)
{
var s;
if (p) {
s = /(.*)>/.exec(chesstag);
chesstag = s[1] + ' border="a1">';
} else {
s = /(.*) border="a1"(.*)/.exec(chesstag);
if (s)
chesstag = s[1] + s[2];
}
ShowBoard();
}
function changeMode(mode)
{
var s = /(.*")(bw|color)+(".*)/.exec(chesstag);
chesstag = s[1] + mode + s[3];
ShowBoard();
}
function setDir(dir)
{
var s = /(.*dir=")(up|down)+(".*)/.exec(chesstag);
chesstag = s[1] + dir + s[3];
}
function changeDir(dir)
{
setDir(dir)
ShowBoard();
}
function ShowBoard() {
var msg;
if (problems[count].search(' w ') != -1) {
setDir('up');
msg = "White";
} else {
setDir('down');
msg = "Black";
}
document.getElementById('board').innerHTML=chesstag+problems[count].replace(/(.*) (b|w) .*/,'$1')+'</chess>';
parseChess();
document.getElementById('msg').innerHTML=msg+" to play and win! ("+(count+1)+' of '+problems.length+')';
}
function GotoGame(n) {
count = n;
if (count < 0)
count = problems.length-1;
else if (count >= problems.length)
count = 0;
localStore('lastBoard', count.toString());
ShowBoard();
}
function RandomGame() {
GotoGame(Math.floor(Math.random() * (problems.length)));
}
// Window.onload = function {
document.addEventListener("DOMContentLoaded", function() {
//console.log("showboard");
document.getElementById("bfont+").addEventListener("click", () => {changeFont(4);});
document.getElementById("bfont-").addEventListener("click", () => {changeFont(-4);});
document.getElementById("button0").addEventListener("click", () => {GotoGame(0);});
document.getElementById("button1").addEventListener("click", () => {GotoGame(count-1);});
document.getElementById("button2").addEventListener("click", () => {GotoGame(count+1);});
document.getElementById("button3").addEventListener("click", () => {GotoGame(problems.length-1);});
document.getElementById("button4").addEventListener("click", RandomGame);
ShowBoard();
});