xxxxxxxxxx
121
let board = [];
let pieces = [];
let selectedPiece = null;
function setup() {
createCanvas(650, 650);
for (let i = 0; i < 10; i++) {
board[i] = [];
for (let j = 0; j < 10; j++) {
board[i][j] = null;
}
}
// Place pieces on the board
initializePieces();
}
function draw() {
background(255);
drawBoard();
drawLabels();
drawPieces();
}
function drawBoard() {
for (let i = 0; i < 10; i++) {
for (let j = 0; j < 10; j++) {
fill((i + j) % 2 === 0 ? 200 : 100);
rect(i * 50 + 50, j * 50 + 50, 50, 50);
}
}
}
function drawLabels() {
textSize(20);
fill(0);
for (let i = 0; i < 10; i++) {
text(String.fromCharCode(65 + i), i * 50 + 75, 40); // Column labels (A-J)
text(i + 1, 30, i * 50 + 75); // Row labels (1-10)
}
}
function initializePieces() {
pieces.push(new Piece('K', 0, 0, 'white'));
pieces.push(new Piece('G', 1, 0, 'white'));
pieces.push(new Piece('L', 0, 1, 'white'));
pieces.push(new Piece('C', 0, 2, 'white'));
for (let i = 0; i < 3; i++) {
pieces.push(new Piece('A', 3, i, 'white'));
pieces.push(new Piece('A', i, 3, 'white'));
}
// Mirror for black
pieces.push(new Piece('K', 9, 9, 'black'));
pieces.push(new Piece('G', 8, 9, 'black'));
pieces.push(new Piece('L', 9, 8, 'black'));
pieces.push(new Piece('C', 9, 7, 'black'));
for (let i = 0; i < 3; i++) {
pieces.push(new Piece('A', 6, 9 - i, 'black'));
pieces.push(new Piece('A', 9 - i, 6, 'black'));
}
}
function drawPieces() {
for (let piece of pieces) {
piece.show();
}
}
function mouseDragged() {
let x = floor((mouseX - 50) / 50);
let y = floor((mouseY - 50) / 50);
if (!selectedPiece) {
selectedPiece = getPieceAt(x, y);
} else {
selectedPiece.move(x, y);
}
}
function mouseReleased() {
let x = floor((mouseX - 50) / 50);
let y = floor((mouseY - 50) / 50);
let piece =getPieceAt(x,y);
if (piece != null && piece != selectedPiece) {
piece.x = 1000;
piece.y = 1000; // hide
}
selectedPiece = null;
}
function getPieceAt(x, y) {
for (let piece of pieces) {
if (piece.x === x && piece.y === y) {
return piece;
}
}
return null;
}
class Piece {
constructor(type, x, y, color) {
this.type = type;
this.x = x;
this.y = y;
this.color = color;
}
show(offsetX = 0, offsetY = 0) {
fill(this.color === 'white' ? 255 : 0);
textAlign(CENTER, CENTER);
textSize(32);
text(this.type, this.x * 50 + 75 + offsetX, this.y * 50 + 75 + offsetY);
}
move(x, y) {
this.x = x;
this.y = y;
}
}