-3
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    
    char[][] board = {
        {'?','?','?'},
        {'?','?','?'},
        {'?','?','?'}
    };
    
    System.out.print("Type any key to play the game and type 'n' to stop the game: ");
    String Stop = sc.nextLine();
    while(true){ 
        if(Stop.equals("n"))break;
            System.out.print("Player" + "[" + "X" + "]: ");
            int PlayerX = sc.nextInt();            
           
            if(PlayerX == 1){
                board[2][0] = 'x';
            }
            if(PlayerX == 2){
                board[2][1] = 'x';
            }
            if(PlayerX == 3){
                board[2][2] = 'x';
            }
            if(PlayerX == 4){
                board[1][0] = 'x';
            }
            if(PlayerX == 5){
                board[1][1] = 'x';
            }
            if(PlayerX == 6){
                board[1][2] = 'x';
            }
            if(PlayerX == 7){
                board[0][0] = 'x';
            }
             
            if(PlayerX == 8){
                board[0][1] = 'x';
            }
            if(PlayerX == 9){
                board[0][2] = 'x';
            }
            
            for(char[] x1 : board){
                for(char x2 : x1){
                    System.out.print(x2 + "\t");
                }
                System.out.println();
            }
                       
            System.out.print("Player" + "[" + "O" + "]: ");
            int PlayerO = sc.nextInt();
            
            if(PlayerO == 1){
                board[2][0] = 'o';
            }
            if(PlayerO == 2){
                board[2][1] = 'o';
            }
            if(PlayerO == 3){
                board[2][2] = 'o';
            }
            if(PlayerO == 4){
                board[1][0] = 'o';
            }
            if(PlayerO == 5){
                board[1][1] = 'o';
            }
            if(PlayerO == 6){
                board[1][2] = 'o';
            }
            if(PlayerO == 7){
                board[0][0] = 'o';
            }
            if(PlayerO == 8){
                board[0][1] = 'o';
            }
            if(PlayerO == 9){
                board[0][2] = 'o';
            }
            
            for(char[] x1 : board){
                for(char x2 : x1){
                    System.out.print(x2 + "\t");
                }
                System.out.println();
            }
        } 
    }
}

I am trying to make a simple TicTacToe program in Java. I am already done in placing the X and O, but I am struggling on checking if there is a winner.

I am confused on what code I will type to check the winner of the program.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197

1 Answers1

1

You simply just need to write some code to check if there are 3 matches in each row column and diagonal.

You can utilise for loops to do this more efficiently

public static char checkWinner(char[][] board) {
    // Check rows
    for (int i = 0; i < 3; i++) {
        if (board[i][0] != '?' && board[i][0] == board[i][1] && board[i][1] == board[i][2]) {
            return board[i][0];
        }
    }

    // Check columns
    for (int j = 0; j < 3; j++) {
        if (board[0][j] != '?' && board[0][j] == board[1][j] && board[1][j] == board[2][j]) {
            return board[0][j];
        }
    }

    // Check diagonal
    if (board[0][0] != '?' && board[0][0] == board[1][1] && board[1][1] == board[2][2]) {
        return board[0][0];
    }

    // Check anti-diagonal
    if (board[0][2] != '?' && board[0][2] == board[1][1] && board[1][1] == board[2][0]) {
        return board[0][2];
    }

    // No winner
    return '?';
}

If you havent come accross for loops yet, there are plenty of good tutorials out there such as https://www.w3schools.com/java/java_for_loop.asp

(Note, you can also use loops to clean up some of your pre-existing code)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
JDChris100
  • 146
  • 9