丟沙包游戲(或殺人游戲)用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('\n');
21 printf("%d\t", *(people + i));
22 }
23 printf("\n");
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!\n");
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!\n");
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("\nHi! This is a game, that throw bag to kill people!\n");
81 printf("Now, let's begin the game!\n");
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("\nOk! We have %d people in this game, and we will kill people "\
101 "from N0:%d people every %d people like this: \n",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! \t%d people leave!\n",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!\n", i+1);
137
138 free(people);
139 getchar();
140 getchar();
141 return 0;
142 }
代碼簡述:
定義兩倍於人數的內存空間作為緩存,每個人按照自己所處的位置進行編號,被殺掉的位置,編號置為零,表示該人已被殺,所有編號不等於零的位置,代表沒有被淘汰的人,每一輪清點剩下未被淘汰的人數,游戲依次進行,指導剩下一個人為止。