import javax.swing.*; import java.awt.*; import java.awt.event.*; public class CheckerGame extends JFrame { private static final int BOARD_SIZE = 8; private static final int CELL_SIZE = 80; private static final int PIECE_RADIUS = 30; private Cell[][] board; private Piece selectedPiece; private boolean isWhiteTurn; public CheckerGame() { setTitle("Checker Game"); setSize(CELL_SIZE * BOARD_SIZE, CELL_SIZE * BOARD_SIZE); setResizable(false); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); board = new Cell[BOARD_SIZE][BOARD_SIZE]; selectedPiece = null; isWhiteTurn = true; addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { int row = e.getY() / CELL_SIZE; int col = e.getX() / CELL_SIZE; handleCellClick(row, col); } }); setFocusable(true); requestFocusInWindow(); } public void startGame() { initializeBoard(); repaint(); } private void initializeBoard() { for (int row = 0; row < BOARD_SIZE; row++) { for (int col = 0; col < BOARD_SIZE; col++) { if ((row + col) % 2 == 0) { board[row][col] = new Cell(row, col, null, Color.LIGHT_GRAY); } else { if (row < 3) { board[row][col] = new Cell(row, col, new Piece(row, col, true), Color.LIGHT_GRAY); } else if (row > 4) { board[row][col] = new Cell(row, col, new Piece(row, col, false), Color.LIGHT_GRAY); } else { board[row][col] = new Cell(row, col, null, Color.LIGHT_GRAY); } } } } } private void handleCellClick(int row, int col) { if (selectedPiece == null) { Piece piece = board[row][col].getPiece(); if (piece != null && piece.isWhite() == isWhiteTurn) { selectedPiece = piece; } } else { if (selectedPiece.isValidMove(row, col) && isMoveValid(row, col)) { movePiece(selectedPiece, row, col); selectedPiece = null; isWhiteTurn = !isWhiteTurn; } } repaint(); } private boolean isMoveValid(int row, int col) { int selectedRow = selectedPiece.getRow(); int selectedCol = selectedPiece.getCol(); if (row < 0 || row >= BOARD_SIZE || col < 0 || col >= BOARD_SIZE) { return false; } if (board[row][col].getPiece() != null) { return false; } int rowDiff = Math.abs(row - selectedRow); int colDiff = Math.abs(col - selectedCol); if (rowDiff == 1 && colDiff == 1) { return true; } else if (rowDiff == 2 && colDiff == 2) { int jumpRow = (row + selectedRow) / 2; int jumpCol = (col + selectedCol) / 2; Piece jumpedPiece = board[jumpRow][jumpCol].getPiece(); return jumpedPiece != null && jumpedPiece.isWhite() != selectedPiece.isWhite(); } return false; } private void movePiece(Piece piece, int row, int col) { int selectedRow = piece.getRow(); int selectedCol = piece.getCol(); board[selectedRow][selectedCol].setPiece(null); board[row][col].setPiece(piece); piece.setRow(row); piece.setCol(col); } public void paint(Graphics g) { super.paint(g); for (int row = 0; row < BOARD_SIZE; row++) { for (int col = 0; col < BOARD_SIZE; col++) { Cell cell = board[row][col]; g.setColor(cell.getColor()); g.fillRect(col * CELL_SIZE, row * CELL_SIZE, CELL_SIZE, CELL_SIZE); Piece piece = cell.getPiece(); if (piece != null) { g.setColor(piece.isWhite() ? Color.WHITE : Color.BLACK); g.fillOval(col * CELL_SIZE + (CELL_SIZE - PIECE_RADIUS * 2) / 2, row * CELL_SIZE + (CELL_SIZE - PIECE_RADIUS * 2) / 2, PIECE_RADIUS * 2, PIECE_RADIUS * 2); } } } } private class Cell { private int row; private int col; private Piece piece; private Color color; public Cell(int row, int col, Piece piece, Color color) { this.row = row; this.col = col; this.piece = piece; this.color = color; } public int getRow() { return row; } public int getCol() { return col; } public Piece getPiece() { return piece; } public Color getColor() { return color; } public void setPiece(Piece piece) { this.piece = piece; } } private class Piece { private int row; private int col; private boolean isWhite; public Piece(int row, int col, boolean isWhite) { this.row = row; this.col = col; this.isWhite = isWhite; } public int getRow() { return row; } public void setRow(int row) { this.row = row; } public int getCol() { return col; } public void setCol(int col) { this.col = col; } public boolean isWhite() { return isWhite; } public boolean isValidMove(int row, int col) { int rowDiff = Math.abs(row - this.row); int colDiff = Math.abs(col - this.col); return (rowDiff == 1 && colDiff == 1) || (rowDiff == 2 && colDiff == 2); } } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { CheckerGame game = new CheckerGame(); game.setVisible(true); game.startGame(); } }); } }