-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoly-chess.html
More file actions
325 lines (296 loc) · 10.3 KB
/
poly-chess.html
File metadata and controls
325 lines (296 loc) · 10.3 KB
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<link rel="import" href="../polymer/polymer.html">
<dom-module id="poly-chess">
<template>
<style is="custom-style" type="text/css">
:host {
display: block;
--width: 640px;
--tile-size: 64px;
}
.chessboard {
width: var(--width);
height: var(--width);
margin: 20px;
border: 25px solid #333;
}
.white {
background-color: rgba(255, 255, 255, 0.0);
}
.black {
background-color: #999;
}
.noselect {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.tile{
float: left;
width: 12.5%;
height: 12.5%;
font-size: var(--tile-size);
text-align: center;
display: table-cell;
vertical-align: middle;
cursor: pointer;
}
.ghost-overlay{
text-align: center;
font-size: var(--tile-size);
pointer-events: none;
position: absolute;
}
.hidden{
display:none;
}
</style>
<div id="board" class="chessboard noselect">
<template is='dom-repeat' items='[[board]]'>
<template is='dom-repeat' items='[[item]]'>
<div on-up="onMouseUp" on-down="onMouseDown" data-location$="[[item.location]]" data-type$="[[item.contents.typeName]]" class$="tile [[item.colour]]">[[displayPiece(item)]]</div>
</template>
</template>
<div id="hover" class="white hidden ghost-overlay">[[displayHover(_draggedType)]]</div>
</div>
</template>
<script>
Polymer({
is: 'poly-chess',
properties: {
_initialClick:{
type: Object,
value: null
},
_offsetY: {
type: Number,
value: 0
},
_offsetX: {
type: Number,
value: 0
},
pieceTypes: {
type: Object,
value: {
"WHITE": {
"PAWN": "\u2659",
"CASTLE": "\u2656",
"KNIGHT": "\u2658",
"BISHOP": "\u2657",
"QUEEN": "\u2655",
"KING": "\u2654",
},
"BLACK": {
"PAWN": "\u265F",
"CASTLE": "\u265C",
"KNIGHT": "\u265E",
"BISHOP": "\u265D",
"QUEEN": "\u265B",
"KING": "\u265A"
}
}
},
_columns: {
type: Array,
value: ["a", "b", "c", "d", "e", "f", "g", "h"]
},
_rows: {
type: Array,
value: [1, 2, 3, 4, 5, 6, 7, 8].reverse()
},
edgeLength:{
type:Number,
value: 640
},
_standardBoard: {
type: Array,
value: [
{x: 1, y: 0, type: "PAWN", colour: "BLACK"},
{x: 1, y: 1, type: "PAWN", colour: "BLACK"},
{x: 1, y: 2, type: "PAWN", colour: "BLACK"},
{x: 1, y: 3, type: "PAWN", colour: "BLACK"},
{x: 1, y: 4, type: "PAWN", colour: "BLACK"},
{x: 1, y: 5, type: "PAWN", colour: "BLACK"},
{x: 1, y: 6, type: "PAWN", colour: "BLACK"},
{x: 1, y: 7, type: "PAWN", colour: "BLACK"},
{x: 0, y: 0, type: "CASTLE", colour: "BLACK"},
{x: 0, y: 1, type: "KNIGHT", colour: "BLACK"},
{x: 0, y: 2, type: "BISHOP", colour: "BLACK"},
{x: 0, y: 3, type: "QUEEN", colour: "BLACK"},
{x: 0, y: 4, type: "KING", colour: "BLACK"},
{x: 0, y: 5, type: "BISHOP", colour: "BLACK"},
{x: 0, y: 6, type: "KNIGHT", colour: "BLACK"},
{x: 0, y: 7, type: "CASTLE", colour: "BLACK"},
{x: 6, y: 0, type: "PAWN", colour: "WHITE"},
{x: 6, y: 1, type: "PAWN", colour: "WHITE"},
{x: 6, y: 2, type: "PAWN", colour: "WHITE"},
{x: 6, y: 3, type: "PAWN", colour: "WHITE"},
{x: 6, y: 4, type: "PAWN", colour: "WHITE"},
{x: 6, y: 5, type: "PAWN", colour: "WHITE"},
{x: 6, y: 6, type: "PAWN", colour: "WHITE"},
{x: 6, y: 7, type: "PAWN", colour: "WHITE"},
{x: 7, y: 0, type: "CASTLE", colour: "WHITE"},
{x: 7, y: 1, type: "KNIGHT", colour: "WHITE"},
{x: 7, y: 2, type: "BISHOP", colour: "WHITE"},
{x: 7, y: 3, type: "QUEEN", colour: "WHITE"},
{x: 7, y: 4, type: "KING", colour: "WHITE"},
{x: 7, y: 5, type: "BISHOP", colour: "WHITE"},
{x: 7, y: 6, type: "KNIGHT", colour: "WHITE"},
{x: 7, y: 7, type: "CASTLE", colour: "WHITE"},
]
}
},
ready(){
this._pieces = this._standardBoard.slice(0);
var board = this.setupBoard();
this.placePieces(board);
this.board = board;
this.updateStyles({
'--width': this.edgeLength + "px",
'--tile-size': this.edgeLength / 10 + "px"
});
},
displayPiece(item){
if(!item.contents.hidden){
return item.contents.type;
}
return "";
},
displayHover (pieceName){
if(typeof(pieceName) === "undefined" || pieceName === ""){
return "";
}
return this.pieceTypes[pieceName.colour][pieceName.type];
} ,
placePieces(board){
for(var i = 0; i < this._pieces.length; i ++){
var item = this._pieces[i];
board[item.x][item.y].contents = {
type: this.pieceTypes[item.colour][item.type],
typeName: item.type,
color: item.colour,
hidden: item.hidden
};
}
},
acceptMove(originTile, landingTile, type, colour){
return true;
},
setupBoard(){
return this._rows.map(row => {
return this._columns.map(column => {
return {
location: column + row,
colour: (row + this._columns.indexOf(column)) % 2 == 0 ? "white" : "black",
contents: {
colour: "",
type: ""
}
}
})
})
},
onMouseDown(event){
this.$.hover.style.display = "block";
var clientRect = event.target.getBoundingClientRect();
var locationString = event.target.dataset.location;
var clickedPiece = this.getPieceAt(locationString);
clickedPiece.hidden = true;
this._draggedType = clickedPiece;
this.updateBoard();
var hoverOverlay = this.$.hover
this.setOverlayInitialPosition(hoverOverlay, clientRect);
//There are no position fields on the event so we null it here
// and allow the move handler to lazily populate it
this._initialClick = null;
},
updateBoard(){
var board = this.setupBoard();
this.placePieces(board);
this.board = board;
},
setOverlayInitialPosition(hoverOverlay, clientRect){
this._offsetX = clientRect.right - hoverOverlay.style.width - (this.edgeLength * 0.1125);
this._offsetY = clientRect.top - hoverOverlay.style.height + window.pageYOffset;
hoverOverlay.style.left = (this._offsetX) + "px";
hoverOverlay.style.top = (this._offsetY) + "px";
},
getPieceAt(locationString){
var location = this.stringToLocation(locationString);
var x = location[0];
var y = location[1];
for(var i = 0; i < this._pieces.length; i ++){
var piece = this._pieces[i];
if(piece.x === x && piece.y === y){
return piece;
}
}
},
stringToLocation(locationString){
return [
this._rows.indexOf(parseInt(locationString.charAt(1))),
this._columns.indexOf(locationString.charAt(0))
];
},
removePieceAt(locationString){
var location = this.stringToLocation(locationString);
var x = location[0];
var y = location[1];
this._pieces = this._pieces.filter(item => {
return !(item.x == x && item.y == y);
})
},
onMouseUp(event){
this.$.hover.style.display = "none";
var originTile = event.target.dataset.location;
var clickedPiece = this.getPieceAt(originTile);
clickedPiece.hidden = false;
var landingDiv = Polymer.dom(this.$.board).querySelector(".tile:hover");
if(landingDiv != null){
landingTile = landingDiv.getAttribute("data-location");
this.movePiece(landingTile, originTile);
}
this.updateBoard();
},
forceMovePiece(landingTile, originTile){
if(landingTile === originTile){
return;
}
piece = this.getPieceAt(originTile);
var landingLocation = this.stringToLocation(landingTile);
this.removePieceAt(landingTile)
piece.x = landingLocation[0];
piece.y = landingLocation[1];
},
movePiece(landingTile, originTile) {
if(landingTile === originTile){
return;
}
piece = this.getPieceAt(originTile);
var landingLocation = this.stringToLocation(landingTile);
if(this.acceptMove(originTile, landingTile, piece.type, piece.colour)){
this.removePieceAt(landingTile)
piece.x = landingLocation[0];
piece.y = landingLocation[1];
}
},
attached() {
this.$.board.addEventListener('mousemove', this.onMouseMove.bind(this));
},
detached() {
this.$.board.removeEventListener('mousemove', this.onMouseMove);
},
onMouseMove(event) {
var hoverOverlay = this.$.hover;
if(this._initialClick == null){
this._initialClick = event;
}
hoverOverlay.style.left = (this._offsetX + event.clientX - this._initialClick.clientX) + "px";
hoverOverlay.style.top = (this._offsetY + event.clientY - this._initialClick.clientY) + "px";
}
});
</script>
</dom-module>