TicTacToe使用Java中的2D数组。

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

TicTacToe using 2D array in java

问题

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

  1. public static void main(String[] args) {
  2. Scanner sc = new Scanner(System.in);
  3. char[][] board = {
  4. {'?', '?', '?'},
  5. {'?', '?', '?'},
  6. {'?', '?', '?'}
  7. };
  8. System.out.print("Type any key to play the game and type 'n' to stop the game: ");
  9. String Stop = sc.nextLine();
  10. while(true){
  11. if(Stop.equals("n"))break;
  12. System.out.print("Player" + "[" + "X" + "]: ");
  13. int PlayerX = sc.nextInt();
  14. if(PlayerX == 1){
  15. board[2][0] = 'x';
  16. }
  17. if(PlayerX == 2){
  18. board[2][1] = 'x';
  19. }
  20. if(PlayerX == 3){
  21. board[2][2] = 'x';
  22. }
  23. if(PlayerX == 4){
  24. board[1][0] = 'x';
  25. }
  26. if(PlayerX == 5){
  27. board[1][1] = 'x';
  28. }
  29. if(PlayerX == 6){
  30. board[1][2] = 'x';
  31. }
  32. if(PlayerX == 7){
  33. board[0][0] = 'x';
  34. }
  35. if(PlayerX == 8){
  36. board[0][1] = 'x';
  37. }
  38. if(PlayerX == 9){
  39. board[0][2] = 'x';
  40. }
  41. for(char[] x1 : board){
  42. for(char x2 : x1){
  43. System.out.print(x2 + "\t");
  44. }
  45. System.out.println();
  46. }
  47. System.out.print("Player" + "[" + "O" + "]: ");
  48. int PlayerO = sc.nextInt();
  49. if(PlayerO == 1){
  50. board[2][0] = 'o';
  51. }
  52. if(PlayerO == 2){
  53. board[2][1] = 'o';
  54. }
  55. if(PlayerO == 3){
  56. board[2][2] = 'o';
  57. }
  58. if(PlayerO == 4){
  59. board[1][0] = 'o';
  60. }
  61. if(PlayerO == 5){
  62. board[1][1] = 'o';
  63. }
  64. if(PlayerO == 6){
  65. board[1][2] = 'o';
  66. }
  67. if(PlayerO == 7){
  68. board[0][0] = 'o';
  69. }
  70. if(PlayerO == 8){
  71. board[0][1] = 'o';
  72. }
  73. if(PlayerO == 9){
  74. board[0][2] = 'o';
  75. }
  76. for(char[] x1 : board){
  77. for(char x2 : x1){
  78. System.out.print(x2 + "\t");
  79. }
  80. System.out.println();
  81. }
  82. }
  83. }
  84. }

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

英文:
  1. public static void main(String[] args) {
  2. Scanner sc = new Scanner(System.in);
  3. char[][] board = {
  4. {'?','?','?'},
  5. {'?','?','?'},
  6. {'?','?','?'}
  7. };
  8. System.out.print("Type any key to play the game and type 'n' to stop the game: ");
  9. String Stop = sc.nextLine();
  10. while(true){
  11. if(Stop.equals("n"))break;
  12. System.out.print("Player" + "[" + "X" + "]: ");
  13. int PlayerX = sc.nextInt();
  14. if(PlayerX == 1){
  15. board[2][0] = 'x';
  16. }
  17. if(PlayerX == 2){
  18. board[2][1] = 'x';
  19. }
  20. if(PlayerX == 3){
  21. board[2][2] = 'x';
  22. }
  23. if(PlayerX == 4){
  24. board[1][0] = 'x';
  25. }
  26. if(PlayerX == 5){
  27. board[1][1] = 'x';
  28. }
  29. if(PlayerX == 6){
  30. board[1][2] = 'x';
  31. }
  32. if(PlayerX == 7){
  33. board[0][0] = 'x';
  34. }
  35. if(PlayerX == 8){
  36. board[0][1] = 'x';
  37. }
  38. if(PlayerX == 9){
  39. board[0][2] = 'x';
  40. }
  41. for(char[] x1 : board){
  42. for(char x2 : x1){
  43. System.out.print(x2 + "\t");
  44. }
  45. System.out.println();
  46. }
  47. System.out.print("Player" + "[" + "O" + "]: ");
  48. int PlayerO = sc.nextInt();
  49. if(PlayerO == 1){
  50. board[2][0] = 'o';
  51. }
  52. if(PlayerO == 2){
  53. board[2][1] = 'o';
  54. }
  55. if(PlayerO == 3){
  56. board[2][2] = 'o';
  57. }
  58. if(PlayerO == 4){
  59. board[1][0] = 'o';
  60. }
  61. if(PlayerO == 5){
  62. board[1][1] = 'o';
  63. }
  64. if(PlayerO == 6){
  65. board[1][2] = 'o';
  66. }
  67. if(PlayerO == 7){
  68. board[0][0] = 'o';
  69. }
  70. if(PlayerO == 8){
  71. board[0][1] = 'o';
  72. }
  73. if(PlayerO == 9){
  74. board[0][2] = 'o';
  75. }
  76. for(char[] x1 : board){
  77. for(char x2 : x1){
  78. System.out.print(x2 + "\t");
  79. }
  80. System.out.println();
  81. }
  82. }
  83. }
  84. }

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循环来更高效地完成这个任务。

  1. public static char checkWinner(char[][] board) {
  2. // 检查行
  3. for (int i = 0; i < 3; i++) {
  4. if (board[i][0] != '?' && board[i][0] == board[i][1] && board[i][1] == board[i][2]) {
  5. return board[i][0];
  6. }
  7. }
  8. // 检查列
  9. for (int j = 0; j < 3; j++) {
  10. if (board[0][j] != '?' && board[0][j] == board[1][j] && board[1][j] == board[2][j]) {
  11. return board[0][j];
  12. }
  13. }
  14. // 检查对角线
  15. if (board[0][0] != '?' && board[0][0] == board[1][1] && board[1][1] == board[2][2]) {
  16. return board[0][0];
  17. }
  18. // 检查反对角线
  19. if (board[0][2] != '?' && board[0][2] == board[1][1] && board[1][1] == board[2][0]) {
  20. return board[0][2];
  21. }
  22. // 无获胜者
  23. return '?';
  24. }

如果你还没有接触过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

  1. public static char checkWinner(char[][] board) {
  2. // Check rows
  3. for (int i = 0; i &lt; 3; i++) {
  4. if (board[i][0] != &#39;?&#39; &amp;&amp; board[i][0] == board[i][1] &amp;&amp; board[i][1] == board[i][2]) {
  5. return board[i][0];
  6. }
  7. }
  8. // Check columns
  9. for (int j = 0; j &lt; 3; j++) {
  10. if (board[0][j] != &#39;?&#39; &amp;&amp; board[0][j] == board[1][j] &amp;&amp; board[1][j] == board[2][j]) {
  11. return board[0][j];
  12. }
  13. }
  14. // Check diagonal
  15. if (board[0][0] != &#39;?&#39; &amp;&amp; board[0][0] == board[1][1] &amp;&amp; board[1][1] == board[2][2]) {
  16. return board[0][0];
  17. }
  18. // Check anti-diagonal
  19. if (board[0][2] != &#39;?&#39; &amp;&amp; board[0][2] == board[1][1] &amp;&amp; board[1][1] == board[2][0]) {
  20. return board[0][2];
  21. }
  22. // No winner
  23. return &#39;?&#39;;
  24. }

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:

确定