2017-05-08 34 views
-1

將數字輸入到我的網格我已經做了一個數獨遊戲,其中計算機與用戶(人類)對戰並需要幫助實現其中的一部分。我已經嘗試了一切! 因此,我已經完成了模式1,用戶使用StdIn選擇人和計算機的移動,但現在我必須在模式0中進行隨機移動。Sodoku遊戲和如何使用Math.random()

這是到目前爲止我的代碼:

public class Twoduko { 

    static int Board[][]; 

    public static void main(String[] args) { 
     StdOut.print("Enter the height of the grid:"); 
     int H = StdIn.readInt(); 
     StdOut.print("Enter the width of the grid:"); 
     int W = StdIn.readInt(); 
     StdOut.print("Enter the mode:"); 
     int M = StdIn.readInt(); 

     generateBoard(H, W); 
     drawBoard(H, W); 
     playGameModeZero(H, W, M); 
     playGameModeOne(H, W, M); 
     playGameModeTwo(H, W, M); 
    } 

    public static void generateBoard(int H, int W) { 

     int tempBoard[][] = new int[H * H][W * W]; 

     for (int i = 0; i < H * H; i++) { 
      for (int j = 0; j < W * W; j++) { 

       tempBoard[i][j] = 0; 
      } 
     } 
     Board = tempBoard; 
    } 

    public static void drawBoard(int H, int W) { 
     if (W == 2) { 
      System.out.println("+---+---++---+---+"); 
     } else if (W == 3) { 
      System.out.println("+---+---+---++---+---+---++---+---+---+"); 
     } else if (W == 4) { 
      System.out.println("+---+---+---+---++---+---+---+---++---+---+---+---++---+---+---+---+"); 
     } else if (W == 5) { 
      System.out.println(
        "+---+---+---+---+---++---+---+---+---+---++---+---+---+---+---++---+---+---+---+---++---+---+---+---+---+"); 
     } 

     for (int i = 0; i < H * H; i++) { 

      for (int j = 0; j < W * W; j++) { 

       if (W == 2 && j == 2) { 
        System.out.print("||"); 
       } else if (W == 3 && j == 3) { 
        System.out.print("||"); 
       } else if (W == 3 && j == 6) { 
        System.out.print("||"); 
       } else if (W == 4 && j == 4) { 
        System.out.print("||"); 
       } else if (W == 4 && j == 8) { 
        System.out.print("||"); 
       } else if (W == 4 && j == 12) { 
        System.out.print("||"); 
       } else if (W == 5 && j == 5) { 
        System.out.print("||"); 
       } else if (W == 5 && j == 10) { 
        System.out.print("||"); 
       } else if (W == 5 && j == 15) { 
        System.out.print("||"); 
       } else if (W == 5 && j == 20) { 
        System.out.print("||"); 
       } else { 
        System.out.print("|"); 
       } 
       System.out.print(Board[i][j] + " "); 
      } 
      System.out.print("" + "|"); 
      System.out.println(""); 

      if (W == 2 && H == 2) { 
       if (i == 1) { 
        System.out.println("+===+===++===+===+"); 
       } else { 
        System.out.println("+---+---++---+---+"); 
       } 
      } 
      if (W == 3 && H == 3) { 
       if (i == 2) { 
        System.out.println("+===+===+===++===+===+===++=== +===+===+"); 
       } else if (i == 5) { 
        System.out.println("+===+===+===++===+===+===++=== +===+===+"); 
       } else { 
        System.out.println("+---+---+---++---+---+---++---+---+---+"); 
       } 
      } 
      if (W == 3 && H == 2) { 
       if (i == 1) { 
        System.out.println("+===+===+===++===+===+===++=== +===+===+"); 
       } else { 
        System.out.println("+---+---+---++---+---+---++---+---+---+"); 
       } 
      } 
      if (W == 4 && H == 4) { 
       if (i == 3) { 
        System.out.println("+===+===+===+===++===+===+===+ ===++===+===+===+===++===+===+===+===+"); 
       } else if (i == 7) { 
        System.out.println("+===+===+===+===++===+===+===+ ===++===+===+===+===++===+===+===+===+"); 
       } else if (i == 11) { 
        System.out.println("+===+===+===+===++===+===+===+ ===++===+===+===+===++===+===+===+===+"); 
       } else { 
        System.out.println("+---+---+---+---++---+---+---+---++---+---+---+---++---+---+---+---+"); 
       } 
      } 
      if (W == 4 && H == 2) { 
       if (i == 1) { 
        System.out.println("+===+===+===+===++===+===+===+ ===++===+===+===+===++===+===+===+===+"); 
       } else { 
        System.out.println("+---+---+---+---++---+---+---+---++---+---+---+---++---+---+---+---+"); 
       } 
      } 
      if (W == 4 && H == 3) { 
       if (i == 2) { 
        System.out.println("+===+===+===+===++===+===+===+ ===++===+===+===+===++===+===+===+===+"); 
       } else if (i == 5) { 
        System.out.println("+===+===+===+===++===+===+===+ ===++===+===+===+===++===+===+===+===+"); 
       } else { 
        System.out.println("+---+---+---+---++---+---+---+---++---+---+---+---++---+---+---+---+"); 
       } 
      } 
      if (W == 5 && H == 2) { 
       if (i == 1) { 
        System.out.println(
          "+===+===+===+===+===++===+===+===+===+===++===+== =+===+===+===++===+===+===+===+===++===+===+===+== =+===+"); 
       } else { 
        System.out.println(
          "+---+---+---+---+---++---+---+---+---+---++---+---+---+---+---++---+---+---+---+---++---+---+---+---+---+"); 
       } 
      } 
     } 
    } 

