/** * 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); }
/** * 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; }