0

I am writing my tic tac toe method right now and now I am running into all sorts of errors. I have my findWinner method which compitiled, but when I added my updateBoard method I started having a lot of trouble.

Could someone give help to my code?

**A little messy right now but will update

   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;
   }
}
jSeesFor3ver
  • 83
  • 4
  • 16
  • You should delete that giant `switch` statement in `updateBoard`. All you need to do to calculate the row and column is `int i = (move-1)/3;` and `int j = (move-1)%3;`. To check for an invalid entry just check `if (1 > move || move > 9)`. – DaoWen Oct 24 '13 at 01:40
  • You've also declared `board` twice. Once at the instance level as `Check[][] board`, and once in `main` as `char[][] board`. You also seem to think that `updateBoard` is *yet another* copy of your board, since you repeatedly use expressions like `updateBoard[fTurn][fCol]`, but `updateBoard` is a method name... Just *read the compiler errors* and figure out what's wrong. You should also code small sections and check to see if they work. Coding a huge section and then fixing hundreds of compiler errors is not a good strategy. – DaoWen Oct 24 '13 at 01:47
  • @DaoWen Ok so I updated my switch statement. However still getting a couple errors. I am getting really frustrated now and feel helpless. Anyway, which board declaration should I use for my code? It seems I wrote one declaration for the findWinner method and also one for the main. Also, can you help me fix my code so I can at least get the what I have written working? I am seeming to not be able to go from getting the user row and column input, and putting it on the board, then doing my checks. It's drivin me nuts! – jSeesFor3ver Oct 24 '13 at 02:32
  • The switch statement wasn't causing errors (as far as I saw anyway)—it was just way too complicated a solution for a simple problem (calculating row & column from the square number). As for the board, it really doesn't matter which you use. I'd personally use `static Check[][] board` since it's more type safe. However, instead of having the explicit `Blank` type in your `enum`, I'd just use `null` since the entire array will be `null`-filled by default. – DaoWen Oct 24 '13 at 02:35
  • You should also get rid of `static int[][] moveBoard`. You only need one board. I'm sure that having all these different boards of different types floating around is causing you all sorts of problems. Keeping them all in sync would be a huge headache. – DaoWen Oct 24 '13 at 02:46
  • `moveCount == (n^2 - 1)` is not doing what you think it does. In Java the carrot operator `^` doesn't do exponentiation, but rather a [bitwise exclusive or](http://stackoverflow.com/a/2672217/1427124). You should just use `n*n` instead of trying to do `n²`. – DaoWen Oct 24 '13 at 02:49

0 Answers0