0

こんにちは、三目並べゲームを書いています。コードのコメントに必要なものを詳しく説明しました。私が今困っているのは getMove メソッドを作ることです。行と列が押された後、if/else ステートメントで getMove メソッドを呼び出す必要があると思いますか?

行/列番号を取得して、ユーザーが入力したものをボードに入れる方法がわかりません。

これが私のコードです:

import java.util.*;

public class TicTac{
   //declare a constant variable
   public static final int SIZE = 3; //size of each row and each column

   public static void main(String[] args) {

      //initialize the board
      char[][] board = new char[3][3];

      //display the board
      displayBoard(board);

      //prompt for the first player
      //determine if X or O is pressed

      System.out.println("Who wants to go first (X or O)? ");
      Scanner xOrO = new Scanner(System.in);
      String entOp = xOrO.nextLine();
      char enterOp = entOp.charAt(0);

      if (enterOp == 'X'){
           System.out.println("Enter a row (0,1,2) for player X: ");
           Scanner enterRow = new Scanner(System.in);
           int fTurn = enterRow.nextInt();

           System.out.println("Enter a column (0,1,2) for player X: ");
           Scanner enterCol = new Scanner(System.in);
           int fCol = enterCol.nextInt();

      }  else if (enterOp == 'O') {
           System.out.println("Enter a row (0,1,2) for player O: ");
           Scanner enterRow = new Scanner(System.in);
           int fTurn = enterRow.nextInt();

           System.out.println("Enter a column (0,1,2) for player X: ");
           Scanner enterCol = new Scanner(System.in);
           int fCol = enterCol.nextInt();

      }  else {
           System.out.println("Must enter either X or O");  
         }

      //and display the board
      //displayBoard(board);

      }

   //initializeBoard method


   //displayBoard method
   public static void drawLine() {
      for (int i = 0; i <= 9 * SIZE; i++) {
         System.out.print("-");
      }
      System.out.println();
   }

   public static void displayBoard(char[][] board) {
      drawLine();
      for (int i = 0; i < SIZE; i++) {
         for (int j = 0; j < SIZE; j++) {
            System.out.print("| " + board[i][j] + " ");
         }
         System.out.println("|");
         drawLine();
      }
      System.out.println();
   }


   //getMove method: to prompt the current player for target position. And place the mark in the position if the position is available.
//    public static void getMove() {
//    
//    
//    
//    
//    
//    }
   //findWinner method: after each move, check the board see if there is a winner



   //hasEmptyCell method: check if there is still empty spot in the board
}
4

1 に答える 1

0

私は動きを得るためにこのようなことを試みます。私がJavaで遊んでから少し経ったことを覚えておいてください。2D char 配列には最初から null 値があると想定しています。その宣言を変更してグローバルに移動することをお勧めします:

 char[][] board = {{'', '', ''},{'', '', ''},{'', '', ''}};

次に、2 番目の読み取りが次のように行われた後に getMove を呼び出します。

int x = Integer.parseInt(firstTurn);
int y = Integer.parseInt(firstCol);
getMove(x, y, 'X');

ユーザーが実際に整数を入力するかどうかわからないため、例外をキャッチする必要があります。これを行うには、ある種のループ (while など) が非常にうまく機能します。

getMove 関数は次のようになります。

public static bool getMove(int x, int y, char player) {
    if (board[x][y] == '') {
        board[x][y] = player;
        return true;  //Here you are returning true to show the spot was available.
    }
    return false; //And here you are returning false to show the spot was not available.
}
于 2013-10-23T00:02:47.663 に答える