SHARE
TWEET

Untitled

a guest Aug 12th, 2017 48 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.domain;
  2.  
  3. import java.util.List;
  4.  
  5. public abstract class BishopMoveCalculator {
  6.  
  7.     public List<Coordinate> calculate(int aRow, int aColumn, List<Coordinate> aCandidates) {
  8.         while (isValidCoordinate(aRow, aColumn)) {
  9.             aCandidates.add(new Coordinate(aRow, aColumn));
  10.  
  11.             aRow = nextRow(aRow);
  12.             aColumn = nextColumn(aColumn);
  13.         }
  14.  
  15.         return aCandidates;
  16.     }
  17.  
  18.     public abstract boolean isValidCoordinate(int aRow, int aColumn);
  19.     public abstract int nextRow(int aCurrentRow);
  20.     public abstract int nextColumn(int aCurrentColumn);
  21.  
  22. }
  23.  
  24.  
  25. ...
  26.  
  27.  
  28. package com.domain;
  29.  
  30. import java.util.ArrayList;
  31. import java.util.List;
  32.  
  33. public class WhiteBishop extends GamePiece {
  34.  
  35.     public WhiteBishop(GameBoard aBoard, Coordinate aTile) {
  36.         super(PieceType.WHITE_BISHOP, aTile, aBoard);
  37.     }
  38.  
  39.     @Override
  40.     public List<Coordinate> candidateMoves() {
  41.         List<Coordinate> candidates = new ArrayList<Coordinate>();
  42.  
  43.         candidates = calculateForwardLeftMoves(candidates);
  44.         candidates = calculateForwardRightMoves(candidates);
  45.         candidates = calculateBackwardLeftMoves(candidates);
  46.         candidates = calculateBackwardRightMoves(candidates);
  47.  
  48.         return candidates;
  49.     }
  50.  
  51.     private List<Coordinate> calculateForwardLeftMoves(List<Coordinate> aCandidates) {
  52.         int currentRow = super.row() - 1;
  53.         int currentColumn = super.column() - 1;
  54.  
  55.         BishopMoveCalculator calculator = new ForwardLeftMoveCalculator();
  56.         return calculator.calculate(currentRow, currentColumn, aCandidates);
  57.     }
  58.  
  59.     private List<Coordinate> calculateForwardRightMoves(List<Coordinate> aCandidates) {
  60.         int currentRow = super.row() - 1;
  61.         int currentColumn = super.column() + 1;
  62.  
  63.         BishopMoveCalculator calculator = new ForwardRightMoveCalculator(super.board);
  64.         return calculator.calculate(currentRow, currentColumn, aCandidates);
  65.     }
  66.  
  67.     private List<Coordinate> calculateBackwardLeftMoves(List<Coordinate> aCandidates) {
  68.         int currentRow = super.row() + 1;
  69.         int currentColumn = super.column() - 1;
  70.  
  71.         BishopMoveCalculator calculator = new BackwardLeftMoveCalculator(super.board);
  72.         return calculator.calculate(currentRow, currentColumn, aCandidates);
  73.     }
  74.  
  75.     private List<Coordinate> calculateBackwardRightMoves(List<Coordinate> aCandidates) {
  76.         int currentRow = super.row() + 1;
  77.         int currentColumn = super.column() + 1;
  78.  
  79.         BishopMoveCalculator calculator = new BackwardRightMoveCalculator(super.board);
  80.         return calculator.calculate(currentRow, currentColumn, aCandidates);
  81.     }
  82.  
  83.     @Override
  84.     protected GamePiece constructNewPieceToMove(Coordinate aNewLocation) {
  85.         return new WhiteBishop(board, aNewLocation);
  86.     }
  87.  
  88.     private class ForwardLeftMoveCalculator extends BishopMoveCalculator {
  89.         @Override
  90.         public boolean isValidCoordinate(int aRow, int aColumn) {
  91.             return (aRow >= 0) && (aColumn >= 0);
  92.         }
  93.  
  94.         @Override
  95.         public int nextRow(int aCurrentRow) {
  96.             return --aCurrentRow;
  97.         }
  98.  
  99.         @Override
  100.         public int nextColumn(int aCurrentColumn) {
  101.             return --aCurrentColumn;
  102.         }
  103.     }
  104.  
  105.     private class ForwardRightMoveCalculator extends BishopMoveCalculator {
  106.         private final GameBoard board;
  107.  
  108.         public ForwardRightMoveCalculator(GameBoard aBoard) {
  109.             board = aBoard;
  110.         }
  111.  
  112.         @Override
  113.         public boolean isValidCoordinate(int aRow, int aColumn) {
  114.             return (aRow >= 0) && (aColumn < board.columns());
  115.         }
  116.  
  117.         @Override
  118.         public int nextRow(int aCurrentRow) {
  119.             return --aCurrentRow;
  120.         }
  121.  
  122.         @Override
  123.         public int nextColumn(int aCurrentColumn) {
  124.             return ++aCurrentColumn;
  125.         }
  126.     }
  127.  
  128.     private class BackwardLeftMoveCalculator extends BishopMoveCalculator {
  129.         private final GameBoard board;
  130.  
  131.         public BackwardLeftMoveCalculator(GameBoard aBoard) {
  132.             board = aBoard;
  133.         }
  134.  
  135.         @Override
  136.         public boolean isValidCoordinate(int aRow, int aColumn) {
  137.             return (aRow < board.rows()) && (aColumn >= 0);
  138.         }
  139.  
  140.         @Override
  141.         public int nextRow(int aCurrentRow) {
  142.             return ++aCurrentRow;
  143.         }
  144.  
  145.         @Override
  146.         public int nextColumn(int aCurrentColumn) {
  147.             return --aCurrentColumn;
  148.         }
  149.     }
  150.  
  151.     private class BackwardRightMoveCalculator extends BishopMoveCalculator {
  152.         private final GameBoard board;
  153.  
  154.         public BackwardRightMoveCalculator(GameBoard aBoard) {
  155.             board = aBoard;
  156.         }
  157.  
  158.         @Override
  159.         public boolean isValidCoordinate(int aRow, int aColumn) {
  160.             return (aRow < board.rows()) && (aColumn < board.columns());
  161.         }
  162.  
  163.         @Override
  164.         public int nextRow(int aCurrentRow) {
  165.             return ++aCurrentRow;
  166.         }
  167.  
  168.         @Override
  169.         public int nextColumn(int aCurrentColumn) {
  170.             return ++aCurrentColumn;
  171.         }
  172.     }
  173.  
  174. }
RAW Paste Data
Top