Code · LLD machine coding · Personal project
Chess Game
Two-player chess on the console: an 8×8 board, each piece validating its own moves, turn handling, and invalid-move errors.
Question
requirements
// 2 chess prayler
Board.java 62 lines
package chessgame;
import chessgame.pieces.*;
public class Board {
private final Cell[][] board;
public Board() {
board = new Cell[8][8];
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
board[row][col] = new Cell(row, col);
}
}
setupPieces();
}
//initialise pieces for game start
private void setupPieces() {
// Add pawns and main pieces for both sides
for (int j = 0; j < 8; j++) {
board[1][j].setPiece(new Pawn(Color.WHITE));
board[6][j].setPiece(new Pawn(Color.BLACK));
}
// Initialize white pieces
board[0][0].setPiece(new Rook(Color.WHITE));
board[0][1].setPiece(new Knight(Color.WHITE));
board[0][2].setPiece(new Bishop(Color.WHITE));
board[0][3].setPiece(new Queen(Color.WHITE));
board[0][4].setPiece(new King(Color.WHITE));
board[0][5].setPiece(new Bishop(Color.WHITE));
board[0][6].setPiece(new Knight(Color.WHITE));
board[0][7].setPiece(new Rook(Color.WHITE));
// Initialize black pieces
board[7][0].setPiece(new Rook(Color.BLACK));
board[7][1].setPiece(new Knight(Color.BLACK));
board[7][2].setPiece(new Bishop(Color.BLACK));
board[7][3].setPiece(new Queen(Color.BLACK));
board[7][4].setPiece(new King(Color.BLACK));
board[7][5].setPiece(new Bishop(Color.BLACK));
board[7][6].setPiece(new Knight(Color.BLACK));
board[7][7].setPiece(new Rook(Color.BLACK));
}
public Cell getCell(int row, int col) {
return board[row][col];
}
public synchronized boolean movePiece(Move move) {
Cell from = move.getStart(), to = move.getEnd();
Piece piece = from.getPiece();
if (piece == null || !piece.canMove(this, from, to)) return false;
to.setPiece(piece);
from.setPiece(null);
return true;
}
public Piece getPiece(int row, int col) {
return board[row][col].getPiece();
}
public boolean isCheckmate(Color color) {
// TODO: Implement checkmate logic
return false;
}
public boolean isStalemate(Color color) {
// TODO: Implement stalemate logic
return false;
}
} Cell.java 25 lines
package chessgame;
import chessgame.pieces.Piece;
public class Cell {
private final int row, col;
private Piece piece;
public Cell(int row, int col) {
this.row = row;
this.col = col;
}
public boolean isOccupied() {
return piece != null ? true: false;
}
public Piece getPiece() {
return piece;
}
public void setPiece(Piece piece) {
this.piece = piece;
}
public int getRow() {
return row;
}
public int getCol() {
return col;
}
} ChessGame.java 60 lines
package chessgame;
import chessgame.pieces.Piece;
import java.util.Scanner;
public class ChessGame {
private final Board board;
private Player whitePlayer , blackPlayer;
private Player currentPlayer;
public ChessGame() {
this.board = new Board();
}
public void setPlayers(String whitePlayerName , String blackPlayerName) {
this.whitePlayer = new Player(whitePlayerName, Color.WHITE);
this.blackPlayer = new Player(blackPlayerName, Color.BLACK);
this.currentPlayer = whitePlayer;
}
public void start() {
// Game loop
while (!isGameOver()) {
Player player = currentPlayer;
System.out.println(player.getName() + "'s turn.");
// Get move from the player
Move move = getPlayerMove(player);
// Make the move on the board
try {
board.movePiece(move);
} catch (InvalidMoveException e) {
System.out.println(e.getMessage());
System.out.println("Try again!");
continue;
}
// Switch to the next player
switchTurn();
}
// Display game result
// displayResult();
}
private Move getPlayerMove(Player player) {
// For simplicity, let's assume the player enters the move via console input
Scanner scanner = new Scanner(System.in);
System.out.print("Enter source row: ");
int sourceRow = scanner.nextInt();
System.out.print("Enter source column: ");
int sourceCol = scanner.nextInt();
System.out.print("Enter destination row: ");
int destRow = scanner.nextInt();
System.out.print("Enter destination column: ");
int destCol = scanner.nextInt();
Piece piece = board.getPiece(sourceRow, sourceCol);
if (piece == null || piece.getColor() != player.getColor()) {
throw new IllegalArgumentException("Invalid piece selection!");
}
return new Move(board.getCell(sourceRow, sourceCol), board.getCell(destRow, destCol));
}
private void switchTurn() {
currentPlayer = currentPlayer == whitePlayer ? blackPlayer:whitePlayer;
}
private boolean isGameOver() {
return false;
}
} Color.java 4 lines
package chessgame;
public enum Color {
WHITE, BLACK
} InvalidMoveException.java 6 lines
package chessgame;
public class InvalidMoveException extends RuntimeException {
public InvalidMoveException(final String message) {
super(message);
}
} Move.java 14 lines
package chessgame;
public class Move {
private final Cell start, end;
public Move(Cell start, Cell end) {
this.start = start;
this.end = end;
}
public Cell getStart() {
return start;
}
public Cell getEnd(){
return end;
}
} Player.java 15 lines
package chessgame;
public class Player {
private final String name;
private final Color color;
public Player(String name, Color color) {
this.name = name;
this.color = color;
}
public Color getColor() {
return color;
}
public String getName() {
return name;
}
} pieces/Bishop.java 15 lines
package chessgame.pieces;
import chessgame.Board;
import chessgame.Cell;
import chessgame.Color;
public class Bishop extends Piece{
public Bishop(Color color) {
super(color);
}
@Override
public boolean canMove(Board board, Cell from, Cell to) {
int rowDiff = Math.abs(from.getRow() - to.getRow());
int colDiff = Math.abs(from.getCol() - to.getCol());
return (rowDiff == colDiff);
}
} pieces/King.java 15 lines
package chessgame.pieces;
import chessgame.Board;
import chessgame.Cell;
import chessgame.Color;
public class King extends Piece{
public King(Color color) {
super(color);
}
@Override
public boolean canMove(Board board, Cell from, Cell to) {
int rowDiff = Math.abs(from.getRow() - to.getRow());
int colDiff = Math.abs(from.getCol() - to.getCol());
return ( rowDiff <= 1 && colDiff <= 1 );
}
} pieces/Knight.java 15 lines
package chessgame.pieces;
import chessgame.Board;
import chessgame.Cell;
import chessgame.Color;
public class Knight extends Piece{
public Knight(Color color) {
super(color);
}
@Override
public boolean canMove(Board board, Cell from, Cell to) {
int rowDiff = Math.abs(from.getRow() - to.getRow());
int colDiff = Math.abs(from.getCol() - to.getCol());
return (rowDiff == 2 && colDiff == 1) || (colDiff == 2 && rowDiff == 1);
}
} pieces/Pawn.java 25 lines
package chessgame.pieces;
import chessgame.Board;
import chessgame.Cell;
import chessgame.Color;
public class Pawn extends Piece {
public Pawn(Color color) {
super(color);
}
@Override
public boolean canMove(Board board, Cell from, Cell to) {
int rowDiff = to.getRow() - from.getRow();
int colDiff = Math.abs(to.getCol() - from.getCol());
//
// if (color == Color.WHITE) {
// return (rowDiff == 1 && colDiff == 0) ||
// (from.getRow() == 1 && rowDiff == 2 && colDiff == 0) ||
// (rowDiff == 1 && colDiff == 1 && board.getPiece(to.getRow(), to.getCol()) != null);
// } else {
// return (rowDiff == -1 && colDiff == 0) ||
// (from.getRow() == 6 && rowDiff == -2 && colDiff == 0) ||
// (rowDiff == -1 && colDiff == 1 && board.getPiece(to.getRow(), to.getCol()) != null);
// }
return false;
}
} pieces/Piece.java 14 lines
package chessgame.pieces;
import chessgame.Board;
import chessgame.Cell;
import chessgame.Color;
public abstract class Piece {
protected final Color color;
public Piece(Color color) {
this.color = color;
}
public abstract boolean canMove(Board board, Cell from, Cell to);
public Color getColor() {
return color;
}
} pieces/Queen.java 15 lines
package chessgame.pieces;
import chessgame.Board;
import chessgame.Cell;
import chessgame.Color;
public class Queen extends Piece{
public Queen(Color color) {
super(color);
}
@Override
public boolean canMove(Board board, Cell from, Cell to) {
int rowDiff = Math.abs(from.getRow() - to.getRow());
int colDiff = Math.abs(from.getCol() - to.getCol());
return ((rowDiff == colDiff) || (from.getRow() == to.getRow()) || (from.getCol() == to.getCol()));
}
} pieces/Rook.java 15 lines
package chessgame.pieces;
import chessgame.Board;
import chessgame.Cell;
import chessgame.Color;
public class Rook extends Piece{
public Rook(Color color){
super(color);
}
@Override
public boolean canMove(Board board, Cell from , Cell to) {
int rowDiff = Math.abs(from.getRow() - to.getRow());
int colDiff = Math.abs(from.getCol() - to.getCol());
return (rowDiff == 0 || colDiff == 0);
}
} ChessGameDemo.java 8 lines
package chessgame;
public class ChessGameDemo {
public static void main(String[] args) {
ChessGame chessGame = new ChessGame();
chessGame.setPlayers("Alice", "Bob");
chessGame.start();
}
}