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

时间:2024-01-10 21:22:26

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

游戏简述:

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

游戏代码:

 /***********************************************************************************
简述:
杀人游戏(或者丢沙包游戏),一些人(num)一起玩游戏,从某个指定的人(start)开始
轮流扔沙包,每隔固定人数(step)砸中一个人,该人被杀退出游戏,依次进行,直到最后
只剩下一个人,游戏结束!
Date:
2015.12.05
***********************************************************************************/ #include <stdio.h>
#include <stdlib.h> void show_people(int *people, int num) //打印当前的杀人状况
{
int i;
printf("People alive: ");
for (i = ; i < num; i++)
{
if (i % == )
putchar('\n');
printf("%d\t", *(people + i));
}
printf("\n");
} int check_rest(int *people, int num) //检查剩余人数
{
int i, count = ;
for (i = ; i < num; i++)
{
if (*(people + i) != )
count++;
}
return count;
} void init_people(int *people, int num) //初始化任务位置编号
{
int i;
for (i = ; i < * num; i++)
{
*(people + i) = (i < num) ? (i + ) : (i + - num);
}
} int jump_killed(int *people, int p)
{
while (*( people + p - ) == ) //遇到人被杀掉的位置后跳过
{
p++;
}
return p;
} int check_num(int num, int step, int start) //检查游戏参数的有效性
{
if ( ( num <= ) || ( step <= ) || ( start <= ) )
{
printf("Sorry! Maybe number error!\n");
printf("Press any key to exit!");
return ;
}
if ( ( *num <= step ) || ( start > num ) )
{
printf("Sorry! People number is small than step! Game over!\n");
printf("Press any key to exit!");
return ;
} return ;
} int main (void)
{
int i, num = , step = , start = ;
int *people = NULL;
int rest = , round = ;
int p = ; printf("\nHi! This is a game, that throw bag to kill people!\n");
printf("Now, let's begin the game!\n");
printf("First, enter the number of people in the game(>0): ");
scanf("%d", &num);
printf("Second, enter the step to pull people out(>0&&<num): ");
scanf("%d", &step);
printf("Third, enter one people to start the game(>0&&<num): ");
scanf("%d", &start); if (check_num(num, step, start))
{
getchar();
getchar();
return ;
} people =(int *) malloc( * num * sizeof(int) );//创建两倍人数的内存空间
if (people == NULL) //创建内存失败,退出游戏
return ; printf("\nOk! We have %d people in this game, and we will kill people "\
"from N0:%d people every %d people like this: \n",num, start, step); init_people(people, num); //将游戏中人编号初始化为位置序号
p = start;
rest = check_rest(people, num); while ( rest > )
{
int i = , j = , stemp = ; while ( i < step )
{
if (*( people + p - ) != ) //余下的人中逐个计数
i++;
p++;
p = (p > * num) ? (p - * num) : p; //位置指针超出缓存,调整
p = (p > num) ? (p - num) : p;
p = jump_killed(people, p); //遇到人被杀掉的位置后跳过
} stemp = p - ; //将人的编号和内存位置匹配
round++;
*(people + stemp) = ; //People was killed!
stemp = p > num ? ( stemp - num ) : ( stemp + num );
*(people + stemp) = ;
p = p > num ? ( p - num ) : p;
printf("Round %d: No.%d was killed! \t%d people leave!\n",round, p, rest-);
rest = check_rest(people, num); //清点剩余人数
} for ( i = ; i < num; i++) //游戏结束,找到剩余的最后一个人
{
if (*( people + i ) != )
break;
}
printf("Game over! No.%d people alive!\n", i+); free(people);
getchar();
getchar();
return ;
}

代码简述:

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