-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.js
253 lines (211 loc) · 8.15 KB
/
Board.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
const startBoard = game => {
const board = document.getElementById('board');
const squares = board.querySelectorAll('.square');
const whiteSematary = document.getElementById('whiteSematary');
const blackSematary = document.getElementById('blackSematary');
const turnSign = document.getElementById('turn');
let clickedPieceName;
/*
//重置棋盘,遍历所有的棋盘方格,将内容清空,并根据当前游戏状态中的棋子位置信息将棋子重新放置到相应的方格上
const resetBoard = () => {
for (const square of squares) {
square.innerHTML = '';
}
for (const piece of game.pieces) {
const square = document.getElementById(piece.position);
square.innerHTML = `<img class="piece ${piece.rank}" id="${piece.name}" src="img/${piece.color}-${piece.rank}.png">`
}
}
resetBoard();
*/
const resetBoard = () => {
for (const square of squares) {
square.innerHTML = '';
}
for (const piece of game.pieces) {
const square = document.getElementById(piece.position);
square.innerHTML = `<img class="piece ${piece.rank}" id="${piece.name}" src="./img/${piece.color}-${piece.rank}.png">`;
}
}
resetBoard();
// 设置允许移动的方格函数,并为其周围附上阴影
const setAllowedSquares = (pieceImg) => {
clickedPieceName = pieceImg.id;
const allowedMoves = game.getPieceAllowedMoves(clickedPieceName);
if (allowedMoves) {
const clickedSquare = pieceImg.parentNode;
clickedSquare.classList.add('clicked-square');
allowedMoves.forEach( allowedMove => {
if (document.contains(document.getElementById(allowedMove))) {
document.getElementById(allowedMove).classList.add('allowed');
}
});
}
else{
clearSquares();
}
}
//清除方格的样式函数。移除之前设置的允许移动的方格的CSS类,并将之前点击过的方格的CSS类也移除
const clearSquares = () => {
const allowedSquares = board.querySelectorAll('.allowed');
allowedSquares.forEach( allowedSquare => allowedSquare.classList.remove('allowed') );
const cllickedSquare = document.getElementsByClassName('clicked-square')[0];
if (cllickedSquare) {
cllickedSquare.classList.remove('clicked-square');
}
}
//移动棋子函数点击一个方格时,该函数会获取该方格的位置,
//并根据位置信息判断是否存在棋子。如果存在棋子且棋子颜色与当前轮到的玩家颜色相同
function movePiece(square) {
const position = square.getAttribute('id');
const existedPiece = game.getPieceByPos(position);
if (existedPiece && existedPiece.color === game.turn) {
const pieceImg = document.getElementById(existedPiece.name);
clearSquares();
return setAllowedSquares(pieceImg);
}
// game.movePiece(clickedPieceName, position);
AIturn =game.movePiece(clickedPieceName, position);
//if(startzeroClicked && game.turn === 'black' &&AIturn){
if( game.turn === 'black'&&startzeroClicked){
ai.foolMove();
/*
const pieceImg = document.getElementById(existedPiece.name);
clearSquares();
return setAllowedSquares(pieceImg);
*/
}
//if(startzeroClicked && game.turn === 'black' &&AIturn){
if ( game.turn === 'black'&&startonetClicked){
ai.geniusMove();
/*
const pieceImg = document.getElementById(existedPiece.name);
clearSquares();
return setAllowedSquares(pieceImg);
*/
}
}
//遍历所有的棋盘方格,并为每个方格添加点击、拖拽等事件监听器。当玩家点击一个方格或在一个方格上拖放时,会调用movePiece函数
squares.forEach( square => {
square.addEventListener('click', function () {
if (game.turn === 'white') {
movePiece(this);
}});
square.addEventListener("dragover", function(event){
event.preventDefault();
});
square.addEventListener("drop", function () {
movePiece(this);
});
});
//为棋子图片添加点击事件
pieces.forEach( piece => {
const pieceImg = document.getElementById(piece.name);
pieceImg.addEventListener("drop", function () {
const square = document.getElementById(piece.position);
movePiece(square);
});
});
document.querySelectorAll('img.piece').forEach( pieceImg => {
pieceImg.addEventListener("dragstart", function(event) {
event.stopPropagation();
event.dataTransfer.setData("text", event.target.id);
clearSquares();
setAllowedSquares(event.target)
});
pieceImg.addEventListener("drop", function(event) {
event.stopPropagation();
clearSquares();
setAllowedSquares(event.target)
});
});
game.on('pieceMove', piece => {
const square = document.getElementById(piece.position)
square.append( document.getElementById(piece.name) );
clearSquares();
});
game.on('turnChange', turn => {
turnSign.innerHTML = turn === 'white' ? "White's 回合" : "Black's 回合";
});
game.on('promotion', queen => {
const square = document.getElementById(queen.position);
square.innerHTML = `<img class="piece queen" id="${queen.name}" src="img/${queen.color}Queen.png">`;
})
game.on('kill', piece => {
const pieceImg = document.getElementById(piece.name);
pieceImg.parentNode.removeChild(pieceImg);
pieceImg.className = '';
const sematary = piece.color === 'white' ? whiteSematary : blackSematary;
sematary.querySelector('.'+piece.rank).append(pieceImg);
});
game.on('checkMate', color => {
const endScene = document.getElementById('endscene');
endScene.getElementsByClassName('winning-sign')[0].innerHTML = color + ' Wins';
endScene.classList.add('show');
})
}
const pieces = [
new Rook(11, 'whiteRook1'),
new Knight(12, 'whiteKnight1'),
new Bishop(13, 'whiteBishop1'),
new Queen(14, 'whiteQueen'),
new King(15, 'whiteKing'),
new Bishop(16, 'whiteBishop2'),
new Knight(17, 'whiteKnight2'),
new Rook(18, 'whiteRook2'),
new Pawn(21, 'whitePawn1'),
new Pawn(22, 'whitePawn2'),
new Pawn(23, 'whitePawn3'),
new Pawn(24, 'whitePawn4'),
new Pawn(25, 'whitePawn5'),
new Pawn(26, 'whitePawn6'),
new Pawn(27, 'whitePawn7'),
new Pawn(28, 'whitePawn8'),
new Pawn(71, 'blackPawn1'),
new Pawn(72, 'blackPawn2'),
new Pawn(73, 'blackPawn3'),
new Pawn(74, 'blackPawn4'),
new Pawn(75, 'blackPawn5'),
new Pawn(76, 'blackPawn6'),
new Pawn(77, 'blackPawn7'),
new Pawn(78, 'blackPawn8'),
new Rook(81, 'blackRook1'),
new Knight(82, 'blackKnight1'),
new Bishop(83, 'blackBishop1'),
new Queen(84, 'blackQueen'),
new King(85, 'blackKing'),
new Bishop(86, 'blackBishop2'),
new Knight(87, 'blackKnight2'),
new Rook(88, 'blackRook2')
];
const game = new Game(pieces);
startBoard(game);
let startzero = document.getElementById('the-zero');
let startzeroClicked = false;
startzero.addEventListener('click', function() {
startzeroClicked = true;
});
let startone = document.getElementById('the-one');
let startonetClicked = false;
startone.addEventListener('click', function() {
startonetClicked = true;
});
const ai = new aiplayer(game, 'black');
/*
function movePiece(square) {
const position = square.getAttribute('id');
const existedPiece = game.getPieceByPos(position);
if (existedPiece && existedPiece.color === game.turn) {
const pieceImg = document.getElementById(existedPiece.name);
clearSquares();
return setAllowedSquares(pieceImg);
}
game.movePiece(clickedPieceName, position);
const AIturn = game.movePiece(clickedPieceName, position);
if (startzeroClicked && game.turn === 'black' && AIturn) {
setTimeout(() => {
ai.foolMove();
}, 1000);
}
}
*/