windows 如何在井字游戏中重置数组

wn9m85ua  于 2022-11-30  发布在  Windows
关注(0)|答案(1)|浏览(114)

大家好,我是一个新的C,我正试图使我的大学项目井字游戏,我正在努力如何重置我的阵列在我的游戏中,每次我再次玩它不重置阵列,有人能帮助我吗?(为我糟糕的英语sry)
下面是代码:

  1. `#include <stdio.h>
  2. #include <conio.h>
  3. #include <stdlib.h>
  4. char space[3][3] = {
  5. {'1','2','3'},
  6. {'4','5','6'},
  7. {'7','8','9'}
  8. };
  9. void board();
  10. int checkWin();
  11. int game();
  12. void reset();
  13. int main(){
  14. int choice = -1;
  15. do{
  16. printf("\n\n\n\n\n\n\n\t\t\t ================\n");
  17. printf("\t\t\t Tic Tac Toe\n");
  18. printf("\t\t\t ================\n");
  19. printf("\t\t -----------Menu-----------\n\n");
  20. printf("\t\t 1. Play\n");
  21. printf("\t\t 2. Exit\n");
  22. scanf("%d", &choice);
  23. switch(choice){
  24. case 1: game();
  25. break;
  26. case 2: printf("Goodbye!!");
  27. exit(0);
  28. break;
  29. default: printf(".......Wrong Key !.......Try Again!......");
  30. break;
  31. }
  32. }while(choice != 0);
  33. }
  34. int game(){
  35. int player = 1, i, choice;
  36. char mark;
  37. do
  38. {
  39. system("cls");
  40. board();
  41. player = (player % 2) ? 1 : 2;
  42. printf("Player %d, enter a number: ", player);
  43. scanf("%d", &choice);
  44. mark = (player == 1) ? 'X' : 'O';
  45. if (choice == 1)
  46. space[0][0] = mark;
  47. else if (choice == 2)
  48. space[0][1] = mark;
  49. else if (choice == 3)
  50. space[0][2] = mark;
  51. else if (choice == 4)
  52. space[1][0] = mark;
  53. else if (choice == 5)
  54. space[1][1] = mark;
  55. else if (choice == 6)
  56. space[1][2] = mark;
  57. else if (choice == 7)
  58. space[2][0] = mark;
  59. else if (choice == 8)
  60. space[2][1] = mark;
  61. else if (choice == 9)
  62. space[2][2] = mark;
  63. else
  64. {
  65. printf("Invalid move ");
  66. player--;
  67. getch();
  68. }
  69. i = checkWin();
  70. player++;
  71. }while (i == - 1);
  72. board();
  73. reset();
  74. if (i == 1)
  75. printf("==>\aPlayer %d win \n\n", --player);
  76. else
  77. printf("==>\aGame draw\n\n");
  78. getch();
  79. return 0;
  80. }
  81. int checkWin(){
  82. if (space[0][0] == space[0][1] && space[0][1] == space[0][2])
  83. return 1;
  84. else if (space[1][0] == space[1][1] && space[1][1] == space[1][2])
  85. return 1;
  86. else if (space[2][0] == space[2][1] && space[2][1] == space[2][2])
  87. return 1;
  88. else if (space[0][0] == space[1][0] && space[1][0] == space[2][0])
  89. return 1;
  90. else if (space[0][1] == space[1][1] && space[1][1] == space[2][1])
  91. return 1;
  92. else if (space[0][2] == space[1][2] && space[1][2] == space[2][2])
  93. return 1;
  94. else if (space[0][0] == space[1][1] && space[1][1] == space[2][2])
  95. return 1;
  96. else if (space[0][2] == space[1][1] && space[1][1] == space[2][0])
  97. return 1;
  98. else if (space[0][0] != space[0][0] && space[0][1] != space[0][1] && space[0][2] != space[0][2] &&
  99. space[1][0] != space[1][0] && space[1][1] != space[1][1] && space[1][2] != space[1][2] && space[2][0]
  100. != space[2][0] && space[2][1] != space[2][1] && space[2][2] != space[2][1])
  101. return 0;
  102. else
  103. return - 1;
  104. }
  105. void reset(){
  106. for (int i = 0; i < 3; i++)
  107. {
  108. for (int j = 0; j < 3; j++)
  109. {
  110. space[i][j] = 0;
  111. }
  112. }
  113. }
  114. void board(){
  115. system("cls");
  116. printf("\n\n\tTic Tac Toe\n\n");
  117. printf("Player 1 (X) - Player 2 (O)\n\n\n");
  118. printf(" | | \n");
  119. printf(" %c | %c | %c \n", space[0][0], space[0][1], space[0][2]);
  120. printf("_____|_____|_____\n");
  121. printf(" | | \n");
  122. printf(" %c | %c | %c \n", space[1][0], space[1][1], space[1][2]);
  123. printf("_____|_____|_____\n");
  124. printf(" | | \n");
  125. printf(" %c | %c | %c \n", space[2][0], space[2][1], space[2][2]);
  126. printf(" | | \n\n");
  127. }`

我尝试使用for循环,但它不工作,我如何解决这个问题?

  1. `void reset(){
  2. for (int i = 0; i < 3; i++)
  3. {
  4. for (int j = 0; j < 3; j++)
  5. {
  6. space[i][j] = 0;
  7. }
  8. }
  9. }`
y53ybaqx

y53ybaqx1#

我在C语言中并不流畅,但这不起作用:

  1. char resetted[3][3] = {
  2. {'0','0','0'},
  3. {'0','0','0'},
  4. {'0','0','0'}
  5. };
  6. void reset(){
  7. space = resetted;
  8. }

相关问题