Minesweeper

Status
Nicht offen für weitere Antworten.
G

Guest

Gast
Hallo. Ich muss wohl ein paar Fehler haben...

Code:
import java.util.Random;

public class Minesweeper {

	static final int MINE = 99;

	public static void main(String[] args) {

		boolean[][] field;

		// solange man spielen will
		do {
			// S1) Einlesen der Feldgroesse
			int w = readFieldWidth();
			int h = readFieldHeight();

			// S2) Einlesen der Bombenwahrscheinlichkeit
			double prob = readBombProb();

			// S3) Erstellen eines entsprechenden Feldes
			field = createField(w, h, prob);

			// Testausgabe
			printFieldForTesting(field);

			// S4) Spielen des Spieles
			boolean won = play(field);

			if (won) {
				System.out.println("Gratuliere ! Gewonnen");
			} else {
				System.out.println("Beng!!! Leider im Kampf gefallen!");
			}

			// S5) Fragen ob noch einmal gespielt werden soll.
		} while (playAgain());
	}

	static void printFieldForTesting(boolean[][] f) {

		System.out.println();
		for (int i = 0; i < f.length; i++) {
			for (int j = 0; j < f[i].length; j++) {
				if (f[i][j]) {
					System.out.println(" * ");
				} else {
					System.out.println(" - ");
				}
			}
			System.out.println();
		}
		System.out.println();
	}

	static int readFieldWidth() {
		System.out.print("Feldbreite: ");
		return In.readInt();
	}

	static int readFieldHeight() {
		System.out.print("Feldhoehe: ");
		return In.readInt();
	}

	static double readBombProb() {
		System.out.print("Wahrscheinlichkeit fuer Mine: ");
		return In.readDouble();
	}

	static boolean[][] createField(int w, int h, double prob) {
		boolean[][] f = new  boolean[h][w];
		for (int i = 0; i < f.length; i++) {
			for (int j = 0; j < f[i].length; j++) {
				f[i][j] = Math.random() < prob;
			}
		}
		return f;
	}

	static boolean playAgain() {
		System.out.println();
		System.out.println("Wollen Sie es nochmals versuchen (j|n)?");
		return In.readChar() == 'j';
	}

	static boolean play(boolean[][] field) {
		int[][] uncovered;
		uncovered = createUncovered(field.length, field[0].length);
		int row, col;
		boolean dead = false;
		// Ausgeben der aufgedeckten Felder
		print(uncovered);
		do {

			// Einlesen des nächsten Feldes das aufgedeckt werden soll
		System.out.println("Zeile: ");
			row = In.readInt();
			System.out.print("Spalte: ");
			col = In.readInt();

			// Aufdecken: entweder Mine erwischt oder Anzahl der nachbarminen anzeigen
			dead = uncover(row, col, field, uncovered);
			// Ausgeben der aufgedeckten Felder
			print(uncovered);

		//solange bis alle nicht-Minen aufgedeckt oder !field[row][col])
		} while (! allUncovered(uncovered, field) && ! dead);

		// return won
		return ! dead;

	}

	static boolean uncover(int r, int c, boolean[][] f, int[][] uc) {

		if (! inField(r, c, f)) {
			System.out.println("Zeile " + r + ", Spalte " + c + " nicht in Feld");
			return false;
		}

		if (f[r][c]) {
			uc[r][c] = MINE;
			return true;
		}
		uc[r][c] = countMines(f, r, c); // Nachbarnminen
		return false;
	}

	static int countMines(boolean[][] f, int r, int c) {

		int n = 0;

		for (int i = r-1; i <= r+1; i++) {
			for (int j= c-1; j <= c+1; j++) {
				if (inField(i, j, f) && f[i][j]) {
					n++;
				}
			}
		}
		return n;

	}

	static boolean inField(int r, int c, boolean[][] f) {
		return r >= 0 && c >= 0 && r < f.length && c < f[0].length;
	}

	static boolean allUncovered(int[][] uc, boolean[][] f) {
		for (int i = 0; i < f.length; i++) {
			for (int j = 0; j < f[i].length; j++) {
				// wenn keine Mine auf i, j soll es aufgedeckt sein
				if (! f[i][j] && uc[i][j] == -1) {
					return false;
				}
			}
		}
		return true;
	}

	static void print(int[][] uc) {
		System.out.println();
		for (int i = 0; i < uc.length; i++) {
			for (int j = 0; j < uc[i].length; j++) {
				if (uc[i][j] == -1) {
					System.out.print(" - ");
				} else if (uc[i][j] == MINE) {
					System.out.print(" * ");
				} else {
					System.out.print(" " + uc[i][j] + " ");
				}
			}
			System.out.println();
		}
	}

	static int[][] createUncovered(int h, int w) {
		int [][] uc = new int[h][w];
		for (int i = 0; i < uc.length; i++) {
			for (int j = 0; j < uc[i].length; j++) {
				uc[i][j] = -1;
			}
		}
		return uc;
	}

}

und die dazugehörigen Fehlermeldungen..

Compile Minesweeper.java....
Minesweeper.java:57: cannot find symbol

symbol : variable In

location: class Minesweeper

return In.readInt();

^

Minesweeper.java:62: cannot find symbol

symbol : variable In

location: class Minesweeper

return In.readInt();

^

Minesweeper.java:67: cannot find symbol

symbol : variable In

location: class Minesweeper

return In.readDouble();

^

Minesweeper.java:83: cannot find symbol

symbol : variable In

location: class Minesweeper

return In.readChar() == 'j';

^

Minesweeper.java:97: cannot find symbol

symbol : variable In

location: class Minesweeper

row = In.readInt();

^

Minesweeper.java:99: cannot find symbol

symbol : variable In

location: class Minesweeper

col = In.readInt();

^

6 errors


Compilierung beendet
 

Kim Stebel

Bekanntes Mitglied
was soll In sein? vielleicht in einer anderen Klasse deklariert? falls nicht, ersetzen durch System.in
 
Status
Nicht offen für weitere Antworten.

Ähnliche Java Themen

Neue Themen


Oben