2015-10-02 40 views
0

我是這個網站的新手,但是我在開始遊戲時隱藏了廣泛的問題,我只是想讓它從用戶中看不見。但我不知道我在哪裏錯過了我的代碼。這個你能幫我嗎。Java掃雷遊戲我想在開始遊戲時隱藏廣闊的空間嗎?

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); 
      } 
    } 
} 

地點和例如,當它開始(我試着將它張貼隨着比賽的劑量,但每次我嘗試它錯過了順序)的遊戲。

[.] [.] [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] [.] [.] 

輸入你的猜測(XY):

我想是這樣的

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

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

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

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

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

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

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

[] [] [] [] [] [] [] [] 
+0

當你第一次調用'printMineMap()',這裏'isVisible [x] [y]'爲每個x和y,值爲false。所以你需要改變這裏的邏輯。 – sifho

回答

1

我喜歡這種感覺是這樣的一個奇怪的方式,但我假設你'對Java新來說,這是一些任務或個人項目來幫助你學習。

從我可以告訴,它看起來像你的if語句爲if和else做同樣的事情。考慮更改else語句打印 「[]」

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

換句話說,上面應該是:

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

也許給一個鏡頭?

+0

男人非常感謝你,是的,最後它的工作 – Mohd