Java Advanced, 8. The Heigan Dance
Дава ми 20/100 в Джъдж. Направих си няколко теста и не съм сигурен къде ми е грешката. Логически слаби места са ми:
-ArrayOutOfBoundsException handling - на много места съм сложил много if-ове и може там да имам някъде пропуск
-какво се случва точно ако и двамата умрат по едно и също време? Това не се казва в задачата и там е доста вероятно да имам грешка
Условие:
https://softuni.bg/trainings/resources/officedocument/59902/exercise-java-advanced-may-2021/3345
Моето Решение:
package JavaAdvanced;
import java.util.Scanner;
public class TheHeiganDance {
public static void addSpell(int row, int col, boolean[][] arr) {
if (row - 1 >= 0 && col - 1 >= 0) {
arr[row - 1][col - 1] = true;
}
if (row - 1 >= 0) {
arr[row - 1][col] = true;
}
if (row - 1 >= 0 && col + 1 < 15) {
arr[row - 1][col + 1] = true;
}
if (col - 1 >= 0) {
arr[row][col - 1] = true;
}
arr[row][col] = true;
if (col + 1 < 15) {
arr[row][col + 1] = true;
}
if (row + 1 < 15 && col - 1 >= 0) {
arr[row + 1][col - 1] = true;
}
if (row + 1 < 15) {
arr[row + 1][col] = true;
}
if (row + 1 < 15 && col + 1 < 15) {
arr[row + 1][col + 1] = true;
}
}
public static void removeAllSpells(boolean[][] arr) {
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
arr[i][j] = false;
}
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
boolean[][] spellMap = new boolean[15][15];
double damageDoneToHeigan = Double.parseDouble(scan.nextLine());
double heiganHitPoints = 3000000;
int playerHitPoints = 18500;
String[] data;
String spell = "";
int spellRow;
int spellCol;
int playerRow = 7;
int playerCol = 7;
boolean inevitableDamage;
boolean poisoned = false;
while (heiganHitPoints > 0 && playerHitPoints > 0) {
heiganHitPoints = heiganHitPoints - damageDoneToHeigan;
if (heiganHitPoints <= 0) {
break;
}
if (poisoned == true) {
playerHitPoints = playerHitPoints - 3500;
poisoned = false;
}
data = scan.nextLine().split(" ");
spell = data[0];
spellRow = Integer.parseInt(data[1]);
spellCol = Integer.parseInt(data[2]);
inevitableDamage = false;
addSpell(spellRow, spellCol, spellMap);
boolean upWallCheck = false;
boolean rightWallCheck = false;
boolean downWallCheck = false;
boolean leftWallCheck = false;
if (playerRow + 1 == 15) {
upWallCheck = true;
}
if (playerCol + 1 == 15) {
rightWallCheck = true;
}
if (playerRow - 1 == -1) {
downWallCheck = true;
}
if (playerCol - 1 == -1) {
leftWallCheck = true;
}
boolean moved = false;
if (spellMap[playerRow][playerCol] == true) {
if (upWallCheck == false) {
if (spellMap[playerRow + 1][playerCol] == false) {
playerRow = playerRow + 1;
moved = true;
}
}
if (moved == false) {
if (rightWallCheck == false) {
if (spellMap[playerRow][playerCol + 1] == false) {
playerCol = playerCol + 1;
moved = true;
}
}
}
if (moved == false) {
if (downWallCheck == false) {
if (spellMap[playerRow - 1][playerCol] == false) {
playerRow = playerRow - 1;
moved = true;
}
}
}
if (moved == false) {
if (leftWallCheck == false) {
if (spellMap[playerRow][playerCol - 1] == false) {
playerCol = playerCol - 1;
moved = true;
}
}
}
if (moved == false) {
inevitableDamage = true;
}
}
if (inevitableDamage == true) {
switch (spell) {
case "Cloud":
playerHitPoints = playerHitPoints - 3500;
poisoned = true;
break;
case "Eruption":
playerHitPoints = playerHitPoints - 6000;
break;
}
}
removeAllSpells(spellMap);
}
if (heiganHitPoints <= 0 && playerHitPoints > 0) {
System.out.println("Heigan: Defeated!");
System.out.println("Player: " + playerHitPoints);
}
if (heiganHitPoints > 0 && playerHitPoints <= 0) {
System.out.printf("Heigan: %.2f%n", heiganHitPoints);
System.out.print("Player: Killed by ");
if (spell.equals("Cloud")) {
System.out.println("Plague Cloud");
} else {
System.out.println("Eruption");
}
}
if (heiganHitPoints <= 0 && playerHitPoints <= 0) {
System.out.println("Heigan: Defeated!");
System.out.print("Player: Killed by ");
if (spell.equals("Cloud")) {
System.out.println("Plague Cloud");
} else {
System.out.println("Eruption");
}
}
System.out.println("Final position: " + playerRow + ", " + playerCol);
}
}