Compare commits

..

2 Commits

Author SHA1 Message Date
0540b262dd Add basic moving capabilities 2021-06-01 21:47:06 +02:00
a3ae7c9f71 Refactor with some OOP 2021-06-01 21:43:25 +02:00

View File

@@ -38,45 +38,118 @@ function drawChessBoard(canvas) {
}
}
function drawPiece(canvas, piece, field) {
function computeScreenCoords(position, canvas) {
let canvasRect = canvas.getBoundingClientRect();
let rc = boardIndexToRowCol(field);
let im = new Image(BOX_SIDE, BOX_SIDE);
im.src = `./icons/${piece}.svg`;
im.onload = () => {
let rc = boardIndexToRowCol(position);
let topPx = canvasRect.top + INDEX_MARGIN + rc[0]*BOX_SIDE + window.pageYOffset;
let leftPx = canvasRect.left + INDEX_MARGIN + rc[1]*BOX_SIDE + window.pageXOffset;
return [topPx, leftPx]
}
class Move {
constructor(kind, from, to) {
this.kind = kind;
this.from = from;
this.to = to;
}
}
class Piece {
constructor(who, color, position) {
this.who = who;
this.color = color;
this.position = position;
this._im = null;
}
icon() {
return `./icons/${this.who}_${this.color}.svg`
}
draw(canvas) {
let [topPx, leftPx] = computeScreenCoords(this.position, canvas);
if (this._im === null) {
let im = new Image(BOX_SIDE, BOX_SIDE);
im.src = this.icon();
im.onload = () => {
im.style.position = 'absolute';
im.style.top = `${topPx}px`;
im.style.left = `${leftPx}px`;
};
document.body.appendChild(im);
this._im = im;
}
else {
this._im.style.top = `${topPx}px`;
this._im.style.left = `${leftPx}px`;
}
}
undraw() {
if (this._im !== null) {
this._im.remove();
this._im = null;
}
}
}
function _setupBoard(canvas) {
drawPiece(_canvas, 'rook_black', 'a8');
drawPiece(_canvas, 'rook_black', 'h8');
drawPiece(_canvas, 'knight_black', 'b8');
drawPiece(_canvas, 'knight_black', 'g8');
drawPiece(_canvas, 'bishop_black', 'c8');
drawPiece(_canvas, 'bishop_black', 'f8');
drawPiece(_canvas, 'queen_black', 'd8');
drawPiece(_canvas, 'king_black', 'e8');
for (let colLetter of INDEX_LETTERS) {
drawPiece(canvas, 'pawn_black', `${colLetter}7`);
drawPiece(canvas, 'pawn_white', `${colLetter}2`);
class Configuration {
constructor(pieces) {
this.pieces = pieces
}
draw(canvas) {
for (let piece of this.pieces) {
piece.draw(canvas);
}
}
getAt(position) {
for (let piece of this.pieces) {
if (piece.position === position) return piece;
}
return null;
}
dropAt(position) {
for (let idx = 0; idx < this.pieces.length; idx++) {
if (this.pieces[idx].position === position) {
this.pieces[idx].undraw();
this.pieces.splice(idx, 1);
return;
}
}
}
makeMove(move, canvas) {
let piece = this.getAt(move.from);
if (piece === null) return;
this.dropAt(move.to);
piece.position = move.to;
piece.draw(canvas);
}
}
drawPiece(_canvas, 'rook_white', 'a1');
drawPiece(_canvas, 'rook_white', 'h1');
drawPiece(_canvas, 'knight_white', 'b1');
drawPiece(_canvas, 'knight_white', 'g1');
drawPiece(_canvas, 'bishop_white', 'c1');
drawPiece(_canvas, 'bishop_white', 'f1');
drawPiece(_canvas, 'queen_white', 'd1');
drawPiece(_canvas, 'king_white', 'e1');
function setupBoard() {
let pawns = [];
for (let x of INDEX_LETTERS) {
pawns.push(new Piece('pawn', 'black', `${x}7`),
new Piece('pawn', 'white', `${x}2`));
}
return new Configuration([
new Piece('rook', 'black', 'a8'),
new Piece('rook', 'black', 'h8'),
new Piece('knight', 'black', 'b8'),
new Piece('knight', 'black', 'g8'),
new Piece('bishop', 'black', 'c8'),
new Piece('bishop', 'black', 'f8'),
new Piece('queen', 'black', 'd8'),
new Piece('king', 'black', 'e8'),
...pawns,
new Piece('rook', 'white', 'a1'),
new Piece('rook', 'white', 'h1'),
new Piece('knight', 'white', 'b1'),
new Piece('knight', 'white', 'g1'),
new Piece('bishop', 'white', 'c1'),
new Piece('bishop', 'white', 'f1'),
new Piece('queen', 'white', 'd1'),
new Piece('king', 'white', 'e1'),
])
}
function boardIndexToRowCol(boardIndex) {
@@ -87,6 +160,19 @@ function boardIndexToRowCol(boardIndex) {
return [row, col];
}
var _canvas = createCanvas();
drawChessBoard(_canvas);
_setupBoard(_canvas);
async function demo() {
let canvas = createCanvas();
drawChessBoard(canvas);
let config = setupBoard();
config.draw(canvas);
await new Promise(r => setTimeout(r, 6000));
config.makeMove({'kind': 'xxx', 'from': 'e2', 'to': 'e4'}, canvas);
await new Promise(r => setTimeout(r, 2000));
config.makeMove({'kind': 'xxx', 'from': 'e7', 'to': 'e5'}, canvas);
await new Promise(r => setTimeout(r, 2000));
config.makeMove({'kind': 'xxx', 'from': 'd2', 'to': 'd4'}, canvas);
await new Promise(r => setTimeout(r, 2000));
config.makeMove({'kind': 'xxx', 'from': 'e5', 'to': 'd4'}, canvas);
}
demo();