C language implementation of sandbags games (or murder games), sandbags and murder games

Source: Internet
Author: User

C language implementation of sandbags games (or murder games), sandbags and murder games

Sandbags games (or murder games) are implemented in C language:

 

Game Overview:

A killer game (or a sandbag game) sets some people (num) to play the game together and throws sandbags in turn from a specified person (set to start, the next person who throws a sandbag is 1. If a person hits a person at a fixed number (set to "step"), the person is killed and quit the game. After the last person is reached, the person is re-taken to start counting, take turns until there is only one person left. The game is over!

Game Code:

1 /************************************** **************************************** * ***** 2 Brief description: 3. In a murder game (or sandbags games), some people (num) play the game together. From a specified person (start), 4 throws sandbags in turn, and a fixed number of people (step) hit a person, the person was killed and quit the game, and then continued until the last five people left, the game ended! 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) // print the current murder condition 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 ch Eck_rest (int * people, int num) // check the remaining 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) // initialization task location no. 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) // skip 49 {50 p ++; 51} 52 return p after the person is killed; 53} 54 55 int check_num (int num, int step, int start) // check the validity of game parameters 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); // creates 97 if (people = NULL) times the memory space. // The Memory creation fails and the 98 return 0 is returned; 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); // initialize the number of players in the game as the position number 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) // count the remaining persons one by one: 114 I ++; 115 p ++; 116 p = (p> 2 * num )? (P-2 * num): p; // if the position pointer exceeds the cache, adjust 117 p = (p> num )? (P-num): p; 118 p = jump_killed (people, p); // skip the 119} 120 121 stemp = p-1 after a person is killed; // match the human ID with the memory location 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); // check the remaining number of people 129} 130 131 for (I = 0; I <num; I ++) // when the game ends, find the remaining person 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}

 

Code Description:

Defines two times the number of people in the memory space as the cache. Each person numbers according to their own location, the location where the person is killed, and the number is set to zero, indicating that the person has been killed, all numbers are not equal to zero, indicating the number of people who have not been eliminated. The number of people who have not been eliminated is counted in each round, and the game proceeds in sequence to guide the remaining person.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.