Chess
unknown
java
a month ago
6.4 kB
1
Indexable
Never
public class ChessGameDemo { // Piece Interface public interface Piece { char getSymbol(); boolean isValidMove(int startX, int startY, int endX, int endY, Board board); void move(int startX, int startY, int endX, int endY, Board board); } // Board Interface public interface Board { Piece getPiece(int x, int y); void setPiece(int x, int y, Piece piece); boolean isOccupied(int x, int y); void printBoard(); } // Player Interface public interface Player { String getName(); Piece getPiece(); boolean makeMove(Board board, int startX, int startY, int endX, int endY); } // Game Interface public interface Game { void startGame(); void endGame(); Player getCurrentPlayer(); void switchPlayer(); } // Pawn Class public static class Pawn implements Piece { private char symbol; public Pawn(char symbol) { this.symbol = symbol; } @Override public char getSymbol() { return symbol; } @Override public boolean isValidMove(int startX, int startY, int endX, int endY, Board board) { int dx = Math.abs(endX - startX); int dy = Math.abs(endY - startY); // Pawn moves one step forward or two steps from the initial position if (dx == 0 && dy == 1 && !board.isOccupied(endX, endY)) { return true; } if (dx == 0 && dy == 2 && startY == 1 && !board.isOccupied(endX, endY)) { return true; } // Capture logic if (dx == 1 && dy == 1 && board.isOccupied(endX, endY) && board.getPiece(endX, endY).getSymbol() != this.symbol) { return true; } return false; } @Override public void move(int startX, int startY, int endX, int endY, Board board) { if (isValidMove(startX, startY, endX, endY, board)) { board.setPiece(endX, endY, this); board.setPiece(startX, startY, null); } else { throw new IllegalArgumentException("Invalid move"); } } } // ChessBoard Class public static class ChessBoard implements Board { private Piece[][] board; public ChessBoard() { board = new Piece[8][8]; } @Override public Piece getPiece(int x, int y) { if (x >= 0 && x < 8 && y >= 0 && y < 8) { return board[x][y]; } return null; } @Override public void setPiece(int x, int y, Piece piece) { if (x >= 0 && x < 8 && y >= 0 && y < 8) { board[x][y] = piece; } } @Override public boolean isOccupied(int x, int y) { return getPiece(x, y) != null; } @Override public void printBoard() { for (int y = 7; y >= 0; y--) { for (int x = 0; x < 8; x++) { Piece piece = getPiece(x, y); if (piece != null) { System.out.print(piece.getSymbol() + " "); } else { System.out.print(". "); } } System.out.println(); } } } // ChessPlayer Class public static class ChessPlayer implements Player { private String name; private Piece pieceType; public ChessPlayer(String name, Piece pieceType) { this.name = name; this.pieceType = pieceType; } @Override public String getName() { return name; } @Override public Piece getPiece() { return pieceType; } @Override public boolean makeMove(Board board, int startX, int startY, int endX, int endY) { Piece piece = board.getPiece(startX, startY); if (piece != null && piece.getSymbol() == pieceType.getSymbol()) { if (piece.isValidMove(startX, startY, endX, endY, board)) { piece.move(startX, startY, endX, endY, board); return true; } } return false; } } // ChessGame Class public static class ChessGame implements Game { private Board board; private Player player1; private Player player2; private Player currentPlayer; public ChessGame(Player player1, Player player2) { this.board = new ChessBoard(); this.player1 = player1; this.player2 = player2; this.currentPlayer = player1; // Initialize board with pieces (simplified example) for (int i = 0; i < 8; i++) { board.setPiece(i, 1, new Pawn('P')); // Player 1's pawns board.setPiece(i, 6, new Pawn('p')); // Player 2's pawns } } @Override public void startGame() { board.printBoard(); } @Override public void endGame() { System.out.println("Game over!"); } @Override public Player getCurrentPlayer() { return currentPlayer; } @Override public void switchPlayer() { currentPlayer = (currentPlayer == player1) ? player2 : player1; } public void makeMove(int startX, int startY, int endX, int endY) { if (currentPlayer.makeMove(board, startX, startY, endX, endY)) { switchPlayer(); } else { System.out.println("Invalid move"); } board.printBoard(); } } public static void main(String[] args) { Player player1 = new ChessPlayer("Player 1", new Pawn('P')); Player player2 = new ChessPlayer("Player 2", new Pawn('p')); Game game = new ChessGame(player1, player2); game.startGame(); // Example moves game.makeMove(0, 1, 0, 3); // Player 1 moves a pawn game.makeMove(0, 6, 0, 5); // Player 2 moves a pawn } }
Leave a Comment