Skip to content

Instantly share code, notes, and snippets.

@carefree-ladka
Created February 1, 2025 18:08
Show Gist options
  • Select an option

  • Save carefree-ladka/3d72dc46b1cbe0ee71f7857167cd95f9 to your computer and use it in GitHub Desktop.

Select an option

Save carefree-ladka/3d72dc46b1cbe0ee71f7857167cd95f9 to your computer and use it in GitHub Desktop.
Chess Game Based on OOPS
class Piece {
constructor(color, name) {
this.color = color; // "White" or "Black"
this.name = name;
}
isValidMove(start, end, board) {
return false; // Each piece will override this method
}
}
class King extends Piece {
constructor(color) {
super(color, "King");
}
isValidMove(start, end, board) {
const dx = Math.abs(start.x - end.x);
const dy = Math.abs(start.y - end.y);
return dx <= 1 && dy <= 1; // King moves 1 square in any direction
}
}
class Queen extends Piece {
constructor(color) {
super(color, "Queen");
}
isValidMove(start, end, board) {
return board.isStraightOrDiagonalMove(start, end);
}
}
class Bishop extends Piece {
constructor(color) {
super(color, "Bishop");
}
isValidMove(start, end, board) {
return board.isDiagonalMove(start, end);
}
}
class Knight extends Piece {
constructor(color) {
super(color, "Knight");
}
isValidMove(start, end, board) {
const dx = Math.abs(start.x - end.x);
const dy = Math.abs(start.y - end.y);
return (dx === 2 && dy === 1) || (dx === 1 && dy === 2); // L-shaped move
}
}
class Rook extends Piece {
constructor(color) {
super(color, "Rook");
}
isValidMove(start, end, board) {
return board.isStraightMove(start, end);
}
}
class Pawn extends Piece {
constructor(color) {
super(color, "Pawn");
}
isValidMove(start, end, board) {
const forward = this.color === "White" ? 1 : -1;
const dx = end.x - start.x;
const dy = Math.abs(end.y - start.y);
if (dx === forward && dy === 0 && !board.getPiece(end)) {
return true; // Normal forward move
}
if (dx === forward && dy === 1 && board.getPiece(end)) {
return true; // Capturing a piece diagonally
}
return false;
}
}
class Board {
constructor() {
this.grid = this.createBoard();
}
createBoard() {
let board = Array.from({ length: 8 }, () => Array(8).fill(null));
const pieceOrder = [Rook, Knight, Bishop, Queen, King, Bishop, Knight, Rook];
// Place White pieces
pieceOrder.forEach((PieceClass, i) => {
board[0][i] = new PieceClass("White");
board[1][i] = new Pawn("White");
});
// Place Black pieces
pieceOrder.forEach((PieceClass, i) => {
board[7][i] = new PieceClass("Black");
board[6][i] = new Pawn("Black");
});
return board;
}
getPiece(position) {
return this.grid[position.x][position.y];
}
movePiece(start, end) {
let piece = this.getPiece(start);
if (!piece) {
console.log("No piece at starting position!");
return false;
}
if (!piece.isValidMove(start, end, this)) {
console.log("Invalid move!");
return false;
}
this.grid[end.x][end.y] = piece;
this.grid[start.x][start.y] = null;
console.log(`${piece.name} moved to ${end.x},${end.y}`);
return true;
}
isStraightMove(start, end) {
return start.x === end.x || start.y === end.y;
}
isDiagonalMove(start, end) {
return Math.abs(start.x - end.x) === Math.abs(start.y - end.y);
}
isStraightOrDiagonalMove(start, end) {
return this.isStraightMove(start, end) || this.isDiagonalMove(start, end);
}
}
class ChessGame {
constructor() {
this.board = new Board();
this.players = ["White", "Black"];
this.turn = 0;
}
playTurn(start, end) {
let currentPlayer = this.players[this.turn % 2];
let piece = this.board.getPiece(start);
if (!piece || piece.color !== currentPlayer) {
console.log(`It's ${currentPlayer}'s turn!`);
return;
}
if (this.board.movePiece(start, end)) {
this.turn++;
}
}
}
// Example Game
const game = new ChessGame();
game.playTurn({ x: 1, y: 4 }, { x: 3, y: 4 }); // White Pawn moves forward
game.playTurn({ x: 6, y: 4 }, { x: 4, y: 4 }); // Black Pawn moves forward
game.playTurn({ x: 0, y: 3 }, { x: 4, y: 7 }); // White Queen tries to move
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment