Chinaunix首页 | 论坛 | 博客
  • 博客访问: 268865
  • 博文数量: 84
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 927
  • 用 户 组: 普通用户
  • 注册时间: 2015-03-06 23:00
个人简介

growing

文章分类

全部博文(84)

文章存档

2017年(6)

2016年(61)

2015年(17)

我的朋友

分类: C/C++

2015-10-26 11:42:34


点击(此处)折叠或打开

  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #define NAME_SIZE 50
  6. #define PASSWORD_SIZE 20
  7. #define NAME "zhangsan"
  8. #define PASSWORD "123456"
  9. //#define CONTINUE(X) {printf("&s",X);getchar();}

  10. //player 结构体
  11. typedef struct player{
  12. char name[NAME_SIZE];
  13. char password[PASSWORD_SIZE];
  14. int total_num;
  15. int victory;
  16. }player_t;

  17. player_t *player;

  18. player_t * creat_player(void)
  19. {
  20. player = (player_t *)malloc(sizeof(player_t)* 1);
  21. if (NULL == player)
  22. {
  23. return NULL;
  24. }
  25. memset(player, 0, sizeof(player_t));
  26. strcpy(player->name, NAME);
  27. strcpy(player->password, PASSWORD);
  28. player->total_num = 0;
  29. player->victory = 0;
  30. return player;
  31. }

  32. int my_rand()
  33. {
  34. int computer_choice = 0;
  35. srand(time(NULL));
  36. computer_choice = (rand() % 3 + 1);
  37. return computer_choice;
  38. }
  39. destroy_player()
  40. {
  41. if (NULL != player)
  42. {
  43. free(player);
  44. }
  45. player = NULL;
  46. }
  47. void load()
  48. {
  49. int i = 0;
  50. for (i = 2; i > 0; i--)
  51. {
  52. system("cls");
  53. printf("电脑出拳中:%d\n", i);
  54. fflush(stdout);
  55. Sleep(1000);
  56. }
  57. printf("\n");
  58. }

  59. void menue()
  60. {
  61. //    system("cls");
  62. printf("\n\n************猜拳游戏*************\n");
  63. printf("1:石头 2:剪刀 3:步 0:退出\n");
  64. printf("请您出拳: ");
  65. }

  66. void out_print(int player_choice, int computer_choice)
  67. {
  68. if (player_choice == 1 && computer_choice == 1)
  69. printf("您选择石头\n电脑也选择石头\n");
  70. else if (player_choice == 1 && computer_choice == 2)
  71. printf("您选择石头\n电脑选择剪刀\n");
  72. else if (player_choice == 1 && computer_choice == 3)
  73. printf("您选择石头\n电脑选择布\n");
  74. else if (player_choice == 2 && computer_choice == 1)
  75. printf("您选择剪刀\n电脑选择石头\n");
  76. else if (player_choice == 2 && computer_choice == 2)
  77. printf("您选择剪刀\n电脑也选择剪刀\n");
  78. else if (player_choice == 2 && computer_choice == 3)
  79. printf("您选择剪刀\n电脑选择布\n");
  80. else if (player_choice == 3 && computer_choice == 1)
  81. printf("您选择布\n电脑选择石头\n");
  82. else if (player_choice == 3 && computer_choice == 2)
  83. printf("您选择石头\n电脑选择剪刀\n");
  84. else if (player_choice == 3 && computer_choice == 3)
  85. printf("您选择石头\n电脑也选择布\n");
  86. }
  87. void menue_ctr()
  88. {
  89. int win = 0;
  90. int computer_choice = 0;
  91. int player_choice = 0;
  92. while (1)
  93. {
  94. menue();
  95. do
  96. {
  97. //    menue();
  98. scanf("%d", &player_choice);
  99. } while (player_choice > 3 || player_choice < 0);
  100. if (player_choice == 0)
  101. {
  102. return;
  103. }
  104. load();
  105. computer_choice = my_rand();
  106. win = player_choice - computer_choice;
  107. (player->total_num)++;
  108. //load();
  109. out_print(player_choice, computer_choice);
  110. switch (win)
  111. {
  112. case -1:
  113. case 2:
  114. {
  115.   printf("恭喜您胜利!\n");
  116.   (player->victory)++;
  117.   break;
  118. }
  119. case 0:
  120. {
  121.   printf("和局\n");
  122.   break;
  123. }
  124. default:
  125. {
  126.    printf("再接再厉,您输了!");
  127. }
  128. }
  129. }
  130. }


  131. void victory_display(int total,int victory)
  132. {

  133. double win = 0.0;
  134. win = (double)victory / (double)total;
  135. printf("\n\n\t\t 排行榜\n");
  136. printf("\t\t****************\n\n");
  137. printf("%10s %10s %10s %10s\n", "姓名", "总局数", "胜场", "胜率");
  138. printf("%10s %10d %10d %10f\n", player->name, player->total_num, player->victory,win);
  139. }

  140. int main()
  141. {
  142. player = creat_player();
  143. if (NULL == player)
  144. {
  145. return 1;
  146. }
  147. menue_ctr();
  148. victory_display(player->total_num,player->victory);
  149. system("pause");
  150. return 0;
  151. }

阅读(1158) | 评论(0) | 转发(0) |
0

上一篇:指针(上课代码)

下一篇:高级vim配置

给主人留下些什么吧!~~