    public static void playGameModeZero(int H, int W, int M) { 
     if (M == 0) { 
      boolean play = true; 
      boolean humanTurn = false; 
      boolean computerTurn = false; 
      int turn = 0; 
      if (turn % 2 == 0) { 
       System.out.println("Computer's turn"); 
      } 
      if (turn % 2 == 0) { 
       computerTurn = true && humanTurn == false; 
      } 
      if (turn % 2 != 0) { 
       System.out.println("Human's turn"); 
      } 
      if (turn % 2 != 0) { 
       humanTurn = true && computerTurn == false; 
      } 
      turn++; 
      while (play) { 

       if (humanTurn) { 
        int row = 0; 
        int column = 0; 
        int entry = 0; 
        StdOut.print("Enter row:"); 
        row = StdIn.readInt(); 
        StdOut.print("Enter column:"); 
        column = StdIn.readInt(); 
        StdOut.print("Enter your entry value:"); 
        entry = StdIn.readInt(); 

        if (entry <= 0 || entry >= (W * W + 1)) { 
         play = false; 
         System.out.println("Illegal move."); 
        } 

        if (row > W * W || column > H * H) { 
         play = false; 
         System.out.println("Illegal move. "); 
        } 

        for (int i = 0; i < H * H - 1; i++) { 
         if (Board[i][column] == entry) { // check row 
          play = false; 
          System.out.println("Illegal move."); 
         } 
        } 

        for (int j = 0; j < W * W; j++) { // check column 
         if (Board[row][j] == entry) { 
          play = false; 
          System.out.println("Illegal move."); 
         } 
        } 
        int subRow = 0; 
        int subCol = 0; 
        subRow = row/H; 
        subCol = column/W; 

        for (int i = 0; i < H; i++) { // check subgrid 
         for (int j = 0; j < W; j++) { 
          if (Board[i + H * subRow][j + W * subCol] == entry) { 
           play = false; 
           System.out.println("Illegal move."); 
          } 
         } 
        } 
       } 
       if (computerTurn) { 
        Board[(int) Math.random() * (W * W)][(int) Math.random() * (H * H)] = (int) (Math.random() * H * W) 
          + 1; 
       } 
      } 
     } 

    } 

