Wie kann ich ein letztes Mal durchgehen, nachdem ich in meinem Mindsweeper -Spiel eine Mine gefunden habe?
Posted: 08 Feb 2025, 15:36
Ich habe verschiedene Methoden ausprobiert, um dies zu beheben, kann es aber immer noch nicht herausfinden. Derzeit habe ich es behoben, indem ich das Ganze nur noch einmal gedruckt habe, aber anscheinend ist es falsch. < /P>
Dies ist mein Code: < /p>
Dies ist mein Code: < /p>
Code: Select all
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Random r = new Random();
int mapSize = 10;
int[][] map = new int[mapSize][mapSize];
char[] ABC = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'};
boolean boom = false;
boolean validInput;
int fields = 0;
int mineCount = 0;
for (int i = 0; i < mapSize; i++) {
for (int j = 0; j < mapSize; j++) {
map[i][j] = r.nextInt(-3, 1);
if (map[i][j] == 0) {
mineCount++;
}
}
}
int totalFields = (mapSize * mapSize) - mineCount;
do {
System.out.print("\t");
for (char letter : ABC) {
System.out.printf("%2c\t", letter);
}
System.out.println();
for (int i = 0; i < mapSize; i++) {
System.out.printf("%d\t", i);
for (int j = 0; j < mapSize; j++) {
if (map[i][j] == 5) {
System.out.print("[*]\t");
} else if (map[i][j] == 2) {
System.out.print("[-]\t");
} else {
System.out.print("[ ]\t");
}
}
System.out.println();
}
// double prozent = (totalFields / 100) * mineFound;; --> alt Eingabe der Prozent berechnung
double percentChecked;
if (totalFields > 0) {
percentChecked = ((fields / (double) totalFields) * 100);
} else {
percentChecked = 100;
}
String result = String.format("%.2f", percentChecked);
System.out.println("\nDu hast " + fields + "/" + totalFields + " (" + result + "%) der sicheren Felder überprüft.");
System.out.println("Es bleiben noch " + mineCount + " Minen versteckt.");
validInput = false;
int x = -1, y = -1;
while (!validInput) {
System.out.println("Wo willst du nach Minen suchen? (z.B. A1 bis J9)");
String inputUser = sc.next().toUpperCase();
if (inputUser.length() == 2 && inputUser.charAt(0) >= 'A' && inputUser.charAt(0) = '0' && inputUser.charAt(1)