Skip to content
Advertisement

Which is the mistake in the code for game Minesweeper

JavaScript

Good morning. In short what the code does. When game begin, the user is asked System.out.print("How many mines do you want on the field?: ");. User input a number and random are generate mines on the 2d array table. Parallel points (x, y) from 2d array where is mines are add to List<Point> points = new ArrayList<>(); points.add(new Point(x, y)); All this do public void randomX() method. After print the table public void printMinesweeper(). This method print a 2d array table with hidden mines else if (minesweeper[i][j] == 'X') {System.out.print('.');} and visible digit, how many mines are around, calculated and prints by the method else { System.out.print(getCharAt(i, j));}. After program got to the method public void game() in while loop and ask user System.out.print("Set/delete mines marks (y and x coordinates): ");.User input 2 coordinates. Above I had declared static List<Point> guess = new ArrayList<>(); and static Map<Point, Character> fields = init() // is a map with 81 cells with keys Point and value '.'; The coordinates input by user are verificated in if-else statement if (fields.get(new Point(x, y)) == '.' && (getCharAt(x, y) == "0" || getCharAt(x, y) == "X")) programm put in the fields map the character ‘*’ at this coordinates. In parallel this coordinates are added in list guess. And programm do this while game is not finished. In my code if game is finished if ckecked by the method private static boolean checkIfFinished() {return points.equals(guess);} if this is true this answer go to boolean variables finished and while see that finished is true and show System.out.println("Congratulations! You found all mines!");

My problem is: when I introduce a number of mines bigger then 3 even if I set all mines and I know that points.equals(guess) the game return false and game continue. How to resolve this.

Advertisement

Answer

You need to fix checkIfFinished(). In your current case, you check also if the order of your points is the same, otherwise your game won’t finish.

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement