Search code examples
javaminesweeper

Java minesweeper game I want to hide the broad when I start the game?


I'm new to this site, however, I'm having problem hiding my broad when I start the game, I just want to make it invisible from the user. But I don't know where did I missed up with my code. Please help me out with this.

import java.util.Scanner;

public class MineSweeperGame {

    int row, col;
    boolean succes = false;

    public static void main(String[] args) {
            MineSweeperGame ms = new MineSweeperGame();
            ms.run();
    }

    //
    // Recursive reveal method
    //

    public void reveal(int x, int y, char[][] mineField, boolean[][] isVisible) {
            int minx, miny, maxx, maxy;
            int result = 0;
            //
            // if cell(x, y) is not mine, make sure visible that cell.
            //
            if (mineField[x][y] != '*') {
                    isVisible[x][y] = true;
                    //
                    // if cell(x, y) is blank, check all surrounding cells
                    //
                    if (mineField[x][y] == '.') {
                            //
                            // Don't try to check beyond the edges of the board...
                            //
                            minx = (x <= 0 ? 0 : x - 1);
                            miny = (y <= 0 ? 0 : y - 1);
                            maxx = (x >= row - 1 ? row - 1 : x + 1);
                            maxy = (y >= col - 1 ? col - 1 : y + 1);
                            //
                            // Loop over all surrounding cells, call recursive reveal(i, j) method
                            //
                            for (int i = minx; i <= maxx; i++) {
                                    for (int j = miny; j <= maxy; j++) {
                                            if (isVisible[i][j] == false) {
                                                    reveal(i, j, mineField, isVisible);
                                            }
                                    }
                            }
                    }

            } //
            // if cell(x, y) is mine, do nothing.
            //
            else {

            }
    }

    void printMineMap(char[][] mineField, boolean[][] isVisible) {
            System.out.println();
            succes = true;
            //
            // Loop over all cells, print cells
            //
            for (int x = 0; x < row; x++) {
                    for (int y = 0; y < col; y++) {
                            //
                            // Loop over all cells, print cells
                            //
                            if (isVisible[x][y]) {
                                    System.out.print(" " + mineField[x][y] + "  ");
                            } else {
                                    System.out.print("[" + mineField[x][y] + "] ");
                                    if (mineField[x][y] != '*') {
                                            succes = false;
                                    }
                            }
                    }
                    System.out.println();
            }

            if (succes) {
                    System.out.println("********** Congratulations~!!! You win. **********");
            }
    }

    private void run() {
            //
            // Initialize MineField
            //
            char[][] mineField = MineField.getMineField();
            row = mineField.length;
            col = mineField[0].length;
            boolean[][] isVisible = new boolean[row][col];
            // print mine map
            printMineMap(mineField, isVisible);

            while (true) {
                    System.out.print("Enter your guess (x y): ");
                    Scanner in = new Scanner(System.in);
                    //
                    // input x, y
                    //
                    int x = in.nextInt() - 1;
                    if (x < 0) {// if negative value, exit program.
                            System.out.println("Canceled by user.");
                            break;
                    }
                    int y = in.nextInt() - 1;
                    if (x >= row || y >= col) // ignore invalid values 
                    {
                            continue;
                    }
                    //
                    // Check cell(x,y) is mine, if yes, quit program
                    //
                    if (mineField[x][y] == '*') {
                            isVisible[x][y] = true;
                            printMineMap(mineField, isVisible);
                            System.out.println("Game Over ~!!!");
                            break;
                    }
                    //
                    // call recursive reveal method to reveal cell(x, y)
                    //
                    reveal(x, y, mineField, isVisible);
                    printMineMap(mineField, isVisible);
            }
    }
}

where and example of the game when it start ( I tried to post it as the game dose but every time I try it missed up the order).

[.] [.] [1] [1] [2] [*] [1] [.] 

[.] [.] [1] [*] [2] [1] [1] [.]

[.] [.] [1] [1] [1] [1] [2] [2] 

[.] [1] [2] [2] [1] [1] [*] [*] 

[.] [1] [*] [*] [1] [1] [2] [2] 

[1] [2] [3] [4] [3] [1] [.] [.] 

[*] [1] [1] [*] [*] [1] [.] [.] 

[1] [1] [1] [2] [2] [1] [.] [.]

Enter your guess (x y):

What I want is something like this

[] [] [] [] [] [] [] []

[] [] [] [] [] [] [] []

[] [] [] [] [] [] [] []

[] [] [] [] [] [] [] []

[] [] [] [] [] [] [] []

[] [] [] [] [] [] [] []

[] [] [] [] [] [] [] []

[] [] [] [] [] [] [] []

Solution

  • I feel like this is a weird way of doing this, but I'm assuming you're new to java and this is some sort of assignment or personal project to help yourself learn.

    From what I can tell, It looks like your if statement is doing the same thing for both if and else. Consider changing the else statement to print "[ ]"

                                if (isVisible[x][y]) {
                                    System.out.print(" " + mineField[x][y] + "  ");
                            } else {
                                    System.out.print("[" + mineField[x][y] + "] ");
                                    if (mineField[x][y] != '*') {
                                            succes = false;
                                    }
                            }
    

    In other words, the above should be:

                                if (isVisible[x][y]) {
                                    System.out.print(" " + mineField[x][y] + "  ");
                            } else {
                                    System.out.print("[ ] ");
                                    if (mineField[x][y] != '*') {
                                            succes = false;
                                    }
                            }
    

    Maybe give that a shot?