private void setListForPawn() { moveList.clear(); if (color == 1) { int row = square.getRow(); int column = square.getColumn(); int cell1 = square.getCellFromCoord(row + 1, column); int cell2 = square.getCellFromCoord(row + 2, column); int cell3 = square.getCellFromCoord(row + 1, column + 1); int cell4 = square.getCellFromCoord(row + 1, column - 1); Square s1 = (Square) board.getComponent(cell1); Square s2 = (Square) board.getComponent(cell2); Square s3 = (Square) board.getComponent(cell3); Square s4 = (Square) board.getComponent(cell4); if (s1.getComponentCount() == 0) { moveList.add(s1.getPosition()); squareList.add(s1); } if (s2.getComponentCount() == 0) { moveList.add(s2.getPosition()); squareList.add(s2); } if ((s3.getComponentCount() == 1) && ((Piece) s3.getComponent(0)).getColor() == 0) { moveList.add(s3.getPosition()); squareList.add(s3); } if ((s4.getComponentCount() == 1) && ((Piece) s4.getComponent(0)).getColor() == 0) { moveList.add(s4.getPosition()); squareList.add(s4); } } if (color == 0) { int row = square.getRow(); int column = square.getColumn(); int cell1 = square.getCellFromCoord(row - 1, column); int cell2 = square.getCellFromCoord(row - 2, column); int cell3 = square.getCellFromCoord(row - 1, column + 1); int cell4 = square.getCellFromCoord(row - 1, column - 1); Square s1 = (Square) board.getComponent(cell1); Square s2 = (Square) board.getComponent(cell2); Square s3 = (Square) board.getComponent(cell3); Square s4 = (Square) board.getComponent(cell4); if (s1.getComponentCount() == 0) { moveList.add(s1.getPosition()); squareList.add(s1); } if (s2.getComponentCount() == 0) { moveList.add(s2.getPosition()); squareList.add(s2); } if ((s3.getComponentCount() == 1) && ((Piece) s3.getComponent(0)).getColor() == 1) { moveList.add(s3.getPosition()); squareList.add(s3); } if ((s4.getComponentCount() == 1) && ((Piece) s4.getComponent(0)).getColor() == 1) { moveList.add(s4.getPosition()); squareList.add(s4); } } }
/** * Put 6 ninjas into different random positions on the map or randomize current ninjas locations. * * @param initial if first fill up the map with 6 ninjas, set this to true. */ private void assignNinjas(boolean initial) { int rRow; int rCol; if (initial) { for (int i = 0; i < 6; i++) { do { rRow = random.nextInt(8); rCol = random.nextInt(8); } while (isOccupied(rRow, rCol)); Ninja ninja = new Ninja(rRow, rCol); map[rRow][rCol] = ninja; if (debug) { ninja.setVisible(true); } // Store the ninja to the array, and mark the new location as // occupied. ninjas.add(ninja); occupiedLocations.add(map[rRow][rCol]); } } else { // If there are ninjas surrond the original position, move them. for (Square location : surroundSpy) { if (isNinja(map[location.getRow()][location.getCol()])) { do { rRow = random.nextInt(8); rCol = random.nextInt(8); if (isNinja(map[rRow][rCol])) { occupiedLocations.add(map[rRow][rCol]); } } while (isOccupied(rRow, rCol)); map[rRow][rCol] = map[location.getRow()][location.getCol()]; Ninja ninja = (Ninja) map[rRow][rCol]; map[location.getRow()][location.getCol()] = new Square(debug, rRow, rCol); ninja.setRow(rRow); ninja.setCol(rCol); if (debug) { ninja.setVisible(true); } occupiedLocations.add(map[rRow][rCol]); } } } }
public boolean queensideClear() { boolean[] flags = ChessGameDemo.getFlags(); int row = square.getRow(); int column = square.getColumn(); if ((row == 1) && (flags[0] || flags[4])) { return false; } if ((row == 8) && (flags[2] || flags[5])) { return false; } if (((row == 1) && (column == 5)) || ((row == 8) && (column == 5))) { for (int i = 1; i < 4; i++) { int r = row; int c = column - i; int cell = square.getCellFromCoord(r, c); Square sq = (Square) board.getComponent(cell); if ((sq.getComponentCount() == 1) || (r < 1)) { return false; } else if (color == 1) { if ((board.getBlackCheckList()).contains(sq.getPosition())) { return false; } } else { if ((board.getWhiteCheckList()).contains(sq.getPosition())) { return false; } } } return true; } return false; }
public void pulseOccurred(Square source) { int r = source.getRow(); int c = source.getColumn(); int pulsePower = (int) (source.getPower() + source.getPulse()); pulsePower = (int) (pulsePower - Math.ceil(pulsePower * PULSE_DECAY)); // System.out.println("Pulse Occured at ("+c+", "+r+") with Power "+pulsePower); if (pulsePower > 0) { // Top Left Corner if (r - 1 >= 0 && !board[r - 1][c].isPulsing()) board[r - 1][c].dieWithPulse(pulsePower); if (c - 1 >= 0 && !board[r][c - 1].isPulsing()) board[r][c - 1].dieWithPulse(pulsePower); if (r - 1 >= 0 && c - 1 >= 0 && !board[r - 1][c - 1].isPulsing()) board[r - 1][c - 1].dieWithPulse(pulsePower); // Bottom Right Corner if (r + 1 < NUM_OF_SQUARES && !board[r + 1][c].isPulsing()) board[r + 1][c].dieWithPulse(pulsePower); if (c + 1 < NUM_OF_SQUARES && !board[r][c + 1].isPulsing()) board[r][c + 1].dieWithPulse(pulsePower); if (r + 1 < NUM_OF_SQUARES && c + 1 < NUM_OF_SQUARES && !board[r + 1][c + 1].isPulsing()) board[r + 1][c + 1].dieWithPulse(pulsePower); if (r + 1 < NUM_OF_SQUARES && c - 1 >= 0 && !board[r + 1][c - 1].isPulsing()) board[r + 1][c - 1].dieWithPulse(pulsePower); if (r - 1 >= 0 && c + 1 < NUM_OF_SQUARES && !board[r - 1][c + 1].isPulsing()) board[r - 1][c + 1].dieWithPulse(pulsePower); } }
/** * Check if the given Square is a legal attack for this Piece * * @param threatened The Square to attack * @return If this Square is a legal attack */ public boolean isLegalAttack(Square threatened) { if (m_name.equals("Pawn")) { if (m_board.getGame().isStaleLegalDests()) m_board.getGame().genLegalDests(); if (m_captured) return false; if (threatened.getCol() == m_curSquare.getCol()) return false; else return (isLegalDest(threatened) || (threatened.getRow() - m_curSquare.getRow() == ((isBlack()) ? -1 : 1) && Math.abs(threatened.getCol() - m_curSquare.getCol()) == 1)); } return isLegalDest(threatened); }
private void setAttacksForPawn() { moveList.clear(); squareList.clear(); if (color == 1) { int row = square.getRow(); int column = square.getColumn(); int cell1 = square.getCellFromCoord(row + 1, column + 1); int cell2 = square.getCellFromCoord(row + 1, column - 1); String c1 = square.getPositionWithArg(cell1); String c2 = square.getPositionWithArg(cell2); if ((column + 1) < 9) { moveList.add(c1); } if ((column - 1) > 0) { moveList.add(c2); } } if (color == 0) { int row = square.getRow(); int column = square.getColumn(); int cell_1 = square.getCellFromCoord(row - 1, column + 1); int cell_2 = square.getCellFromCoord(row - 1, column - 1); String c1 = square.getPositionWithArg(cell_1); String c2 = square.getPositionWithArg(cell_2); if ((column + 1) < 9) { moveList.add(c1); } if ((column - 1) > 0) { moveList.add(c2); } } }
/** * Add a legal destination to the ArrayList. * * @param dest The Square to be added to the List * @return If the Square was successfully added to the ArrayList */ public boolean addLegalDest(Square dest) { // If the Square is not habitable, don't add it, but return true so we // can move past it. if (!dest.isHabitable()) return true; if (dest.getRow() == m_curSquare.getRow() && dest.getCol() == m_curSquare.getCol()) return false; if (dest.isOccupied() && dest.getPiece().isBlack() == isBlack()) { // If the destination has a Piece from the same team, we must be // guarding that Piece getGuardSquares().add(dest); return false; } // Otherwise, add the Piece and return true. getLegalDests().add(dest); return true; }
/** * Check for array out of bound, get only the moveable locations from the current location. * * @param object the Square object of the location on the map. * @return the ArrayList of valid locations. */ private ArrayList<Square> getValidLocations(Square object) { int row = object.getRow(); int col = object.getCol(); ArrayList<Square> validLocations = new ArrayList<Square>(); // Only add reachable directions to the array. if (row - 1 >= 0 && col <= 8 & col >= 0) { validLocations.add(map[row - 1][col]); } if (row + 1 <= 8 && col <= 8 & col >= 0) { validLocations.add(map[row + 1][col]); } if (col - 1 >= 0 && row >= 0 && row <= 8) { validLocations.add(map[row][col - 1]); } if (col + 1 <= 8 && row >= 0 && row <= 8) { validLocations.add(map[row][col + 1]); } return validLocations; }
private void populate() { // populate with MovePawns for (int i = -2; i <= 2; i++) { for (int j = -2; j <= 2; j++) { Square tmp = setting.getPawn(mover, setting).getSquare(); Board settingClone = new BoardImpl((BoardImpl) setting); Player moverClone = new PlayerImpl(mover); MovePawn tentative = new MovePawnImpl(tmp.getCol() + j, tmp.getRow() + i, moverClone, settingClone); if (tentative.isValid()) { pawnBackerList.add(new StateImpl(settingClone, moverClone, tentative)); } } } for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { Board settingClone = new BoardImpl((BoardImpl) setting); Player moverClone = new PlayerImpl(mover); PlaceWall vTentative = new PlaceWallImpl(i, j, Wall.VERTICAL, moverClone, settingClone); settingClone = new BoardImpl((BoardImpl) setting); moverClone = new PlayerImpl(mover); PlaceWall hTentative = new PlaceWallImpl(i, j, Wall.HORIZONTAL, moverClone, settingClone); if (vTentative.isValid()) { wallBackerList.add(new StateImpl(settingClone, moverClone, vTentative)); } if (hTentative.isValid()) { wallBackerList.add(new StateImpl(settingClone, moverClone, hTentative)); } } } }
private void setListForKing() { moveList.clear(); squareList.clear(); int row = square.getRow(); int column = square.getColumn(); int c1, c2, c3, c4, c5, c6, c7, c8; String cell1, cell2, cell3, cell4, cell5, cell6, cell7, cell8; if (((row + 1) < 9) && (column + 1) < 9) { c1 = square.getCellFromCoord(row + 1, column + 1); cell1 = square.getPositionWithArg(c1); Square s = (Square) board.getComponent(c1); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell1); squareList.add(s); } } else { moveList.add(cell1); squareList.add(s); } } if (((row + 1) < 9) && (column - 1) > 0) { c2 = square.getCellFromCoord(row + 1, column - 1); cell2 = square.getPositionWithArg(c2); Square s = (Square) board.getComponent(c2); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell2); squareList.add(s); } } else { moveList.add(cell2); squareList.add(s); } } if ((row + 1) < 9) { c3 = square.getCellFromCoord(row + 1, column); cell3 = square.getPositionWithArg(c3); Square s = (Square) board.getComponent(c3); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell3); squareList.add(s); } } else { moveList.add(cell3); squareList.add(s); } } if ((column + 1) < 9) { c4 = square.getCellFromCoord(row, column + 1); cell4 = square.getPositionWithArg(c4); Square s = (Square) board.getComponent(c4); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell4); squareList.add(s); } } else { moveList.add(cell4); squareList.add(s); } } if ((column - 1) > 0) { c5 = square.getCellFromCoord(row, column - 1); cell5 = square.getPositionWithArg(c5); Square s = (Square) board.getComponent(c5); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell5); squareList.add(s); } } else { moveList.add(cell5); squareList.add(s); } } if (((row - 1) > 0) && (column - 1) > 0) { c6 = square.getCellFromCoord(row - 1, column - 1); cell6 = square.getPositionWithArg(c6); Square s = (Square) board.getComponent(c6); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell6); squareList.add(s); } } else { moveList.add(cell6); squareList.add(s); } } if (((row - 1) > 0) && (column + 1) < 9) { c7 = square.getCellFromCoord(row - 1, column + 1); cell7 = square.getPositionWithArg(c7); Square s = (Square) board.getComponent(c7); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell7); squareList.add(s); } } else { moveList.add(cell7); squareList.add(s); } } if ((row - 1) > 0) { c8 = square.getCellFromCoord(row - 1, column); cell8 = square.getPositionWithArg(c8); Square s = (Square) board.getComponent(c8); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell8); squareList.add(s); } } else { moveList.add(cell8); squareList.add(s); } } if (queensideClear()) { int cell = square.getCellFromCoord(1, 3); String c = square.getPositionWithArg(cell); moveList.add(c); cell = square.getCellFromCoord(8, 3); c = square.getPositionWithArg(cell); moveList.add(c); } if (kingsideClear()) { int cell = square.getCellFromCoord(1, 7); String c = square.getPositionWithArg(cell); moveList.add(c); cell = square.getCellFromCoord(8, 7); c = square.getPositionWithArg(cell); moveList.add(c); } }
private void setListForKnight() { moveList.clear(); squareList.clear(); /*switch(square.getCellNumber()) { case 56 : moveList.add("c2");moveList.add("b3"); break; case 57 : moveList.add("d2");moveList.add("a3");moveList.add("c3"); break; case 58 : moveList.add("a2");moveList.add("e2");moveList.add("b3");moveList.add("d3"); break; case 59 : moveList.add("b2");moveList.add("f2");moveList.add("c3");moveList.add("e3"); break; case 60 : moveList.add("c2");moveList.add("g2");moveList.add("d3");moveList.add("f3"); break; case 61 : moveList.add("d2");moveList.add("h2");moveList.add("e3");moveList.add("g3"); break; case 62 : moveList.add("e2");moveList.add("f3");moveList.add("h3"); break; case 63 : moveList.add("f2");moveList.add("g3"); break; case 48 : moveList.add("c1");moveList.add("c3");moveList.add("b4"); break; case 49 : moveList.add("d1");moveList.add("d3");moveList.add("a4");moveList.add("c4"); break; case 50 : moveList.add("a1");moveList.add("e1");moveList.add("a3");moveList.add("e3");moveList.add("b4");moveList.add("d4"); break; case 51 : moveList.add("b1");moveList.add("f1");moveList.add("b3");moveList.add("f3");moveList.add("c4");moveList.add("e4"); break; case 52 : moveList.add("c1");moveList.add("g1");moveList.add("c3");moveList.add("g3");moveList.add("d4");moveList.add("f4"); break; case 53 : moveList.add("d1");moveList.add("h1");moveList.add("d3");moveList.add("h3");moveList.add("e4");moveList.add("g4"); break; case 54 : moveList.add("e1");moveList.add("e3");moveList.add("f4");moveList.add("h4"); break; case 55 : moveList.add("f1");moveList.add("f3");moveList.add("g4"); break; case 40 : moveList.add("b1");moveList.add("c2");moveList.add("c4");moveList.add("b5"); break; case 41 : moveList.add("a1");moveList.add("c1");moveList.add("d2");moveList.add("d4");moveList.add("a5");moveList.add("c5"); break; case 42 : moveList.add("b1");moveList.add("d1");moveList.add("a2");moveList.add("e2");moveList.add("a4");moveList.add("e4");moveList.add("b5");moveList.add("d5"); break; case 43 : moveList.add("c1");moveList.add("e1");moveList.add("b2");moveList.add("f2");moveList.add("b4");moveList.add("f4");moveList.add("c5");moveList.add("e5"); break; case 44 : moveList.add("d1");moveList.add("f1");moveList.add("c2");moveList.add("g2");moveList.add("c4");moveList.add("g4");moveList.add("d5");moveList.add("f5"); break; case 45 : moveList.add("e1");moveList.add("g1");moveList.add("d2");moveList.add("h2");moveList.add("d4");moveList.add("h4");moveList.add("e5");moveList.add("g5"); break; case 46 : moveList.add("f1");moveList.add("h1");moveList.add("e2");moveList.add("e4");moveList.add("f5");moveList.add("h5"); break; case 47 : moveList.add("g1");moveList.add("f2");moveList.add("f4");moveList.add("g5"); break; case 32 : moveList.add("b2");moveList.add("c3");moveList.add("c5");moveList.add("b6"); break; case 33 : moveList.add("a2");moveList.add("c2");moveList.add("d3");moveList.add("d5");moveList.add("a6");moveList.add("c6"); break; case 34 : moveList.add("b2");moveList.add("d2");moveList.add("a3");moveList.add("e3");moveList.add("a5");moveList.add("e5");moveList.add("b6");moveList.add("d6"); break; case 35 : moveList.add("c2");moveList.add("e2");moveList.add("b3");moveList.add("f3");moveList.add("b5");moveList.add("f5");moveList.add("c6");moveList.add("e6"); break; case 36 : moveList.add("d2");moveList.add("f2");moveList.add("c3");moveList.add("g3");moveList.add("c5");moveList.add("g5");moveList.add("d6");moveList.add("f6"); break; case 37 : moveList.add("e2");moveList.add("g2");moveList.add("d3");moveList.add("h3");moveList.add("d5");moveList.add("h5");moveList.add("e6");moveList.add("g6"); break; case 38 : moveList.add("f2");moveList.add("h2");moveList.add("e3");moveList.add("e5");moveList.add("f6");moveList.add("h6"); break; case 39 : moveList.add("g2");moveList.add("f3");moveList.add("f5");moveList.add("g6"); break; case 24 : moveList.add("b3");moveList.add("c4");moveList.add("c6");moveList.add("b7"); break; case 25 : moveList.add("a3");moveList.add("c3");moveList.add("d4");moveList.add("d6");moveList.add("a7");moveList.add("c7"); break; case 26 : moveList.add("b3");moveList.add("d3");moveList.add("a4");moveList.add("e4");moveList.add("a6");moveList.add("e6");moveList.add("b7");moveList.add("d7"); break; case 27 : moveList.add("c3");moveList.add("e3");moveList.add("b4");moveList.add("f4");moveList.add("b6");moveList.add("f6");moveList.add("c7");moveList.add("e7"); break; case 28 : moveList.add("d3");moveList.add("f3");moveList.add("c4");moveList.add("g4");moveList.add("c6");moveList.add("g6");moveList.add("d7");moveList.add("f7"); break; case 29 : moveList.add("e3");moveList.add("g3");moveList.add("d4");moveList.add("h4");moveList.add("d6");moveList.add("h6");moveList.add("e7");moveList.add("g7"); break; case 30 : moveList.add("f3");moveList.add("h3");moveList.add("e4");moveList.add("e6");moveList.add("f7");moveList.add("h7"); break; case 31 : moveList.add("g3");moveList.add("f4");moveList.add("f6");moveList.add("g7"); break; case 16 : moveList.add("b4");moveList.add("c5");moveList.add("c7");moveList.add("b8"); break; case 17 : moveList.add("a4");moveList.add("c4");moveList.add("d5");moveList.add("d7");moveList.add("a8");moveList.add("c8"); break; case 18 : moveList.add("b4");moveList.add("d4");moveList.add("a5");moveList.add("e5");moveList.add("a7");moveList.add("e7");moveList.add("b8");moveList.add("d8"); break; case 19 : moveList.add("c4");moveList.add("e4");moveList.add("b5");moveList.add("f5");moveList.add("b7");moveList.add("f7");moveList.add("c8");moveList.add("e8"); break; case 20 : moveList.add("d4");moveList.add("f4");moveList.add("c5");moveList.add("g5");moveList.add("c7");moveList.add("g7");moveList.add("d8");moveList.add("f8"); break; case 21 : moveList.add("e4");moveList.add("g4");moveList.add("d5");moveList.add("h5");moveList.add("d7");moveList.add("h7");moveList.add("e8");moveList.add("g8"); break; case 22 : moveList.add("f4");moveList.add("h4");moveList.add("e5");moveList.add("e7");moveList.add("f8");moveList.add("h8"); break; case 23 : moveList.add("g4");moveList.add("f5");moveList.add("f7");moveList.add("g8"); break; case 8 : moveList.add("b5");moveList.add("c6");moveList.add("c8"); break; case 9 : moveList.add("a5");moveList.add("c5");moveList.add("d6");moveList.add("d8"); break; case 10 : moveList.add("b5");moveList.add("d5");moveList.add("a6");moveList.add("e6");moveList.add("a8");moveList.add("e8"); break; case 11 : moveList.add("c5");moveList.add("e5");moveList.add("b6");moveList.add("f6");moveList.add("b8");moveList.add("f8"); break; case 12 : moveList.add("d5");moveList.add("f5");moveList.add("c6");moveList.add("g6");moveList.add("c8");moveList.add("g8"); break; case 13 : moveList.add("e5");moveList.add("g5");moveList.add("d6");moveList.add("h6");moveList.add("d8");moveList.add("h8"); break; case 14 : moveList.add("f5");moveList.add("h5");moveList.add("e6");moveList.add("e8"); break; case 15 : moveList.add("g5");moveList.add("f6");moveList.add("f8"); break; case 0 : moveList.add("b6");moveList.add("c7"); break; case 1 : moveList.add("a6");moveList.add("c6");moveList.add("d7"); break; case 2 : moveList.add("b6");moveList.add("d6");moveList.add("a7");moveList.add("e7"); break; case 3 : moveList.add("c6");moveList.add("e6");moveList.add("b7");moveList.add("f7"); break; case 4 : moveList.add("d6");moveList.add("f6");moveList.add("c7");moveList.add("g7"); break; case 5 : moveList.add("e6");moveList.add("g6");moveList.add("d7");moveList.add("h7"); break; case 6 : moveList.add("f6");moveList.add("h6");moveList.add("e7");moveList.add("NS"); break; case 7 : moveList.add("g6");moveList.add("f7"); break; }*/ int row = square.getRow(); int column = square.getColumn(); int c1, c2, c3, c4, c5, c6, c7, c8; String cell1, cell2, cell3, cell4, cell5, cell6, cell7, cell8; if (((row + 1) < 9) && (column - 2) > 0) { c1 = square.getCellFromCoord(row + 1, column - 2); cell1 = square.getPositionWithArg(c1); Square s = (Square) board.getComponent(c1); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell1); squareList.add(s); } } else { moveList.add(cell1); squareList.add(s); } } if (((row + 2) < 9) && (column - 1) > 0) { c2 = square.getCellFromCoord(row + 2, column - 1); cell2 = square.getPositionWithArg(c2); Square s = (Square) board.getComponent(c2); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell2); squareList.add(s); } } else { moveList.add(cell2); squareList.add(s); } } if (((row + 1) < 9) && (column + 1) < 9) { c3 = square.getCellFromCoord(row + 2, column + 1); cell3 = square.getPositionWithArg(c3); Square s = (Square) board.getComponent(c3); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell3); squareList.add(s); } } else { moveList.add(cell3); squareList.add(s); } } if (((row + 1) < 9) && (column + 2) < 9) { c4 = square.getCellFromCoord(row + 1, column + 2); cell4 = square.getPositionWithArg(c4); Square s = (Square) board.getComponent(c4); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell4); squareList.add(s); } } else { moveList.add(cell4); squareList.add(s); } } if (((row - 1) > 0) && (column - 2) > 0) { c5 = square.getCellFromCoord(row - 1, column - 2); cell5 = square.getPositionWithArg(c5); Square s = (Square) board.getComponent(c5); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell5); squareList.add(s); } } else { moveList.add(cell5); squareList.add(s); } } if (((row - 2) > 0) && (column - 1) > 0) { c6 = square.getCellFromCoord(row - 2, column - 1); cell6 = square.getPositionWithArg(c6); Square s = (Square) board.getComponent(c6); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell6); squareList.add(s); } } else { moveList.add(cell6); squareList.add(s); } } if (((row - 2) > 0) && (column + 1) < 9) { c7 = square.getCellFromCoord(row - 2, column + 1); cell7 = square.getPositionWithArg(c7); Square s = (Square) board.getComponent(c7); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell7); squareList.add(s); } } else { moveList.add(cell7); squareList.add(s); } } if (((row - 1) > 0) && (column + 2) < 9) { c8 = square.getCellFromCoord(row - 1, column + 2); cell8 = square.getPositionWithArg(c8); Square s = (Square) board.getComponent(c8); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell8); squareList.add(s); } } else { moveList.add(cell8); squareList.add(s); } } }
private void setListForRook() { int row = square.getRow(); int column = square.getColumn(); L1: for (int i = 1; i < 8; i++) { int r = row; int c = column + i; int cell = square.getCellFromCoord(r, c); Square sq = (Square) board.getComponent(cell); if (c < 9) { if (sq.getComponentCount() == 0) { moveList.add(sq.getPositionWithArg(cell)); squareList.add(sq); } else { if (((Piece) sq.getComponent(0)).getColor() != color) { moveList.add(sq.getPositionWithArg(cell)); squareList.add(sq); } break L1; } } } L2: for (int i = 1; i < 8; i++) { int r = row; int c = column - i; int cell = square.getCellFromCoord(r, c); Square sq = (Square) board.getComponent(cell); if (c > 0) { if (sq.getComponentCount() == 0) { moveList.add(sq.getPositionWithArg(cell)); squareList.add(sq); } else { if (((Piece) sq.getComponent(0)).getColor() != color) { moveList.add(sq.getPositionWithArg(cell)); squareList.add(sq); } break L2; } } } L3: for (int i = 1; i < 8; i++) { int r = row - i; int c = column; int cell = square.getCellFromCoord(r, c); Square sq = (Square) board.getComponent(cell); if (r > 0) { if (sq.getComponentCount() == 0) { moveList.add(sq.getPositionWithArg(cell)); squareList.add(sq); } else { if (((Piece) sq.getComponent(0)).getColor() != color) { moveList.add(sq.getPositionWithArg(cell)); squareList.add(sq); } break L3; } } } L4: for (int i = 1; i < 8; i++) { int r = row + i; int c = column; int cell = square.getCellFromCoord(r, c); Square sq = (Square) board.getComponent(cell); if (r < 9) { if (sq.getComponentCount() == 0) { moveList.add(sq.getPositionWithArg(cell)); squareList.add(sq); } else { if (((Piece) sq.getComponent(0)).getColor() != color) { moveList.add(sq.getPositionWithArg(cell)); squareList.add(sq); } break L4; } } } }
/** * Move the all the ninjas in the game to random directions. * * @return true if all ninjas moved successfully, false if foud a spy near by and stabbed him. */ public boolean moveNinja() { // Check if the ninja has stepped on any power up last turn, assign them // back to their location. reAssignPowerUps(); for (Ninja ninja : ninjas) { int row = ninja.getRow(); int col = ninja.getCol(); Square location = null; ArrayList<Square> validLocations = getValidLocations(ninja); // Remove all the rooms and ninjas locations from possible moves. Iterator<Square> iterator = validLocations.iterator(); while (iterator.hasNext()) { Square loc = iterator.next(); if (isRoom(loc) || isNinja(loc)) { iterator.remove(); } } // The ninja can not stab the spy if the spy has invincibility. if (!spy.isInvincible()) { // If there's the spy next to this ninja, stab him! if (checkForSpy(ninja)) { return false; } } else { // Remove the spy location from ninja's possible moves when the // spy is invincible. Iterator<Square> iter = validLocations.iterator(); while (iter.hasNext()) { Square loc = iter.next(); if (loc instanceof Spy) { iter.remove(); } } } // If the ninja got place in the dead end corner and has no where to // move, it can stay in the same position. if (validLocations.size() < 3) { validLocations.add(map[row][col]); } // Choose one random direction from possible locations. int index = random.nextInt(validLocations.size()); location = validLocations.get(index); int Lrow = location.getRow(); int Lcol = location.getCol(); if (isPowerUp(location)) { // If the ninja step on the power up, save the power up and // display the ninja. powerUps.add((PowerUp) location); map[Lrow][Lcol] = ninja; ninja.setRow(Lrow); ninja.setCol(Lcol); map[row][col] = new Square(debug, row, col); } else if (location == map[row][col]) { // If the ninja stay in the same postion, do nothing. map[row][col] = ninja; } else { map[Lrow][Lcol] = ninja; ninja.setRow(Lrow); ninja.setCol(Lcol); if (!isPowerUp(map[row][col])) { map[row][col] = new Square(debug, row, col); } } } return true; }
/** * Hard mode: If the spy is on the ninja's line of sight, the ninja will chase the spy until the * line of sight is broke. If no spy on the line of sight, the ninja will move randomly. * * @return true if all ninjas moved successfully, false if foud a spy near by and stabbed him. */ public boolean moveSmartNinja() { // Check if the ninja has stepped on any power up last turn, assign them // back to their location. reAssignPowerUps(); Square roomLoc = null; int spyRow = spy.getRow(); int spyCol = spy.getCol(); for (Ninja ninja : ninjas) { ArrayList<Square> validLocations = getValidLocations(ninja); int ninjaRow = ninja.getRow(); int ninjaCol = ninja.getCol(); Square location = null; // Remove all the rooms and ninjas locations from possible moves. Iterator<Square> iterator = validLocations.iterator(); while (iterator.hasNext()) { Square loc = iterator.next(); if (isRoom(loc) || isNinja(loc)) { iterator.remove(); } } // The ninja can not stab the spy if the spy has invincibility. if (!spy.isInvincible()) { // If there's the spy next to this ninja, stab him! if (checkForSpy(ninja)) { return false; } } else { // Remove the spy location from ninja's possible moves when the // spy is invincible. Iterator<Square> iter = validLocations.iterator(); while (iter.hasNext()) { Square loc = iter.next(); if (loc instanceof Spy) { iter.remove(); } } } // If the ninja got place in the dead end corner and has no where to // move, it can stay in the same position. if (validLocations.size() < 3) { validLocations.add(map[ninjaRow][ninjaCol]); } // If the ninja is in the same row or the same column, evaluate // which (row or column) and // find if they are above or below you if (spyRow == ninjaRow || spyCol == ninjaCol) { // scenario 1: ninja and spy are in the same column, spy is // above the ninja if (spyCol == ninjaCol && spyRow < ninjaRow) { for (Square room : roomLocations) { if (ninjaRow < room.getRow() && room.getRow() < spyRow) { roomLoc = room; } } if (spyRow < roomLoc.getRow() && roomLoc.getRow() < ninjaRow) { moveNinja(); } else if (validLocations.contains(map[ninjaRow - 1][ninjaCol])) { // This is SUPPOSED to move the ninja to the // location // one spot closer to // the spy and handle the power up properly if (isPowerUp(map[ninjaRow - 1][ninjaCol])) { powerUps.add((PowerUp) map[ninjaRow - 1][ninjaCol]); map[ninjaRow - 1][ninjaCol] = ninja; ninja.setRow(ninjaRow - 1); ninja.setCol(ninjaCol); map[ninjaRow][ninjaCol] = new Square(debug, ninjaRow, ninjaCol); } else { map[ninjaRow - 1][ninjaCol] = ninja; ninja.setRow(ninjaRow - 1); ninja.setCol(ninjaCol); if (!isPowerUp(map[ninjaRow][ninjaCol])) { map[ninjaRow][ninjaCol] = new Square(debug, ninjaRow, ninjaCol); } } } } // scenario 2: ninja and spy are in the same col, spy is below // the ninja if (spyCol == ninjaCol && spyRow > ninjaRow) { for (Square room : roomLocations) { if (ninjaRow < room.getRow() && room.getRow() < spyRow) { roomLoc = room; } } if (ninjaRow < roomLoc.getRow() && roomLoc.getRow() < spyRow) { moveNinja(); } else if (validLocations.contains(map[ninjaRow + 1][ninjaCol])) { if (isPowerUp(map[ninjaRow + 1][ninjaCol])) { powerUps.add((PowerUp) map[ninjaRow + 1][ninjaCol]); map[ninjaRow + 1][ninjaCol] = ninja; ninja.setRow(ninjaRow + 1); ninja.setCol(ninjaCol); map[ninjaRow][ninjaCol] = new Square(debug, ninjaRow, ninjaCol); } else { map[ninjaRow + 1][ninjaCol] = ninja; ninja.setRow(ninjaRow + 1); ninja.setCol(ninjaCol); if (!isPowerUp(map[ninjaRow][ninjaCol])) { map[ninjaRow][ninjaCol] = new Square(debug, ninjaRow, ninjaCol); } } } } // scenario 3: ninja and spy are in the same row, spy is to the // left of the ninja if (spyRow == ninjaRow && spyCol < ninjaCol) { for (Square room : roomLocations) { if (ninjaRow < room.getRow() && room.getRow() < spyRow) { roomLoc = room; } } if (spyCol < roomLoc.getCol() && roomLoc.getCol() < ninjaCol) { moveNinja(); } else if (validLocations.contains(map[ninjaRow][ninjaCol - 1])) { if (isPowerUp(map[ninjaRow][ninjaCol - 1])) { powerUps.add((PowerUp) map[ninjaRow][ninjaCol - 1]); map[ninjaRow][ninjaCol - 1] = ninja; ninja.setRow(ninjaRow); ninja.setCol(ninjaCol - 1); map[ninjaRow][ninjaCol] = new Square(debug, ninjaRow, ninjaCol); } else { map[ninjaRow][ninjaCol - 1] = ninja; ninja.setRow(ninjaRow); ninja.setCol(ninjaCol - 1); if (!isPowerUp(map[ninjaRow][ninjaCol])) { map[ninjaRow][ninjaCol] = new Square(debug, ninjaRow, ninjaCol); } } } // scenario 4: ninja and spy are in the same row, spy is to // the right of the ninja if (spyRow == ninjaRow && spyCol > ninjaCol) { for (Square room : roomLocations) { if (ninjaRow < room.getRow() && room.getRow() < spyRow) { roomLoc = room; } } if (spyCol < roomLoc.getCol() && roomLoc.getCol() < ninjaCol) { moveNinja(); } else if (validLocations.contains(map[ninjaRow][ninjaCol + 1])) { if (isPowerUp(map[ninjaRow][ninjaCol + 1])) { powerUps.add((PowerUp) map[ninjaRow][ninjaCol + 1]); map[ninjaRow][ninjaCol + 1] = ninja; ninja.setRow(ninjaRow); ninja.setCol(ninjaCol + 1); map[ninjaRow][ninjaCol] = new Square(debug, ninjaRow, ninjaCol); } else { map[ninjaRow][ninjaCol + 1] = ninja; ninja.setRow(ninjaRow); ninja.setCol(ninjaCol + 1); if (!isPowerUp(map[ninjaRow][ninjaCol])) { map[ninjaRow][ninjaCol] = new Square(debug, ninjaRow, ninjaCol); } } } } } } else { int index = random.nextInt(validLocations.size()); location = validLocations.get(index); int Lrow = location.getRow(); int Lcol = location.getCol(); if (isPowerUp(location)) { // If the ninja step on the power up, save the power up // and // display the ninja. powerUps.add((PowerUp) location); map[Lrow][Lcol] = ninja; ninja.setRow(Lrow); ninja.setCol(Lcol); map[ninjaRow][ninjaCol] = new Square(debug, ninjaRow, ninjaCol); } else if (location == map[ninjaRow][ninjaCol]) { // If the ninja stay in the same postion, do nothing. map[ninjaRow][ninjaCol] = ninja; } else { map[Lrow][Lcol] = ninja; ninja.setRow(Lrow); ninja.setCol(Lcol); if (!isPowerUp(map[ninjaRow][ninjaCol])) { map[ninjaRow][ninjaCol] = new Square(debug, ninjaRow, ninjaCol); } } } } return true; }