Keener_Tic_Tac_Toe(井字棋游戏)代码(c语言)

Keener_Tic_Tac_Toe(井字棋游戏)代码(c语言)

首页休闲益智井字棋大作战更新时间:2024-04-10

遇到的问题:

1.中间有需要清楚键盘缓冲区,具体(?)

2.number char型,强制int型转换是利用ASCII码,相差48.

3.string型c 支持,c并没有,利用char型字符数组解决,char player1[20];scanf("%s",player1);printf("%s",player1);

代码如下:


  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <stdbool.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6. int main(){
  7. //初始化
  8. char another_game = 'Y';
  9. char game[3][3] = {{'1','2','3'},{'4','5','6'},{'7','8','9'}};
  10. bool win = true;
  11. int choose_player = 0;
  12. char number = '0';
  13. int row = 0 , col = 0;
  14. bool take_turns = false;
  15. char player1[20],player2[20];
  16. int c;
  17. //循环_1
  18. do{
  19. //初始化游戏
  20. printf("井字棋游戏初始化...\n");
  21. char game[3][3] = {{'1','2','3'},{'4','5','6'},{'7','8','9'}};
  22. win = false;
  23. //输入玩家1,2姓名
  24. printf("(不要使用空格,回车键结束)\n");
  25. printf("请输入玩家1姓名:\n");
  26. scanf("%s",player1);
  27. printf("请输入玩家2姓名:\n");
  28. scanf("%s",player2);
  29. //随机选择先落子玩家
  30. srand((unsigned)time(NULL));
  31. choose_player = rand()%2 1;
  32. if(choose_player == 1){
  33. take_turns = true;
  34. printf("%s(玩家1)首先落子(1~9).\n",player1);
  35. }else{
  36. take_turns = false;
  37. printf("%s(玩家2)首先落子(1~9).\n",player2);
  38. }
  39. //循环_2
  40. while(!win){
  41. //输出3x3游戏格
  42. printf("\n");
  43. printf(" %c | %c | %c \n",game[0][0],game[0][1],game[0][2]);
  44. printf("--- --- ---\n");
  45. printf(" %c | %c | %c \n",game[1][0],game[1][1],game[1][2]);
  46. printf("--- --- ---\n");
  47. printf(" %c | %c | %c \n",game[2][0],game[2][1],game[2][2]);
  48. printf("\n");
  49. //玩家1,玩家2分别下棋
  50. //落子是否有效
  51. do{
  52. printf("%s落子:",take_turns?player1:player2);
  53. //这里需要清楚缓冲区(why?)
  54. if(fEOF(stdin) || ferror(stdin)){
  55. break;
  56. }
  57. while((c = getchar()) != '\n' && c != EOF);
  58. scanf("%c",&number);
  59. //why:ASCII码表对应的数字差48~
  60. row = ((int)number-48-1)/3;
  61. col = ((int)number-48-1)%3;
  62. //printf("%d.%d.%c.\n",row,col,game[row][col]);
  63. //printf("%c\n",number);
  64. }while( game[row][col] != number);
  65. //更新棋盘
  66. if(take_turns){
  67. game[row][col] = 'X';
  68. }else{
  69. game[row][col] = 'O';
  70. }
  71. //循环_2,判定:是否有人胜利
  72. if((game[0][0] == game[0][1] && game[0][1] == game[0][2])||
  73. (game[1][0] == game[1][1] && game[1][1] == game[1][2])||
  74. (game[2][0] == game[2][1] && game[2][1] == game[2][2])||
  75. (game[0][0] == game[1][0] && game[1][0] == game[2][0])||
  76. (game[0][1] == game[1][1] && game[1][1] == game[2][1])||
  77. (game[0][2] == game[1][2] && game[1][2] == game[2][2])||
  78. (game[0][0] == game[1][1] && game[1][1] == game[2][2])||
  79. (game[0][2] == game[1][1] && game[1][1] == game[2][0])
  80. ){
  81. win = true;
  82. printf("%s胜利!游戏结束.",take_turns?player1:player2);
  83. printf("\n");
  84. printf(" %c | %c | %c \n",game[0][0],game[0][1],game[0][2]);
  85. printf("--- --- ---\n");
  86. printf(" %c | %c | %c \n",game[1][0],game[1][1],game[1][2]);
  87. printf("--- --- ---\n");
  88. printf(" %c | %c | %c \n",game[2][0],game[2][1],game[2][2]);
  89. printf("\n");
  90. }else{
  91. if(take_turns){
  92. take_turns = false;
  93. }else{
  94. take_turns = true;
  95. };
  96. }
  97. }
  98. //循环_1,是否开始新游戏
  99. if(feof(stdin) || ferror(stdin)){
  100. break;
  101. }
  102. while((c = getchar()) != '\n' && c != EOF);
  103. printf("是否开始新游戏?(y/n)");
  104. scanf("%c",&another_game);
  105. }while(toupper(another_game)=='Y');
  106. //结束
  107. return 0;
  108. }

查看全文
大家还看了
也许喜欢
更多游戏

Copyright © 2024 妖气游戏网 www.17u1u.com All Rights Reserved