    public static void playGameModeOne(int W, int H, int M) { 
     if (M == 1) { 
      boolean play = true; 
      boolean humanTurn = false; 
      boolean computerTurn = false; 
      int turn = 0; 
      if (turn % 2 == 0) { 
       System.out.println("Computer's turn"); 
      } 
      if (turn % 2 == 0) { 
       computerTurn = true && humanTurn == false; 
      } 
      if (turn % 2 != 0) { 
       System.out.println("Human's turn"); 
      } 
      if (turn % 2 != 0) { 
       humanTurn = true && computerTurn == false; 
      } 
      turn++; 

      while (play) { 
       int row = 0; 
       int column = 0; 
       int entry = 0; 
       StdOut.print("Enter row:"); 
       row = StdIn.readInt(); 
       StdOut.print("Enter column:"); 
       column = StdIn.readInt(); 
       StdOut.print("Enter your entry value:"); 
       entry = StdIn.readInt(); 

       if (entry <= 0 || entry >= (W * W + 1)) { 
        play = false; 
        System.out.println("Illegal move."); 
       } 

       if (row > W * W || column > H * H) { 
        play = false; 
        System.out.println("Illegal move. "); 
       } 

       for (int i = 0; i < H * H - 1; i++) { 
        if (Board[i][column] == entry) { // check row 
         play = false; 
         System.out.println("Illegal move."); 
        } 
       } 

       for (int j = 0; j < W * W; j++) { // check column 
        if (Board[row][j] == entry) { 
         play = false; 
         System.out.println("Illegal move."); 
        } 
       } 
       int subRow = 0; 
       int subCol = 0; 
       subRow = row/H; 
       subCol = column/W; 

       for (int i = 0; i < H; i++) { // check subgrid 
        for (int j = 0; j < W; j++) { 
         if (Board[i + H * subRow][j + W * subCol] == entry) { 
          play = false; 
          System.out.println("Illegal move."); 
         } 
        } 
       } 
       if (play == true && turn % 2 == 0) { 
        System.out.println("Computer's turn"); 
       } 
       if (play == true && turn % 2 != 0) { 
        System.out.println("Human's turn"); 
       } 
       turn++; 

       Board[row][column] = entry; 
       if (play) { 
        drawBoard(W, H); 

       } 
      } 
      if (computerTurn && play == false) { 

       { 
        System.out.println("Human wins!"); 
       } 
       if (humanTurn && play == false) { 

        System.out.println("Computer wins!"); 
       } 
      } 
     }  
} 

因此,大家可以看到模式0,我開始做一個整數人類和電腦之間交替打開,如果它的計算機打開布爾computerTurn是真的,反之亦然。除了我試過的任何東西之外,隨機整數不會輸入到我繪製的網格中。

if (computerTurn) { 
    Board[(int) Math.random() * (W * W)][(int) Math.random() * (H * H)] = (int) (Math.random() * H * W) 
    + 1; 
    } 

所以這是一種「應該」輸入的隨機數(即,如果電網是2×2,那麼這將是一個隨機數從1-4,並且如果它的3×3的代碼的一部分,它「會是1-9的隨機數。但每當我運行的代碼,它沒有放置在我的網格中的隨機數。

+1

那很多代碼(太多我讀的),但唯一的區別een用戶輸入和計算機輸入應該是值的來源(掃描器vs隨機),其餘部分應該使用完全相同的代碼(準確的說法相同)。如果這不能解決您的問題,請發送縮進[mcve] – AxelH

+3

句號。此時此刻。 A)編輯窗口中的預覽功能存在是有原因的。所有關於標記的規則都一樣。您不會轉儲100行以上的代碼,這些代碼**不會縮進。這使得讀取源代碼幾乎不可能;至少我:停止想要立即幫助你。 B)你在殺死自己。在做其他事情之前,你應該退後一步,找出可以幫助你簡化代碼的*抽象*。如此無盡的嵌套ifs和循環 - 即「不可維護」的代碼。對不起,但你的問題不是「功能」,但**表格** ... – GhostCat

+0

我不明白如何使用隨機數可以工作,如果第一個電話給你1和下一個電話1再次? – 2017-05-08 12:09:27

回答

2

computerTurn代碼,

(int) Math.random() * (W * W) 

這條線將總是給出結果爲0

Math.random()返回一個從0.0 <= Math.random() < 1.0和當您在intdouble(int) Math.random()進行類型轉換的雙倍,它變成0

然後無論你用它做什麼,它仍然是0

所以,如果你想生成0隨機到​​存在,取而代之的是,

(int) (Math.random() * W * W) 
1

這些線是錯誤的:

if (turn % 2 == 0) { 
    computerTurn = true && humanTurn == false; 
} 

if (turn % 2 != 0) { 
    humanTurn = true && computerTurn == false; 
} 

這裏是你的意思寫:

if (turn % 2 == 0) { 
    computerTurn = true; 
    humanTurn = false; 
} 

if (turn %2 != 0) { 
    computerTurn = false; 
    humanTurn = true; 
} 
+0

我希望這些變量賦值可以在沒有'if()'語句的情況下進行設置 - 實質上是'computerTurn =(turn%2 == 0)',然後是'humanTurn =!computerTurn'。 – halfer