0

我现在正在写我的井字游戏方法,现在我遇到了各种各样的错误。我有我编译的 findWinner 方法,但是当我添加我的 updateBoard 方法时,我开始遇到很多麻烦。

有人可以帮助我的代码吗?

**现在有点乱,但会更新

   import java.util.*;

public class TicTac{
   //declare a constant variable
   public static final int SIZE = 3; //size of each row and each column
//    static int MAX_MOVES = SIZE * SIZE;
      static int[][] moveBoard = new int[SIZE][SIZE];

   enum Check{Blank, X, O};
   static int n = 3;
   static Check[][] board = new Check[n][n];
   int moveCount;
 //   static char[][] board = new char[SIZE][SIZE];


   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();

           //return updateBoard(board[fTurn][fCol]);

      }  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 O: ");
           Scanner enterCol = new Scanner(System.in);
           int fCol = enterCol.nextInt();

           //return updateBoard(board[fTurn][fCol]);

      }  else {
           System.out.println("Must enter either capital 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.

   //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

   //findWinner method: after each move, check the board see if there is a winner

    public void findWinner(int x, int y, Check s){
        if(board[x][y] == Check.Blank){
            board[x][y] = s;
        }
        moveCount++;

        //check end conditions

        //check col
        for(int i = 0; i < n; i++){
            if(board[x][i] != s)
                break;
            if(i == n-1){
                System.out.println("S win!");
            }
        }

        //check row
        for(int i = 0; i < n; i++){
            if(board[i][y] != s)
                break;
            if(i == n-1){
                //report win for s
            System.out.println("S win!");
            }
        }

        //check diag
        if(x == y){
            //we're on a diagonal
            for(int i = 0; i < n; i++){
                if(board[i][i] != s)
                    break;
                if(i == n-1){
                    //report win for s
               System.out.println("S win!");
                }
            }
        }

            //check anti diag (thanks rampion)
        for(int i = 0;i<n;i++){
            if(board[i][(n-1)-i] != s)
                break;
            if(i == n-1){
                //report win for s
            System.out.println("S win!");
            }
        }

        //check draw
        if(moveCount == (n^2 - 1)){
            //report draw
         System.out.println("Cats Game!");
        }
    } 

   public static boolean updateBoard(int move, char ch) {
      char[][] board = new char[3][3];
      int i = (move-1)/3;
      int j = (move-1)%3;
      boolean status;
     if (1 > move || move > 9) {
        System.out.println("Invalid Entry"); 
     } else if (moveBoard[i][j] == -1) {
        System.out.println("Entry already marked,please re-enter your move.");
        status = false;
     } else {
        board[i][j] = ch;
        moveBoard[i][j] = -1;
        status = true;
     }
     return status;
   }
}
4

0 回答 0