옵션 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | import java.util.ArrayList; public class Queen extends Piece{ public Queen(int ix, int iy, int c, Board b) { super(PieceCode.QUEEN, ix, iy, c, b); } // method implements abstract method in Piece class public ArrayList<Move> availableMoves() { return queen(); } // method to return Vector of legal moves for a queen private ArrayList<Move> queen() { int x = getX(); int y = getY(); // otherwise create a new vector to store legal moves ArrayList<Move> v = new ArrayList<Move>(); // set up m to refer to a Move object Move m = null; // legal moves to go from x,y to unoccupied places int[][] avaiableMoves = { {x+1,y}, {x+2,y}, {x+3,y}, {x+4,y}, {x+5,y}, {x+6,y}, {x+7,y}, //Positive x axis {x-1,y}, {x-2,y}, {x-3,y}, {x-4,y}, {x-5,y}, {x-6,y}, {x-7,y}, //Negative x axis {x,y+1}, {x,y+2}, {x,y+3}, {x,y+4}, {x,y+5}, {x,y+6}, {x,y+7}, //Positive y axis {x,y-1}, {x,y-2}, {x,y-3}, {x,y-4}, {x,y-5}, {x,y-6}, {x,y-7}, //Negative y axis {x+1,y+1}, {x+2,y+2}, {x+3,y+3}, {x+4,y+4}, {x+5,y+5}, {x+6,y+6}, {x+7,y+7}, //Positive right diagonal {x-1,y-1}, {x-2,y-2}, {x-3,y-3}, {x-4,y-4}, {x-5,y-5}, {x-6,y-6}, {x-7,y-7}, //Negative left diagonal {x-1,y+1}, {x-2,y+2}, {x-3,y+3}, {x-4,y+4}, {x-5,y+5}, {x-6,y+6}, {x-7,y+7}, //Positive left diagonal {x+1,y-1}, {x+2,y-2}, {x+3,y-3}, {x+4,y-4}, {x+5,y-5}, {x+6,y-6}, {x+7,y-7}, //Negative right diagonal }; //using the array of moves, find all the possible moves in the array that piece can from its position for (int i = 0, j = avaiableMoves.length; i<j; i++){ int posX = avaiableMoves[i][0]; int posY = avaiableMoves[i][1]; //using for loop to check whether any of available moves in the list are occupied by other pieces or not when it's not out of range if (!getBoard().outOfRange(posX,posY) && getBoard().occupied(posX,posY) && (getBoard().getPiece(posX,posY).getColour() !=this.getColour())) { v.add(new Move(this, x,y,posX,posY,true)); } //if occupied, then return false else if (!getBoard().outOfRange(posX,posY) && !getBoard().occupied(posX,posY)) { v.add(new Move(this, x,y,posX,posY,false)); } } if (v.isEmpty()) return null; return v; } } | cs |