/** * Stab the spy, move back to original postion, minus on live. Clear the spy off the current * position on the map. */ public void stabSpy() { int oldRow = spy.getRow(); int oldCol = spy.getCol(); spy.getStabbed(); // Game end if the spy reach 0 live. int spyLives = spy.getLives(); if (spyLives == 0) { gameEndStatus = 2; } // Bring the spy back to the original postion. if (!(map[8][0] instanceof Spy)) { map[oldRow][oldCol] = new Square(debug, oldRow, oldCol); } map[8][0] = spy; spy.setRow(8); spy.setCol(0); // Delete old ninja locations from the occupied array. Iterator<Square> iterator = occupiedLocations.iterator(); while (iterator.hasNext()) { Square loc = iterator.next(); if (isNinja(loc)) { iterator.remove(); } } assignNinjas(false); }
/** * Perform the shooting action, kill the first ninja that stands in the bullet direction. * * @param direction 1-up, 2-down, 3-left, 4-right. * @return the status code of the action: 1 - killed a ninja, 2 - missed. */ public int shootNinja(int direction) { int row = spy.getRow(); int col = spy.getCol(); switch (direction) { case 1: // shoot up if (row == 0) { spy.useBullet(); return 2; } else for (int i = 1; row - i >= 0; i++) { // isNinja replace with Square object if (isNinja(map[row - i][col])) { Ninja deleteNinja = (Ninja) map[row - i][col]; map[row - i][col] = new Square(debug, row - i, col); reAssignPowerUps(); Iterator<Ninja> iterator = ninjas.iterator(); while (iterator.hasNext()) { Square loc = iterator.next(); if (loc.equals(deleteNinja)) { iterator.remove(); } } spy.useBullet(); return 1; } } spy.useBullet(); return 2; case 2: // shoot down if (row == 8) { spy.useBullet(); return 2; } else for (int i = 1; row + i <= 8; i++) { // isNinja replace with Square object if (isNinja(map[row + i][col])) { Ninja deleteNinja = (Ninja) map[row + i][col]; map[row + i][col] = new Square(debug, row + i, col); reAssignPowerUps(); Iterator<Ninja> iterator = ninjas.iterator(); while (iterator.hasNext()) { Square loc = iterator.next(); if (loc.equals(deleteNinja)) { iterator.remove(); } } spy.useBullet(); return 1; } } spy.useBullet(); return 2; case 3: // shoot left if (col == 0) { spy.useBullet(); return 2; } else for (int i = 1; col - i >= 0; i++) { // isNinja replace with Square object if (isNinja(map[row][col - i])) { Ninja deleteNinja = (Ninja) map[row][col - i]; map[row][col - i] = new Square(debug, row, col - i); reAssignPowerUps(); Iterator<Ninja> iterator = ninjas.iterator(); while (iterator.hasNext()) { Square loc = iterator.next(); if (loc.equals(deleteNinja)) { iterator.remove(); } } spy.useBullet(); return 1; } } spy.useBullet(); return 2; case 4: // shoot right if (col == 8) { spy.useBullet(); return 2; } else for (int i = 1; col + i <= 8; i++) { // isNinja replace with Square object if (isNinja(map[row][col + i])) { Ninja deleteNinja = (Ninja) map[row][col + i]; map[row][col + i] = new Square(debug, row, col + i); reAssignPowerUps(); Iterator<Ninja> iterator = ninjas.iterator(); while (iterator.hasNext()) { Square loc = iterator.next(); if (loc.equals(deleteNinja)) { iterator.remove(); } } spy.useBullet(); return 1; } } spy.useBullet(); return 2; } return direction; }
/** * Move the spy to the directed direction. The spy can look it room, look for the gre4ka, get stab * if walk into ninja, and activate the power up if found. * * @param direction an integer from 1-4: 1-up, 2-left, 3-down, 4-right. * @return the status code: 1 - the player moved sucessfully, 2 - move failed, 3 - room empty, 4 - * the player got stabbed, 5 - the player activated Bullet power up, 6 - the player activated * Radar power up, 7 - the player activated Invincibility power up, 8- the spy is invicible, * he can not get stabbed or walk into another ninja. */ public int movePlayer(int direction) { int row = spy.getRow(); int col = spy.getCol(); // Check for spy invicibility status, decrement the turn number by 1 // each turn. if (spy.isInvincible()) { invincibilityTurns -= 1; if (invincibilityTurns <= 0) { spy.setInvincibility(false); } } switch (direction) { case 1: // Move up if (row - 1 >= 0) { if (!isRoom(map[row - 1][col])) { if (isNinja(map[row - 1][col])) { if (spy.isInvincible()) { // The spy can not get stabbed or walk into another // ninja. return 8; } else { stabSpy(); return 4; } } else if (map[row - 1][col] instanceof Bullet) { if (spy.getBullets() < 1) { spy.addBullet(); spy.setRow(row - 1); map[row - 1][col] = spy; map[row][col] = new Square(debug, row, col); return 5; } else if (spy.getBullets() == 1) { return 2; } } else if (map[row - 1][col] instanceof Radar) { briefCaseRoom.setSymbol("*"); spy.setRow(row - 1); map[row - 1][col] = spy; map[row][col] = new Square(debug, row, col); return 6; } else if (map[row - 1][col] instanceof Invincibility) { spy.setInvincibility(true); spy.setRow(row - 1); map[row - 1][col] = spy; map[row][col] = new Square(debug, row, col); return 7; } else { spy.setRow(row - 1); map[row - 1][col] = spy; map[row][col] = new Square(debug, row, col); } } else { return 2; } } else { return 2; } break; case 2: // Move left if (col - 1 >= 0) { if (!isRoom(map[row][col - 1])) { if (isNinja(map[row][col - 1])) { if (spy.isInvincible()) { // The spy can not get stabbed or walk into another // ninja. return 8; } else { stabSpy(); return 4; } } else if (map[row][col - 1] instanceof Bullet) { if (spy.getBullets() < 1) { spy.addBullet(); spy.setCol(col - 1); map[row][col - 1] = spy; map[row][col] = new Square(debug, row, col); return 5; } else if (spy.getBullets() == 1) { return 2; } } else if (map[row][col - 1] instanceof Radar) { briefCaseRoom.setSymbol("*"); spy.setCol(col - 1); map[row][col - 1] = spy; map[row][col] = new Square(debug, row, col); return 6; } else if (map[row][col - 1] instanceof Invincibility) { spy.setInvincibility(true); spy.setCol(col - 1); map[row][col - 1] = spy; map[row][col] = new Square(debug, row, col); return 7; } else { spy.setCol(col - 1); map[row][col - 1] = spy; map[row][col] = new Square(debug, row, col); } } else { return 2; } } else { return 2; } break; case 3: // Move down if (row + 1 <= 8) { if (!isRoom(map[row + 1][col])) { if (isNinja(map[row + 1][col])) { if (spy.isInvincible()) { // The spy can not get stabbed or walk into another // ninja. return 8; } else { stabSpy(); return 4; } } else if (map[row + 1][col] instanceof Bullet) { if (spy.getBullets() < 1) { spy.addBullet(); spy.setRow(row + 1); map[row + 1][col] = spy; map[row][col] = new Square(debug, row, col); return 5; } else if (spy.getBullets() == 1) { return 2; } } else if (map[row + 1][col] instanceof Radar) { briefCaseRoom.setSymbol("*"); spy.setRow(row + 1); map[row + 1][col] = spy; map[row][col] = new Square(debug, row, col); return 6; } else if (map[row + 1][col] instanceof Invincibility) { spy.setInvincibility(true); spy.setRow(row + 1); map[row + 1][col] = spy; map[row][col] = new Square(debug, row, col); return 7; } else { spy.setRow(row + 1); map[row + 1][col] = spy; map[row][col] = new Square(debug, row, col); } } else if (isRoom(map[row + 1][col])) { // The spy can only enter the room from this north side by // moving down. // Enter room, check for brief case. if (((Room) map[row + 1][col]).hasBriefCase()) { gameEndStatus = 1; } else { // Return code 3 if room is empty. return 3; } } else { return 2; } } else { return 2; } break; case 4: // Move right if (col + 1 <= 8) { if (!isRoom(map[row][col + 1])) { if (isNinja(map[row][col + 1])) { if (spy.isInvincible()) { // The spy can not get stabbed or walk into another // ninja. return 8; } else { stabSpy(); return 4; } } else if (map[row][col + 1] instanceof Bullet) { if (spy.getBullets() < 1) { spy.addBullet(); spy.setCol(col + 1); map[row][col + 1] = spy; map[row][col] = new Square(debug, row, col); return 5; } else if (spy.getBullets() == 1) { return 2; } } else if (map[row][col + 1] instanceof Radar) { briefCaseRoom.setSymbol("*"); spy.setCol(col + 1); map[row][col + 1] = spy; map[row][col] = new Square(debug, row, col); return 6; } else if (map[row][col + 1] instanceof Invincibility) { spy.setInvincibility(true); spy.setCol(col + 1); map[row][col + 1] = spy; map[row][col] = new Square(debug, row, col); return 7; } else { spy.setCol(col + 1); map[row][col + 1] = spy; map[row][col] = new Square(debug, row, col); } } else { return 2; } } else { return 2; } break; } return 1; }
/** * This method allows the spy to see ahead 2 squares. Remove the darkness mark "X", and switch the * visibility of ninjas, powerups to true if they are nearby. */ public void assignSpyVisibility() { int row = spy.getRow(); int col = spy.getCol(); // Traverse through the spyVisibilityLocations array to switch // visibility location from previous turn to false. if (!spyVisibilityLocations.isEmpty()) { for (Square a : spyVisibilityLocations) { if (a.getSymbol().equals(" ")) { a.setSymbol("X"); a.setVisible(false); } if (isNinja(a) || isPowerUp(a)) { a.setVisible(false); } } spyVisibilityLocations.clear(); } for (int i = 0; i < map.length; ++i) { for (int j = 0; j < map[i].length; ++j) { // looks at the 8 squares right next to the spy if (abs(spy.getRow() - i) < 2 && abs(spy.getCol() - j) < 2) { // Check for ninjas and powerups near by to switch their // visibility to true. if (isNinja(map[i][j])) { map[i][j].setVisible(true); spyVisibilityLocations.add(map[i][j]); } if (isPowerUp(map[i][j])) { map[i][j].setVisible(true); spyVisibilityLocations.add(map[i][j]); } if (map[i][j].getSymbol().equals("X")) { map[i][j].setSymbol(" "); map[i][j].setVisible(true); spyVisibilityLocations.add(map[i][j]); } else { map[i][j].getSymbol(); } } // looks two spaces left and right if (j == spy.getCol() && abs(spy.getRow() - i) < 3) { if (isNinja(map[i][j])) { map[i][j].setVisible(true); spyVisibilityLocations.add(map[i][j]); } if (isPowerUp(map[i][j])) { map[i][j].setVisible(true); spyVisibilityLocations.add(map[i][j]); } if (map[i][j].getSymbol().equals("X")) { map[i][j].setSymbol(" "); map[i][j].setVisible(true); spyVisibilityLocations.add(map[i][j]); } else { map[i][j].getSymbol(); } } // looks two spaces up and down if (i == spy.getRow() && abs(spy.getCol() - j) < 3) { if (isNinja(map[i][j])) { map[i][j].setVisible(true); spyVisibilityLocations.add(map[i][j]); } if (isPowerUp(map[i][j])) { map[i][j].setVisible(true); spyVisibilityLocations.add(map[i][j]); } if (map[i][j].getSymbol().equals("X")) { map[i][j].setSymbol(" "); map[i][j].setVisible(true); spyVisibilityLocations.add(map[i][j]); } else { map[i][j].getSymbol(); } } } } // If the spy stands next to a room, switch the visibility of the square // behind the room to false. if (col == 0) { Square behindRoom = null; if (isRoom(map[row][col + 1])) { behindRoom = map[row][col + 2]; if (behindRoom.getSymbol().equals(" ")) { behindRoom.setSymbol("X"); behindRoom.setVisible(false); } if (isNinja(behindRoom) || isPowerUp(behindRoom)) { behindRoom.setVisible(false); } } } else if (col == 8) { Square behindRoom = null; if (isRoom(map[row][col - 1])) { behindRoom = map[row][col - 2]; if (behindRoom.getSymbol().equals(" ")) { behindRoom.setSymbol("X"); behindRoom.setVisible(false); } if (isNinja(behindRoom) || isPowerUp(behindRoom)) { behindRoom.setVisible(false); } } } else { Square behindRoom = null; if (isRoom(map[row][col + 1])) { behindRoom = map[row][col + 2]; if (behindRoom.getSymbol().equals(" ")) { behindRoom.setSymbol("X"); behindRoom.setVisible(false); } if (isNinja(behindRoom) || isPowerUp(behindRoom)) { behindRoom.setVisible(false); } } if (isRoom(map[row][col - 1])) { behindRoom = map[row][col - 2]; if (behindRoom.getSymbol().equals(" ")) { behindRoom.setSymbol("X"); behindRoom.setVisible(false); } if (isNinja(behindRoom) || isPowerUp(behindRoom)) { behindRoom.setVisible(false); } } } if (row == 0) { Square behindRoom = null; if (isRoom(map[row + 1][col])) { behindRoom = map[row + 2][col]; if (behindRoom.getSymbol().equals(" ")) { behindRoom.setSymbol("X"); behindRoom.setVisible(false); } if (isNinja(behindRoom) || isPowerUp(behindRoom)) { behindRoom.setVisible(false); } } } else if (row == 8) { Square behindRoom = null; if (isRoom(map[row - 1][col])) { behindRoom = map[row - 2][col]; if (behindRoom.getSymbol().equals(" ")) { behindRoom.setSymbol("X"); behindRoom.setVisible(false); } if (isNinja(behindRoom) || isPowerUp(behindRoom)) { behindRoom.setVisible(false); } } } else { Square behindRoom = null; if (isRoom(map[row + 1][col])) { behindRoom = map[row + 2][col]; if (behindRoom.getSymbol().equals(" ")) { behindRoom.setSymbol("X"); behindRoom.setVisible(false); } if (isNinja(behindRoom) || isPowerUp(behindRoom)) { behindRoom.setVisible(false); } } if (isRoom(map[row - 1][col])) { behindRoom = map[row - 2][col]; if (behindRoom.getSymbol().equals(" ")) { behindRoom.setSymbol("X"); behindRoom.setVisible(false); } if (isNinja(behindRoom) || isPowerUp(behindRoom)) { behindRoom.setVisible(false); } } } }
/** * 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; }