Guest User

Untitled

a guest
Jul 16th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.34 KB | None | 0 0
  1. public enum SquareState {
  2. OCCUPIED, EMPTY
  3. }
  4.  
  5. public enum Color {
  6. BLACK, WHITE
  7. }
  8.  
  9. public class ChessBoard {
  10. public static final int MAX_ROWS = 8;
  11. public static final int MAX_COLS = 8;
  12. public static final int NUM_SQUARES = 64;
  13. private Square squares[][];
  14. private List<Piece> whitePieces;
  15. private List<Piece> blackPieces;
  16. private Map<Square, Piece> occupiedSquares;
  17.  
  18. public Piece pieceAt(Square square) {
  19. return occupiedSquares.get(square);
  20. }
  21. }
  22.  
  23. //Square class
  24. public class Square {
  25. private int row, col;
  26. private Color color;
  27. private SquareState state;
  28. }
  29.  
  30. //Piece class -not an abstract class(do I need one, I already have PieceType)
  31. public class Piece {
  32. private PieceType type;
  33. private Color color;
  34. private Square position;
  35.  
  36. public void move(ChessBoard chessBoard, Square dest) {
  37. //place the piece from its position to dest on the board
  38. }
  39. }
  40.  
  41. public class Move {
  42. private MoveType moveType; //undo operation will require move type
  43. private Square source;
  44. private Square destination;
  45. private Piece sourcePiece; //at source
  46. private Piece killedPiece; //at dest
  47. private Move previous, next;
  48. }
  49.  
  50. public class Player {
  51. private boolean isWhite;
  52. private ChessBoard chessBoard;
  53. private MovementGenerator movementGenerator;
  54. private List<Piece> alivePieces;
  55.  
  56. public void makeMove() {
  57. Piece chosenPiece = alivePieces.get(0);
  58. List<Square> squares = movementGenerator.getPossibleMoves(chessBoard, chosenPiece);
  59. chosenPiece.move(chessBoard, squares.get(0));
  60. }
  61. }
  62.  
  63. public class MovementGenerator {
  64. private MovementStrategy movementStrategy;
  65. private MovementStrategyResolver movementStrategyResolver;
  66.  
  67. public MovementGenerator(MovementStrategy movementStrategy) {
  68. this.movementStrategy = movementStrategy;
  69. }
  70.  
  71. public List<Square> getPossibleMoves(ChessBoard chessBoard, Piece piece) {
  72. return movementStrategyResolver.resolveStrategy(piece).getPossibleMoves(chessBoard, piece);
  73. }
  74.  
  75. }
  76.  
  77. public interface MovementStrategy {
  78. public List<Square> getPossibleMoves(ChessBoard chessBoard, Piece piece);
  79. }
  80.  
  81. public class BishopMovementStrategy implements MovementStrategy{
  82. @Override
  83. public List<Square> getPossibleMoves(ChessBoard chessBoard, Piece piece) {
  84. // TODO Auto-generated method stub
  85. return null;
  86. }
  87. }
  88.  
  89. public class KnightMovementStrategy implements MovementStrategy {
  90.  
  91. @Override
  92. public List<Square> getPossibleMoves(ChessBoard chessBoard, Piece piece) {
  93. // TODO Auto-generated method stub
  94. return null;
  95. }
  96.  
  97. }
  98.  
  99. //A class to validate if move made by the player is a legal one
  100.  
  101. public class MovementValidator {
  102.  
  103. private MovementGenerator movementGenerator;
  104.  
  105. public boolean isValidMove(ChessBoard board, Move move) {
  106. Square source = move.getSource();
  107. Square dest = move.getDestination();
  108. if (outsideBoard(source) || outsideBoard(dest))
  109. return false;
  110. // can't kill own pieces
  111. if (board.pieceAt(source).getColor() == board.pieceAt(dest).getColor())
  112. return false;
  113. // if dest empty then proceed(should also check for checkmate condition
  114. // in this case)
  115. if (dest.getState() == SquareState.EMPTY)
  116. return true;
  117. // try to kill the opponent
  118. List<Square> possiblePositions = movementGenerator
  119. .getPossibleMoves(move.getSourcePiece());
  120. if (!possiblePositions.contains(dest))
  121. return false;
  122. return true;
  123. }
  124.  
  125. private boolean outsideBoard(Square square) {
  126. int x = square.getRow();
  127. int y = square.getCol();
  128. if (x < 0 || x > 8 || y > 0 || y > 8)
  129. return false;
  130. return true;
  131. }
  132. }
  133.  
  134. public abstract class Piece {
  135. private PieceType type;
  136. private Color color;
  137. private Square position;
  138.  
  139. /**
  140. * @param move to be checked
  141. * @return if move is valid
  142. */
  143. public abstract boolean isValidMove(Move move);
  144.  
  145. /**
  146. * @return List of moves
  147. */
  148. public abstract List<Move> getValidMoves();
  149.  
  150. public void move(ChessBoard chessBoard, Square dest) {
  151. //place the piece from its position to dest on the board
  152. Move move;
  153.  
  154. /*Instantiate move*/
  155.  
  156. if (validMove(move)){
  157. //Make the move
  158. }
  159. }
  160. }
Add Comment
Please, Sign In to add comment