Java tic tac toe

时间:2013-10-22 23:47:09

标签: java arrays methods tic-tac-toe

您好我正在写一个tic tac toe游戏。我在代码中的评论中拼写出了我需要的东西。我现在遇到麻烦的是制作一个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
}

1 个答案:

答案 0 :(得分:0)

我会尝试这样的事情来取得进展。请记住,自从我使用java以来​​它已经有点了。我假设2D char数组有空值开头。我建议将该声明更改为并将其移动到全局中:

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

然后我会在第二次读取完成之后调用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.
}