sharivan

DraughtsPiece class for Gamezer Checkers rules

Oct 24th, 2016 (edited)
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.02 KB | None | 0 0
  1. package gz.common.logic.boards.draughts;
  2.  
  3. import java.util.List;
  4.  
  5. import common.util.Tree;
  6. import common.util.Tree.Node;
  7. import gz.common.logic.boards.BoardMove;
  8. import gz.common.logic.boards.BoardPiece;
  9. import gz.common.logic.boards.BoardPosition;
  10. import net.jcip.annotations.NotThreadSafe;
  11.  
  12. @NotThreadSafe
  13. public abstract class DraughtsPiece extends BoardPiece {
  14.  
  15.     private static boolean isValidMove(DraughtsGame game, DraughtsPiece piece, int srcRow, int srcCol, int dr, int dc) {
  16.         if (!game.isValidPos(srcRow + dr, srcCol + dc))
  17.             return false;
  18.  
  19.         if (game.getBoardInternal(srcRow + dr, srcCol + dc) != null)
  20.             return false;
  21.  
  22.         if (piece.isKing())
  23.             return true;
  24.  
  25.         if (piece.isBlack())
  26.             return dr < 0;
  27.  
  28.         return dr > 0;
  29.     }
  30.  
  31.     protected static int signal(int x) {
  32.         if (x > 0)
  33.             return 1;
  34.  
  35.         if (x < 0)
  36.             return -1;
  37.  
  38.         return 0;
  39.     }
  40.  
  41.     boolean captured;
  42.  
  43.     protected DraughtsPiece(DraughtsGame game, int playerIndex) {
  44.         super(game, playerIndex);
  45.     }
  46.  
  47.     protected DraughtsPiece(DraughtsGame game, int playerIndex, BoardPosition position) {
  48.         super(game, playerIndex, position);
  49.     }
  50.  
  51.     @Override
  52.     protected void destroy() {
  53.         super.destroy();
  54.     }
  55.  
  56.     protected abstract void generateCaptureList(int dr, int dc, Node<BoardPosition> node);
  57.  
  58.     @Override
  59.     protected final void generateMoveList(List<BoardMove> moveList) {
  60.         DraughtsGame game = getGame();
  61.  
  62.         Tree<BoardPosition> move = new Tree<>(getPosition());
  63.         Node<BoardPosition> root = move.getRoot();
  64.         generateCaptureList(0, 0, root);
  65.  
  66.         if (move.getChildCount() > 0) {
  67.             if (!game.hasCaptures) {
  68.                 moveList.clear();
  69.                 game.hasCaptures = true;
  70.             }
  71.             game.addMovesFromTree(move);
  72.         }
  73.  
  74.         if (!game.hasCaptures)
  75.             generateNonCaptureList(moveList);
  76.     }
  77.  
  78.     protected abstract void generateNonCaptureList(List<BoardMove> moveList);
  79.  
  80.     @Override
  81.     public DraughtsGame getGame() {
  82.         return (DraughtsGame) super.getGame();
  83.     }
  84.  
  85.     protected final boolean isAhead(int dstRow) {
  86.         if (isWhite())
  87.             return dstRow > getPosition().getRow();
  88.  
  89.         if (isBlack())
  90.             return dstRow < getPosition().getRow();
  91.  
  92.         return false;
  93.     }
  94.  
  95.     public final boolean isBlack() {
  96.         return getPlayerIndex() == 1;
  97.     }
  98.  
  99.     public final boolean isBlackKing() {
  100.         return isKing() && isBlack();
  101.     }
  102.  
  103.     public final boolean isBlackMan() {
  104.         return isMan() && isBlack();
  105.     }
  106.  
  107.     public abstract boolean isKing();
  108.  
  109.     public final boolean isMan() {
  110.         return !isKing();
  111.     }
  112.  
  113.     public final boolean isSystemBlocked() {
  114.         DraughtsGame game = getGame();
  115.         if (game == null)
  116.             return false;
  117.  
  118.         BoardPosition position = getPosition();
  119.         if (!game.isValidPos(position))
  120.             return false;
  121.  
  122.         int row = position.getRow();
  123.         int col = position.getCol();
  124.  
  125.         game.setBoardInternal(row, col, null);
  126.         try {
  127.             for (int dr = -1; dr <= 1; dr += 2)
  128.                 for (int dc = -1; dc <= 1; dc += 2)
  129.                     if (isValidMove(game, this, row, col, dr, dc)) {
  130.                         int row1 = row + dr;
  131.                         int col1 = col + dc;
  132.                         for (int dr1 = -1; dr1 <= 1; dr1 += 2)
  133.                             for (int dc1 = -1; dc1 <= 1; dc1 += 2) {
  134.                                 int row2 = row1 + dr1;
  135.                                 int col2 = col1 + dc1;
  136.  
  137.                                 if (row1 == row2 && col1 == col2)
  138.                                     continue;
  139.  
  140.                                 if (!game.isValidPos(row2, col2))
  141.                                     continue;
  142.  
  143.                                 DraughtsPiece other = game.getBoardInternal(row2, col2);
  144.                                 if (other == null)
  145.                                     continue;
  146.  
  147.                                 if (other.getPlayerIndex() == getPlayerIndex())
  148.                                     continue;
  149.  
  150.                                 if (isValidMove(game, other, row2, col2, -2 * dr1, -2 * dc1))
  151.                                     return true;
  152.                             }
  153.  
  154.                         return false;
  155.                     }
  156.  
  157.             return true;
  158.         } finally {
  159.             game.setBoardInternal(row, col, this);
  160.         }
  161.     }
  162.  
  163.     protected abstract BoardPosition isValidSingleCapture(int dstRow, int dstCol, boolean firstCapture);
  164.  
  165.     public final boolean isWhite() {
  166.         return getPlayerIndex() == 0;
  167.     }
  168.  
  169.     public final boolean isWhiteKing() {
  170.         return isKing() && isWhite();
  171.     }
  172.  
  173.     public final boolean isWhiteMan() {
  174.         return isMan() && isWhite();
  175.     }
  176.  
  177.     protected void release() {
  178.         getGame().release(this);
  179.     }
  180.  
  181. }
Advertisement
Add Comment
Please, Sign In to add comment