-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
164 lines (152 loc) · 4.56 KB
/
Copy pathscript.js
File metadata and controls
164 lines (152 loc) · 4.56 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
const canvas = document.querySelector('#chessDisp')
const ctx = canvas.getContext('2d')
ctx.fillStyle = 'red'
ctx.font = 80 + 'px Arial';
const whiteSymbols = ['♙', '♖', '♘', '♗', '♕', '♔'];
const blackSymbols = ['♟︎', '♜', '♞', '♝', '♛', '♚']
const blackColor = '#006400'
const whiteColor = 'beige'
class chessBoard {
constructor(scale) {
this.board = createChessBoard()
this.scale = scale
this.movePiece(0,0,4,4)
this.focusedPos = {x: 4,y: 4}
this.moves = this.getMoves(this.focusedPos.x,this.focusedPos.y)
this.draw()
}
getMoves(x,y) {
const piece = this.board[y][x]
if(!piece) {
return false
}
const side = piece.side
let moves = []
switch(piece.type) {
case 1:
const dirs = [
{dx: 1,dy: 0},
{dx: -1,dy: 0},
{dx: 0, dy: 1},
{dx: 0, dy: -1},
]
return this.checkMovesVerDiag(x,y,side,dirs)
break;
case 3:
break;
}
return moves
}
checkMovesVerDiag(x,y,side,dirs) {
let moves = []
for(let dir of dirs) {
for(let i = 1; i < 8; i++) {
const newX = x + i * dir.dx
const newY = y + i * dir.dy
if(newY >= 8 || newY < 0 || newX >= 8 || newX < 0 ) {
break;
}
const piece = this.board[newY][newX]
if (!piece) {
moves.push({ kill: false, x: newX, y: newY })
} else if (piece.side !== side) {
moves.push({ kill: true, x: newX, y: newY })
break;
} else {
break;
}
}
}
return moves
}
movePiece(x1,y1,x2,y2) {
const piece = this.board[y1][x1]
this.board[y1][x1] = false
this.board[y2][x2] = piece
this.draw()
}
delPiece(x1,y1) {
this.board[y1][x1] = false
this.draw()
}
draw() {
let black = true
for(let y = 0; y < 8; y++) {
black = !black
for(let x = 0; x < 8; x++) {
let color;
if(black) {color = blackColor}
else {color = whiteColor}
ctx.fillStyle = color
const xDraw = x * this.scale;
const yDraw = y * this.scale;
ctx.fillRect(xDraw,yDraw,this.scale,this.scale)
black = !black
const cell = this.board[y][x]
if(cell) {
const symbol = getSymbol(cell.type,cell.side)
ctx.fillStyle = 'black'
ctx.fillText(symbol, xDraw, yDraw+this.scale);
}
}
}
if(this.moves) {
let lastKill = true
for(let move of this.moves) {
const xDraw = move.x * this.scale
const yDraw = move.y * this.scale
if(move.kill) {
ctx.fillStyle = '#5c0d0d79'
lastKill = true
}else if(lastKill){
ctx.fillStyle = '#0d5c3179'
lastKill = false
}
ctx.fillRect(xDraw,yDraw,this.scale,this.scale)
}
}
}
}
function getSymbol(type,side) {
const Symbols = side ? whiteSymbols : blackSymbols;
return Symbols[type]
}
function createChessBoard() {
let board = []
for(let i = 0; i < 8; i++) {
let array = [];
for(let j = 0; j < 8; j++) {
array.push(false)
}
board.push(array)
}
//Pawn
for(let j = 0; j < 8; j++) {
board[1][j] = new Piece(0, false)
board[6][j] = new Piece(0, true)
}
//Rock
board[0][0] = board[0][7] = new Piece(1, false);
board[7][0] = board[7][7] = new Piece(1, true);
//Knight
board[0][1] = board[0][6] = new Piece(2, false);
board[7][1] = board[7][6] = new Piece(2, true);
//bishop
board[0][2] = board[0][5] = new Piece(3, false);
board[7][2] = board[7][5] = new Piece(3, true);
//Quen
board[0][3] = new Piece(4, false);
board[7][3] = new Piece(4, true);
//King
board[0][4] = new Piece(5, false);
board[7][4] = new Piece(5, true);
return board;
}
class Piece {
constructor(type,side) {
this.lastMove
this.type = type
this.side = side
}
}
const board = new chessBoard(80)