TicTacToe使用Java中的2D数组。

huangapple go评论67阅读模式
英文:

TicTacToe using 2D array in java

问题

以下是您要翻译的代码部分:

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();
            }
        } 
    }
}

希望这对您有所帮助。如果您有其他问题,请随时提出。

英文:
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.

答案1

得分: 1

你只需要编写一些代码来检查每一行、列和对角线是否有3个匹配项。你可以利用for循环来更高效地完成这个任务。

public static char checkWinner(char[][] board) {
    // 检查行
    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];
        }
    }

    // 检查列
    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];
        }
    }

    // 检查对角线
    if (board[0][0] != '?' && board[0][0] == board[1][1] && board[1][1] == board[2][2]) {
        return board[0][0];
    }

    // 检查反对角线
    if (board[0][2] != '?' && board[0][2] == board[1][1] && board[1][1] == board[2][0]) {
        return board[0][2];
    }

    // 无获胜者
    return '?';
}

如果你还没有接触过for循环,有很多好的教程可以帮助你,例如 https://www.w3schools.com/java/java_for_loop.asp

(注意,你也可以使用循环来简化你的现有代码)

英文:

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 &lt; 3; i++) {
        if (board[i][0] != &#39;?&#39; &amp;&amp; board[i][0] == board[i][1] &amp;&amp; board[i][1] == board[i][2]) {
            return board[i][0];
        }
    }

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

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

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

    // No winner
    return &#39;?&#39;;
}

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)

huangapple
  • 本文由 发表于 2023年2月19日 17:43:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499217.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定