2017-05-08 5 views
-1

私はコンピュータがユーザ(人間)に対して演奏し、その一部を実装するのに役立つスドクゲームを作った。私はすべてを試しました! 私は、ユーザーがStdInを使用して人間とコンピュータの両方の移動を選択するモード1を実行しましたが、今ではコンピュータがランダム移動を行うモード0を実行する必要があります。ここで SodokuゲームとMath.random()を使用してグリッドに数字を入力する方法

は、これまでの私のコードです:あなたはモード0のために見ることができるように

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

、Iターン、人間とコンピュータの間で交互に整数を作ることによって、そのコンピュータの電源を入れブールcomputerTurn場合開始しました真実であり、逆もまた真である。私が試したものを除いて、ランダムな整数は描画したグリッドには入力されません。

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

は、だから、それをグリッドが2×2であれば、入力乱数(すなわち、それは1-4から、その3x3の場合は乱数だろう「必要がある」コードの一部です「1-9から乱数ができるでしょう。しかし、私は、コードを実行するたびに、それは私のグリッドの乱数を置いていません。

computerTurnのコードで
+1

私が読むには多すぎるコードだが、唯一の違いは、ユーザー入力とコンピュータ入力が値のソース(Scanner vs Random)である必要があります。残りのコードは同じコードを使用する必要があります(正確には同じメソッド)。これで問題が解決しない場合は、インデントされた[mcve] – AxelH

+3

フルストップを投稿してください。いまここで。 A)その理由は、編集ウィンドウ上のプレビュー機能が存在するからです。マークアップに関するすべてのルールに同じです。あなたは、+ **インデントされていない100行以上のコードをダンプしません。そのため、そのソースコードを読むことは不可能に近づいています。そして少なくとも私は:すぐにあなたを手伝ってくれることを止めさせる。 B)あなたは自分を殺している。他に何かをする前に、コードを単純化するのに役立つ* abstractions *を特定してください。そのような無限のネストされたifとループ - それは "コードを維持するのが難しい"ということです。申し訳ありませんが、あなたの問題は "機能"ではありません**フォーム** ... – GhostCat

+0

私は乱数を使用してどのように動作することはできません、最初の呼び出しで1を与え、 –

答えて

2

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

この行う結果は常に0となります。

Math.random()0.0 <= Math.random() < 1.0から、intにはdoubleでタイプキャストすると(int) Math.random()になると、0となるdoubleを返します。

あなたが何をしても、それはまだ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

関連する問題