-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
155 lines (117 loc) · 4.04 KB
/
index.ts
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
import { Game } from "./Game.js"
const icons = [
{
name: "bishop",
white: "https://upload.wikimedia.org/wikipedia/commons/9/9b/Chess_blt60.png",
black: "https://upload.wikimedia.org/wikipedia/commons/8/81/Chess_bdt60.png"
},
{
name: "king",
white: "https://upload.wikimedia.org/wikipedia/commons/3/3b/Chess_klt60.png",
black: "https://upload.wikimedia.org/wikipedia/commons/e/e3/Chess_kdt60.png"
},
{
name: "knight",
white: "https://upload.wikimedia.org/wikipedia/commons/2/28/Chess_nlt60.png",
black: "https://upload.wikimedia.org/wikipedia/commons/f/f1/Chess_ndt60.png"
},
{
name: "pawn",
white: "https://upload.wikimedia.org/wikipedia/commons/0/04/Chess_plt60.png",
black: "https://upload.wikimedia.org/wikipedia/commons/c/cd/Chess_pdt60.png"
},
{
name: "queen",
white: "https://upload.wikimedia.org/wikipedia/commons/4/49/Chess_qlt60.png",
black: "https://upload.wikimedia.org/wikipedia/commons/a/af/Chess_qdt60.png"
},
{
name: "rook",
white: "https://upload.wikimedia.org/wikipedia/commons/5/5c/Chess_rlt60.png",
black: "https://upload.wikimedia.org/wikipedia/commons/a/a0/Chess_rdt60.png"
}
]
const main = document.querySelector('main') as HTMLElement
const turnText = document.querySelector('section h1') as HTMLElement
const turnImgs= document.querySelectorAll('section div img') as NodeListOf<HTMLImageElement>
const turnSection = document.querySelector('.turn') as HTMLElement
const restartSection = document.querySelector('.other') as HTMLElement
const min = document.querySelector('.timer .min') as HTMLElement
const sec = document.querySelector('.timer .sec') as HTMLElement
/* */
const game = new Game(icons, main)
let swapTurn = game.swapTurn()
let turn:'white' | 'black' = swapTurn.next().value
const fields = game.draw_start()
game.addClick(fields, clickHandle)
game.startTimer(min, sec)
/* */
let move:Array<HTMLElement> | null
let currentField:any
restartSection.children[0].addEventListener('click', () => {
for(let x of document.body.children){
if(x.tagName === 'H2') x.remove()
}
main.style.pointerEvents = 'all'
restartSection.style.display = 'none'
turnSection.style.display = 'block'
const ts = turnSection.children[1]
for(let x of ts.children){
if(x.tagName === 'H1'){
x.textContent = 'white'
continue
}
(<HTMLImageElement>x).src = 'https://upload.wikimedia.org/wikipedia/commons/3/3b/Chess_klt60.png'
swapTurn = game.swapTurn()
turn = swapTurn.next().value
game.restartGame()
game.startTimer(min, sec)
}
})
function clickHandle(dataset:{ col:number, row:number }){
const { col, row } = dataset
if(!move || !currentField) {
const moves = game.clickPath(col, row, turn)
if(moves.length){
move = moves[0]
currentField = moves[1]
}
return
}
for(let x of move) {
const col2 = parseInt(x.dataset.col!)
const row2 = parseInt(x.dataset.row!)
if(col === col2 && row === row2) {
turn = swapTurn.next().value
turnText.textContent = turn
changeTurnIcons()
game.movePiece(x, currentField.element)
if(game.isKingDead()){
const t = turn === 'white' ? 'black' : 'white'
displayFinish()
game.finishPopUpText(t, restartSection)
game.stopTimer()
}
}
for(let y of Array.from(x.children)){
if(y.tagName !== 'SPAN') continue
y.remove()
}
if(currentField.element.style.background === 'rgb(14, 201, 151)'){
currentField.element.style.background = currentField.background
}
}
move = null
currentField = null
}
function changeTurnIcons():void{
for(let x of [...turnImgs]){
//@ts-ignore
(<HTMLImageElement>x).src = icons[1][turn]
}
}
function displayFinish():void{
turnSection.style.display = 'none'
main.style.pointerEvents = 'none'
restartSection.style.display = 'flex'
}