Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public enum SquareState {
- OCCUPIED, EMPTY
- }
- public enum Color {
- BLACK, WHITE
- }
- public class ChessBoard {
- public static final int MAX_ROWS = 8;
- public static final int MAX_COLS = 8;
- public static final int NUM_SQUARES = 64;
- private Square squares[][];
- private List<Piece> whitePieces;
- private List<Piece> blackPieces;
- private Map<Square, Piece> occupiedSquares;
- public Piece pieceAt(Square square) {
- return occupiedSquares.get(square);
- }
- }
- //Square class
- public class Square {
- private int row, col;
- private Color color;
- private SquareState state;
- }
- //Piece class -not an abstract class(do I need one, I already have PieceType)
- public class Piece {
- private PieceType type;
- private Color color;
- private Square position;
- public void move(ChessBoard chessBoard, Square dest) {
- //place the piece from its position to dest on the board
- }
- }
- public class Move {
- private MoveType moveType; //undo operation will require move type
- private Square source;
- private Square destination;
- private Piece sourcePiece; //at source
- private Piece killedPiece; //at dest
- private Move previous, next;
- }
- public class Player {
- private boolean isWhite;
- private ChessBoard chessBoard;
- private MovementGenerator movementGenerator;
- private List<Piece> alivePieces;
- public void makeMove() {
- Piece chosenPiece = alivePieces.get(0);
- List<Square> squares = movementGenerator.getPossibleMoves(chessBoard, chosenPiece);
- chosenPiece.move(chessBoard, squares.get(0));
- }
- }
- public class MovementGenerator {
- private MovementStrategy movementStrategy;
- private MovementStrategyResolver movementStrategyResolver;
- public MovementGenerator(MovementStrategy movementStrategy) {
- this.movementStrategy = movementStrategy;
- }
- public List<Square> getPossibleMoves(ChessBoard chessBoard, Piece piece) {
- return movementStrategyResolver.resolveStrategy(piece).getPossibleMoves(chessBoard, piece);
- }
- }
- public interface MovementStrategy {
- public List<Square> getPossibleMoves(ChessBoard chessBoard, Piece piece);
- }
- public class BishopMovementStrategy implements MovementStrategy{
- @Override
- public List<Square> getPossibleMoves(ChessBoard chessBoard, Piece piece) {
- // TODO Auto-generated method stub
- return null;
- }
- }
- public class KnightMovementStrategy implements MovementStrategy {
- @Override
- public List<Square> getPossibleMoves(ChessBoard chessBoard, Piece piece) {
- // TODO Auto-generated method stub
- return null;
- }
- }
- //A class to validate if move made by the player is a legal one
- public class MovementValidator {
- private MovementGenerator movementGenerator;
- public boolean isValidMove(ChessBoard board, Move move) {
- Square source = move.getSource();
- Square dest = move.getDestination();
- if (outsideBoard(source) || outsideBoard(dest))
- return false;
- // can't kill own pieces
- if (board.pieceAt(source).getColor() == board.pieceAt(dest).getColor())
- return false;
- // if dest empty then proceed(should also check for checkmate condition
- // in this case)
- if (dest.getState() == SquareState.EMPTY)
- return true;
- // try to kill the opponent
- List<Square> possiblePositions = movementGenerator
- .getPossibleMoves(move.getSourcePiece());
- if (!possiblePositions.contains(dest))
- return false;
- return true;
- }
- private boolean outsideBoard(Square square) {
- int x = square.getRow();
- int y = square.getCol();
- if (x < 0 || x > 8 || y > 0 || y > 8)
- return false;
- return true;
- }
- }
- public abstract class Piece {
- private PieceType type;
- private Color color;
- private Square position;
- /**
- * @param move to be checked
- * @return if move is valid
- */
- public abstract boolean isValidMove(Move move);
- /**
- * @return List of moves
- */
- public abstract List<Move> getValidMoves();
- public void move(ChessBoard chessBoard, Square dest) {
- //place the piece from its position to dest on the board
- Move move;
- /*Instantiate move*/
- if (validMove(move)){
- //Make the move
- }
- }
- }
Add Comment
Please, Sign In to add comment