2017-04-15 5 views
-2

このチックタックトーゲームは、ユーザーベースのコンピューターベースです。 この課題では、不足している部分を記入することになっています。これは、mainメソッドとmoveAIメソッドを終了するだけです(他のメソッドは既にプリセットされていますので、呼び出す必要があります)。このチック・タック・トー・ゲームの仕事をするためにコードを追加する必要があるところにコメントがあります。私は、私の主な方法はコメントの下にある2行の空白を除いてほとんど完了していると信じています。私はそれらが何を意味しているのか分かりませんし、現在moveAIメソッドを働かせています。これは、Xをどこかに置いた後にコンピュータが動くランダムな場所を生成する場所です。私は、コンピュータが動いている場所が利用可能であり、ユーザーによってまだ撮影されていないことを確認するために何かを書く方法がわからない。助けを前にありがとう!Java 2-D配列 - チックタックトーゲーム

import java.util.Random; 
import java.util.Scanner; 
public class TicTacToeGame 
{ 
    public static void main (String[] args) 
    { 
     Scanner in = new Scanner (System.in); 
     char[][] board = new char[ 3 ][ 3 ]; 
     int x, y = -1; 
     char winner = 'N'; 
      // Initialize the board to spaces 
      // boolean noWinner = true; 
     char player = 'X'; 
     for (int r = 0; x < board.length(); r++) 
     { 
      for (int c = 0; c < board.length(); c++) 
      { 
       board[r][c] = ' '; 
      } 
     } 

     // Print the game board   
     printBoard(board); 

     // Keep playing while the game isn't finished 
     while (winner == 'N') 
     { 
      while (x < 0 && x >2 && y <0 && y > 2) 
      { 
       System.out.println("Enter the row and column, separated by spaces: "); 
       x = in.nextInt(); 
       y = in.nextInt(); 
      } 


      // Get the location from the user and validate it 


      // Mark the position in the board according to the user's specified location 


      // Print the board after the user plays 
      printBoard(board); 
      // Check to see if the game is finished. If it is, break out of the loop. 

      // Have the AI make a move 
      moveAI(board); 
      // Print the board after the AI plays 
      printBoard(board); 

      // Check to see who the winner is 
      winner = checkWinner(board); 
     } 
     // If the winner is 'X' or 'O', print that, otherwise, it is a tie 
     if (winner == 'X') 
      System.out.println("X is the winner!"); 
     else if (winner == 'O') 
      System.out.println("O is the winner!"); 
     else 
      System.out.println("Tie"); 


    } 
    /** 
    * Makes a move for the AI, and marks the board with an 'O'. 
    * 
    * @param board The game board 
    */ 
public static void moveAI (char[][] board) 
    { 
     int x,y = -1; 
     Random r = new Random(); 
     x = r.nextInt(3); 
     y = r.nextInt(3); 
     boolean open = false; 

     // Validate that the random location generated is valid. 
     while (x < 0 && x >2 && y < 0 && y > 2) 
     { 
      x = r.nextInt(3); 
      y = r.nextInt(3); 
     } 
     while (open == false) 
     { 
      if (board[r][c] != ' ') 
      while (x < 0 && x >2 && y < 0 && y > 2) 
      { 
       x = r.nextInt(3); 
       y = r.nextInt(3); 
      } 
     } 
     } 

      // Keep recalculating the location if the one generated is not 
      // if (board[r][c] != ' ') 
      //{ 


      // an empty space. 
      System.out.print(" "); 

      // Be sure to mark the position in the board with an 'O' 
      board[][]=in.nextInt(3) + 'O'; 
     } 

     /** 
     * Prints out the tic-tac-toe board 
     * 
     * @param board The game board 
     */ 
public static void printBoard (char[][] board) 
     { 
      // Box drawing unicode characters: 

      char a = '\u250c';    // U+250C : top-left 
      char b = '\u2510';    // U+2510 : top-right 
      char c = '\u2514';    // U+2514 : bottom-left 
      char d = '\u2518';    // U+2518 : bottom-right 
      char e = '\u252c';    // U+252C : top-vertical-connector 
      char f = '\u2534';    // U+2534 : bottom-vertical-connector 
      char g = '\u251c';    // U+251C : left-horizontal-connector 
      char h = '\u2524';    // U+2524 : right-horizontal-connector 
      char i = '\u253c';    // U+253C : center plus sign connector 
      char j = '\u2500';    // U+2500 : horizontal 
      char k = '\u2502';    // U+2502 : vertical 
      String l = j + "" + j + "" + j; // Three horizontals 

      // Print out the game board 

      System.out.printf ("\n 0 1 2\n" + 
        " %c%s%c%s%c%s%c\n" + 
        "0 %c %c %c %c %c %c %c\n" + 
        " %c%s%c%s%c%s%c\n" + 
        "1 %c %c %c %c %c %c %c\n" + 
        " %c%s%c%s%c%s%c\n" + 
        "2 %c %c %c %c %c %c %c\n" + 
        " %c%s%c%s%c%s%c\n\n", 
        a, l, e, l, e, l, b, 
        k, board[0][0], k, board[0][1], k, board[0][2], k, 
        g, l, i, l, i, l, h, 
        k, board[1][0], k, board[1][1], k, board[1][2], k, 
        g, l, i, l, i, l, h, 
        k, board[2][0], k, board[2][1], k, board[2][2], k, 
        c, l, f, l, f, l, d); 

     } 

     /** 
     * Checks the result of the game 
     * 
     * @param board The game board 
     * @return   'X' if 'X' is the winner 
     *     'O' if 'O' is the winner 
     *     'T' if the game is a tie 
     *     'N' if the game isn't finished 
     */ 
