TicTacToe 在 java 中使用二维数组

问题描述 投票:0回答:0

大家好,我需要帮助。
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 检查程序的获胜者。

java arrays 2d tic-tac-toe
© www.soinside.com 2019 - 2024. All rights reserved.