TicTacToe在java中使用二维数组

omjgkv6w  于 2023-02-20  发布在  Java
关注(0)|答案(1)|浏览(128)

大家好我需要帮助.
public static void main(String[] args){扫描器sc =新扫描器(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();
        }

    } 
    
    
}

我正在尝试用java做一个简单的tictactoes程序。我已经完成了放置X和O的工作,但我正在努力检查是否有赢家。
我很困惑什么代码,我将类型o检查程序的赢家。

pgvzfuti

pgvzfuti1#

您只需要编写一些代码来检查每行、列和对角线中是否有3个匹配项。
您可以使用for循环来更有效地执行此操作

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 '?';
}

如果你还没有接触过for循环,有很多好的教程,比如https://www.w3schools.com/java/java_for_loop.asp
(Note,您也可以使用循环来清理一些预先存在的代码)

相关问题