Search code examples
javamethodsmultidimensional-arraytic-tac-toe

2d array for tictactoe class being problematic


For this program I'm creating the board that will be the basis for my tictactoe game that I have to build. It will be a very simple program, using only the basics of java. At the moment I'm having trouble getting my setX() and setO() methods to work properly. This was the exact text I was given to build my two methods on.

public void setX(int index) and public void setO(int index) If index is within the range of the board and BLANK, this method should assign X or O to the appropriate index in board (setX should place an X; setO, an O). Remember that TicTacToe passes numbers in the range 1-9, but the indices in board are one less than that (in the range 0-8).

My specific problem is how to set the 'x' and 'o' to the proper index. When I run my unit test file (I'm using bluejay) It fails all test cases with these methods. It will return that array first differed at element[0][2](or [1][1] or any combination in my array); expect (or ) but was:

public class Board
{
    // instance variables - replace the example below with your own
    public int SIZE = 3;
    private char BLANK = 'N';
    private char [][] board;

    /**
     * Constructor for objects of class Board, intializes all elements to BLANK, 
     * creates board with SIZE elements
     */
  public Board()
    {
        // initialise instance variables
        board = new char[SIZE][SIZE];
        for(int i = 0; i < board.length; i++){
            for(int j = 0; j < board.length; j++){
                board[i][j] = BLANK;
        }
    }
   }
   /**
    * this method returns the board
    */
   public char [][] getBoard(){

       return board;
    }
    /**
     * prints the elements of the array board in a 3x3 grid
     */
   public void display(){
         for(int i = 0; i < board.length; i++){
            for(int j = 0; j < board.length; j++){
                System.out.print(board[i][j]);
        }
    }
   }
    /**
     * This method assigns X to the appropriate index in board
     */
   public void setX(int index){
       index = index - 1;
          for(int i = 0; i < board.length; i++){
            for(int j = 0; j < board.length; j++){

                if((BLANK <= index) && (index <= board[i][j])){
                    board[i][j] = 'X';
                }
        }
    }

    }
    /**
    * This method assigns O to the appropriate index in board
    */
   public void setO(int index){

       for(int i = 0; i < board.length; i++){
            for(int j = 0; j < board.length; j++){

                if((BLANK <= index) && (index <= board[i][j])){
                    board[i][j] = 'O';
                }
        }
    }
    }
   /**
    * This method returns true if the index is not occupied by an X or O
    */
    public boolean isAvailable(int index){
        boolean available = false;
        for(int i = 0; i < board.length; i++){
            for(int j = 0; j < board.length; j++){
                board[i][j] = BLANK;
               available = true;
        }
    }
        return available;
    }
}

Solution

  • First, I wonder why you even need a two-dimensional array. Why not represent the board as a nine-element single array?

    That aside, the way to convert from a 1-9 position index into the appropriate two-dimensional index is:

    int i = (index - 1) / 3;
    int j = (index - 1) % 3;
    

    Then process board[i][j] for just that one pair of indices. Your setX and setO methods should not be looping.