丢沙包游戏(或杀人游戏)的C语言实现

丢沙包游戏(或杀人游戏)用C语言实现:

游戏简述:

  杀人游戏(或者丢沙包游戏),设定一些人(人数为:num)一起玩游戏,从某个指定的人(设定为:start)开始轮流扔沙包,扔沙包人的下一个人为1,每隔固定人数(设定为:step)砸中一个人,则该人被杀退出游戏,到最后一人后重新接第一个人开始计数,依次轮流进行,直到最后只剩下一个人,游戏结束!

游戏代码:

  1 /***********************************************************************************
  2 简述:
  3     杀人游戏(或者丢沙包游戏),一些人(num)一起玩游戏,从某个指定的人(start)开始
  4     轮流扔沙包,每隔固定人数(step)砸中一个人,该人被杀退出游戏,依次进行,直到最后
  5     只剩下一个人,游戏结束!
  6 Date:
  7     2015.12.05
  8 ***********************************************************************************/
  9 
 10 #include <stdio.h>
 11 #include <stdlib.h>
 12 
 13 void show_people(int *people, int num)                    //打印当前的杀人状况
 14 {
 15     int i;
 16     printf("People alive: ");
 17     for (i = 0; i < num; i++)
 18     {
 19         if (i % 9 == 0)
 20             putchar('
');
 21         printf("%d	", *(people + i));
 22     }
 23     printf("
");
 24 }
 25 
 26 int check_rest(int *people, int num)                    //检查剩余人数
 27 {
 28     int i, count = 0;
 29     for (i = 0; i < num; i++)
 30     {
 31         if (*(people + i) != 0)
 32             count++;
 33     }
 34     return count;
 35 }
 36 
 37 void init_people(int *people, int num)                    //初始化任务位置编号
 38 {
 39     int i;
 40     for (i = 0; i < 2 * num; i++)
 41     {
 42         *(people + i) = (i < num) ? (i + 1) : (i + 1 - num);
 43     }
 44 }
 45 
 46 int jump_killed(int *people, int p)
 47 {
 48     while (*( people + p - 1 ) == 0)                    //遇到人被杀掉的位置后跳过
 49     {
 50         p++;
 51     }
 52     return p;
 53 }
 54 
 55 int check_num(int num, int step, int start)                //检查游戏参数的有效性
 56 {
 57     if ( ( num <= 0 ) || ( step <= 0 ) || ( start <= 0 ) )
 58     {
 59         printf("Sorry! Maybe number error!
");
 60         printf("Press any key to exit!");
 61         return 1;
 62     }
 63     if ( ( 2*num <= step ) || ( start > num ) )
 64     {
 65         printf("Sorry! People number is small than step! Game over!
");
 66         printf("Press any key to exit!");
 67         return 1;
 68     }
 69 
 70     return 0;
 71 }
 72 
 73 int main (void)
 74 {
 75     int i, num = 0, step = 0, start = 0;
 76     int *people = NULL;
 77     int rest = 0, round = 0;
 78     int p = 0;
 79 
 80     printf("
Hi! This is a game, that throw bag to kill people!
");
 81     printf("Now, let's begin the game!
");
 82     printf("First, enter the number of people in the game(>0): ");
 83     scanf("%d", &num);
 84     printf("Second, enter the step to pull people out(>0&&<num): ");
 85     scanf("%d", &step);
 86     printf("Third, enter one people to start the game(>0&&<num): ");
 87     scanf("%d", &start);
 88 
 89     if (check_num(num, step, start))
 90     {
 91         getchar();
 92         getchar();
 93         return 0;
 94     }
 95 
 96     people =(int *) malloc( 2 * num * sizeof(int) );//创建两倍人数的内存空间    
 97     if (people == NULL)                                //创建内存失败,退出游戏
 98         return 0;
 99 
100     printf("
Ok! We have %d people in this game, and we will kill people "
101         "from N0:%d people every %d people like this: 
",num, start, step);
102 
103     init_people(people, num);                        //将游戏中人编号初始化为位置序号 
104     p = start;
105     rest = check_rest(people, num);
106 
107     while ( rest > 1 )
108     {
109         int i = 0, j = 0, stemp = 0;
110 
111         while ( i < step )
112         {            
113             if (*( people + p - 1 ) != 0)            //余下的人中逐个计数
114                 i++;
115             p++;
116             p = (p > 2 * num) ? (p - 2 * num) : p;    //位置指针超出缓存,调整
117             p = (p > num) ? (p - num) : p;
118             p = jump_killed(people, p);                //遇到人被杀掉的位置后跳过
119         }
120 
121         stemp = p - 1;                                //将人的编号和内存位置匹配 
122         round++;
123         *(people + stemp) = 0;                        //People was killed!
124         stemp = p > num ? ( stemp - num ) : ( stemp + num );
125         *(people + stemp) = 0;
126         p = p > num ? ( p - num ) : p;
127         printf("Round %d: No.%d was killed! 	%d people leave!
",round, p, rest-1);
128         rest = check_rest(people, num);                //清点剩余人数
129     }
130 
131     for ( i = 0; i < num; i++)                        //游戏结束,找到剩余的最后一个人
132     {
133         if (*( people + i ) != 0)
134             break;
135     }
136     printf("Game over! No.%d people alive!
", i+1);
137 
138     free(people);
139     getchar();
140     getchar();
141     return 0;
142 }

代码简述:

  定义两倍于人数的内存空间作为缓存,每个人按照自己所处的位置进行编号,被杀掉的位置,编号置为零,表示该人已被杀,所有编号不等于零的位置,代表没有被淘汰的人,每一轮清点剩下未被淘汰的人数,游戏依次进行,指导剩下一个人为止。

原文地址:https://www.cnblogs.com/microxiami/p/5032583.html