public static char checkWinner(char[][] board) 
     { 
      if (board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X' ||  // Check row 0 
        board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X' || // Check row 1 
        board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X' || // Check row 2 
        board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X' || // Check col 0 
        board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X' || // Check col 1 
        board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X' || // Check col 2 
        board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X' || // Check diag \ 
        board[0][2] == 'X' && board[1][1] == 'X' && board[2][0] == 'X' || // Check diag/

        board[0][0] == 'x' && board[0][1] == 'x' && board[0][2] == 'x' || // Check row 0 
        board[1][0] == 'x' && board[1][1] == 'x' && board[1][2] == 'x' || // Check row 1 
        board[2][0] == 'x' && board[2][1] == 'x' && board[2][2] == 'x' || // Check row 2 
        board[0][0] == 'x' && board[1][0] == 'x' && board[2][0] == 'x' || // Check col 0 
        board[0][1] == 'x' && board[1][1] == 'x' && board[2][1] == 'x' || // Check col 1 
        board[0][2] == 'x' && board[1][2] == 'x' && board[2][2] == 'x' || // Check col 2 
        board[0][0] == 'x' && board[1][1] == 'x' && board[2][2] == 'x' || // Check diag \ 
        board[0][2] == 'x' && board[1][1] == 'x' && board[2][0] == 'x')  // Check diag/


      { 
       return 'X'; 
      } 
      else if (board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O' || // Check row 0 
        board[1][0] == 'O' && board[1][1] == 'O' && board[1][2] == 'O' || // Check row 1 
        board[2][0] == 'O' && board[2][1] == 'O' && board[2][2] == 'O' || // Check row 2 
        board[0][0] == 'O' && board[1][0] == 'O' && board[2][0] == 'O' || // Check col 0 
        board[0][1] == 'O' && board[1][1] == 'O' && board[2][1] == 'O' || // Check col 1 
        board[0][2] == 'O' && board[1][2] == 'O' && board[2][2] == 'O' || // Check col 2 
        board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O' || // Check diag \ 
        board[0][2] == 'O' && board[1][1] == 'O' && board[2][0] == 'O' || // Check diag/

        board[0][0] == 'o' && board[0][1] == 'o' && board[0][2] == 'o' || // Check row 0 
        board[1][0] == 'o' && board[1][1] == 'o' && board[1][2] == 'o' || // Check row 1 
        board[2][0] == 'o' && board[2][1] == 'o' && board[2][2] == 'o' || // Check row 2 
        board[0][0] == 'o' && board[1][0] == 'o' && board[2][0] == 'o' || // Check col 0 
        board[0][1] == 'o' && board[1][1] == 'o' && board[2][1] == 'o' || // Check col 1 
        board[0][2] == 'o' && board[1][2] == 'o' && board[2][2] == 'o' || // Check col 2 
        board[0][0] == 'o' && board[1][1] == 'o' && board[2][2] == 'o' || // Check diag \ 
        board[0][2] == 'o' && board[1][1] == 'o' && board[2][0] == 'o')  // Check diag/

      { 
       return 'O'; 
      } 

      boolean finished = true; 

      // If there is a blank space in the board, the game isn't finished yet 
      for (int i = 0; i < board.length; i++) 
       for (int j = 0; j < board[ i ].length; j++) 
        if (board[ i ][ j ] == ' ') 
         finished = false; 

      // If the board is finished and 'X' or 'O' wasn't returned, then it is a tie 
      // Otherwise, the game is not finished yet 
      if (finished) 
       return 'T'; 
      else 
       return 'N'; 
     } 
    } 
+0

あなたのためにコードを記述していただきたいようです。多くのユーザーは、苦労しているコーダーのコードを作成したいと考えていますが、通常、ポスターが既に問題を解決しようとしているときにのみ役立ちます。この努力を実証する良い方法は、これまでに書いたコード、サンプル入力(もしあれば)、予想される出力、実際に得られる出力(コンソール出力、トレースバックなど)を含めることです。あなたが提供する詳細があれば、受け取る可能性のある回答が増えます。 [FAQ]と[ask]を確認してください。 –

答えて

0

//ユーザーから位置を取得し、それを検証する

すでに前の行にこれをやっているように見えます

// you're validating the input here 
while (x < 0 && x >2 && y <0 && y > 2) 
{ 
    System.out.println("Enter the row and column, separated by spaces: "); 
    // you're getting the location from the user here 
    x = in.nextInt(); 
    y = in.nextInt(); 
} 

あなたはおそらくまだ必要ユーザーが入力した位置がすでに入力されている位置かどうかを確認するために配列を調べる必要があります。 moveAIにはあなたにこれに関するヒントがあります。 board[x][y]がスペースかどうかをチェックします。

また、xyの値をリセットしないため、そこにバグがあると思いますので、次回にループが回ったときにはループしません。 xyは最後のターンの値を持っているので、ループ条件はfalseと評価されます。私はwhile(...){...}ループの代わりにdo{...}while(...);ループを使用してこの問題を解決できると思います。マークは、ボードの位置は、ユーザーの指定した場所

によれば、これはちょうどboard[x][y]'O'を置く意味

//。

//ゲームが終了したかどうかを確認してください。そうであれば、ループから抜け出す。

これはcheckWinnerのようです。ゲームが完了すれば'T'、まだ進行している場合は'N'を返します。

1. generate random x, y coordinates 
2. check if board[x][y] is already used (there's a comment that tells how) 
3. if it's used, go back to 1, otherwise put an 'X' in that space and return

はまた、あなたがAIコードでwhile (x < 0 && x >2 && y < 0 && y > 2)をチェックするループを必要としない:AIコードの場合


は、ここで私はあなたがやるべきことだと思う何のためにいくつかの擬似コードです。 Random.nextInt(3)はすでに 0-2の範囲の数値を返します。