(Java) Tic-Tac-Toe game using 2 dimensional Array

First off:

 while (board[row][column] == 'X' || board[row][column] == 'O') {
            System.out.println("This spot is occupied. Please try again");
        }

This will create a infinite loop because row and column shouldn’t change you should ask for new input!

Also

public static Boolean winner(char[][] board){
    for (int i = 0; i< board.length; i++) {
        for (int j = 0; j < board[0].length; j++) {
            if (board[i][j] == 'O' || board[i][j] == 'X') {
                return false;
            }
        }
    }

As soon you hit ‘O’ or ‘X’ you will exit the Method with a false (no winner)

What you probably want to check is if every spot is occupied

public static Boolean winner(char[][] board){
   //Boolean which is true until there is a empty spot
   boolean occupied = true;
   //loop and check if there is empty space or if its a draw
    for (int i = 0; i< board.length; i++) {
        for (int j = 0; j < board[0].length; j++) {
            //Check if spot is not 'O' or not 'X' => empty 
            if (board[i][j] != 'O' || board[i][j] != 'X') {
                occupied = false;
            }
        }
    }
    if(occupied)
        return false;
   //Check if someone won
    return (board[0][0] == board [0][1] && board[0][0] == board [0][2]) ||
        (board[0][0] == board [1][1] && board[0][0] == board [2][2]) ||
        (board[0][0] == board [1][0] && board[0][0] == board [2][0]) ||
        (board[2][0] == board [2][1] && board[2][0] == board [2][2]) ||
        (board[2][0] == board [1][1] && board[0][0] == board [0][2]) ||
        (board[0][2] == board [1][2] && board[0][2] == board [2][2]) ||
        (board[0][1] == board [1][1] && board[0][1] == board [2][1]) ||
        (board[1][0] == board [1][1] && board[1][0] == board [1][2]);
}

This would now check if there is a winner or its a tie

Occupied == true == tie == return false

Winner == return true

But you have three states:

  • Win
  • Tie
  • NotFinished

With the changed Method you will NOT finish the game until you win.

Reason:

 while(!winner(board) == true)

This makes the game run as long as there is NO winner (winner() will be false because everything is occupied or there is no winner)

while(!false==true) => while(true) 

You could write a method similar to winner but it only checks if the board has empty spots:

public static Boolean hasEmptySpot(char[][] board){
   //loop and check if there is empty space 
    for (int i = 0; i< board.length; i++) {
        for (int j = 0; j < board[0].length; j++) {
            if (board[i][j] != 'O' && board[i][j] != 'X') {
                return true;
            }
        }
    }
    return false;
}

//New code 
while(hasEmptySpot(board) || !winner(board)){
          //Your code for the game here
     ....
    }

this would end the game when there is no empty spot left After you finished the game you can call winner(board) and it will return if you tied or won!

By creating hasEmptySpot() you could change your winner method to

public static Boolean winner(char[][] board){
    return (board[0][0] == board [0][1] && board[0][0] == board [0][2]) ||
        (board[0][0] == board [1][1] && board[0][0] == board [2][2]) ||
        (board[0][0] == board [1][0] && board[0][0] == board [2][0]) ||
        (board[2][0] == board [2][1] && board[2][0] == board [2][2]) ||
        (board[2][0] == board [1][1] && board[0][0] == board [0][2]) ||
        (board[0][2] == board [1][2] && board[0][2] == board [2][2]) ||
        (board[0][1] == board [1][1] && board[0][1] == board [2][1]) ||
        (board[1][0] == board [1][1] && board[1][0] == board [1][2]);
}

Why? Because you finished the game and you know there are only two possible outcomes Win or Tie.

I hope this helped you a little bit.

EDIT Had a logic error myself!

First mistake: you still need to check if there is a winner while the game is running forgot that point!

while(hasEmptySpot(board) || !winner(board)){
}

Now this will quit the game loop when there is a winner or no empty spots is left

Second mistake: In hasEmptySpot()

 if (board[i][j] != 'O' && board[i][j] != 'X') {
                return true;

not

 if (board[i][j] != 'O' || board[i][j] != 'X') {
                return true;

Fixed it in the upper examples.

I’m sorry for the inconvenience!

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)