Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #define _DIR(i) ((i) >= 0 && (i) <= 2 ? \
- (i) - 21 : \
- (i) == 3 || (i) == 4 ? \
- ((i) - 3) * 2 - 1 : \
- (i) >= 5 && (i) <= 7 ? \
- (i) + 14 : \
- -1000000)
- #define DIR(x) (i + _DIR(x))
- #define D deck[d]
- #define DIM 20
- #define SQDIM 400
- int x, y, z, xinit, yinit, zinit;
- int stumble, flee, seek;
- int singlekill, doublekill, killtotal;
- int zombvictim, zombhunter, zombtotal;
- int ticks = 1000;
- void shuffle(int *a, int sz)
- {
- int i, j, r, temp;;
- for (i = 0; i < sz; ++i) {
- j = i + rand() % (sz - i);
- temp = a[i];
- a[i] = a[j];
- a[j] = temp;
- }
- }
- void sim(char *field, int *deck, int t)
- {
- stumble = flee = seek = 0;
- singlekill = doublekill = killtotal = 0;
- zombvictim = zombhunter = zombtotal = 0;
- int i, tick, d, dird, moveconv, movedir, actions;
- int adjacents[4] = {1, 3, 4, 6};
- for (tick = 0; tick < t; ++tick)
- for (i = 0; i < SQDIM; ++i) {
- shuffle(deck, 8);
- actions = 2;
- switch(field[i]) {
- case 'Z':
- for (d = 1; d < 8 && actions > 0; d += 2) {
- dird = DIR(D);
- if (dird < 0 || dird >= SQDIM)
- continue;
- switch( field[dird] ) {
- case 'V':
- field[dird] = 'Z';
- zombvictim++;
- y--;
- x++;
- zombtotal++;
- actions -= 2;
- break;
- case 'H':
- field[dird] = 'Z';
- zombhunter++;
- z--;
- x++;
- zombtotal++;
- actions -= 2;
- break;
- default:
- break;
- }
- }
- if (!actions)
- break;
- moveconv = rand() % 4;
- movedir = DIR(adjacents[moveconv]);
- while (movedir < 0 || movedir >= SQDIM) {
- moveconv = (moveconv + 1) % 4;
- movedir = DIR(adjacents[moveconv]);
- }
- if(!field[movedir]) {
- stumble++;
- field[movedir] = 'Z';
- field[i] = 0;
- }
- break;
- case 'V':
- for (d = 0; d < 8 && actions > 0; ++d) {
- dird = DIR(D);
- if (dird < 0 || dird >= SQDIM)
- continue;
- if (field[dird] == 'Z') {
- moveconv = rand() % 8;
- movedir = DIR(moveconv);
- while (movedir < 0 ||
- movedir >= SQDIM) {
- moveconv = (moveconv + 1) % 8;
- movedir = DIR(moveconv);
- }
- if (!field[movedir]) {
- flee++;
- field[movedir] = 'V';
- field[i] = 0;
- }
- actions -= 2;
- }
- }
- break;
- case 'H':
- for (d = 0; d < 8 && actions > 0; ++d) {
- dird = DIR(D);
- if (dird < 0 || dird >= SQDIM)
- continue;
- if (field[dird] == 'Z') {
- if (actions == 1) {
- singlekill--;
- doublekill++;
- } else { singlekill++; }
- field[dird] = 0;
- x--;
- killtotal++;
- actions--;
- }
- }
- if (actions < 2)
- break;
- moveconv = rand() % 8;
- movedir = DIR(moveconv);
- while (movedir < 0 ||
- movedir >= SQDIM) {
- moveconv = (moveconv + 1) % 8;
- movedir = DIR(moveconv);
- }
- if(!field[movedir]) {
- seek++;
- field[movedir] = 'H';
- field[i] = 0;
- }
- break;
- default:
- break;
- }
- }
- }
- int main(int argc, char **argv)
- {
- // x: zombies, y: victims, z: hunters
- int i, j;
- if (argc < 4) {
- printf("too few args. format: [zombies] [victims] [hunters] [[ticks]]\n");
- return 1;
- }
- x = xinit = atoi(argv[1]);
- y = yinit = atoi(argv[2]);
- z = zinit = atoi(argv[3]);
- if (argc > 4)
- ticks = atoi(argv[4]);
- if (x < 0 || y < 0 || z < 0 || x + y + z > SQDIM) {
- printf("all args must be >0, "
- "sum must be less than 401\n");
- return 2;
- }
- int *deck = malloc(SQDIM * sizeof(int));
- char *field = calloc(SQDIM, 1);
- if (!deck || !field) {
- fprintf(stderr, "Mem allocation error\n");
- return 2;
- }
- for (i = 0; i < SQDIM; ++i)
- deck[i] = i;
- srand(time(0));
- shuffle(deck, SQDIM);
- for (i = 0; i < x; ++i)
- field[deck[i]] = 'Z';
- for (i = x; i - x < y; ++i)
- field[deck[i]] = 'V';
- for (i = x + y; i - x - y < z; ++i)
- field[deck[i]] = 'H';
- for (i = 0; i < 8; ++i)
- deck[i] = i;
- printf("Initial field state:\n");
- for (i = 0; i < DIM; ++i) {
- for (j = 0; j < DIM; ++j)
- printf("%c",
- field[i*DIM + j] ? field[i*DIM + j] : '-');
- putchar('\n');
- }
- putchar ('\n');
- for (i = 0; i < 100 + rand(); ++i)
- j = rand();
- sim(field, deck, ticks);
- printf("Final field state:\n");
- for (i = 0; i < DIM; ++i) {
- for (j = 0; j < DIM; ++j)
- printf("%c",
- field[i*DIM + j] ? field[i*DIM + j] : '-');
- putchar('\n');
- }
- printf("\nSimulation results:\n\n");
- printf("Inittal population:\n"
- " Zombies: %d\n"
- " Victims: %d\n"
- " Hunters: %d\n\n",
- xinit, yinit, zinit
- );
- printf("Final population after %d ticks:\n"
- " Zombies: %d\n"
- " Victims: %d\n"
- " Hunters: %d\n\n",
- ticks, x, y, z
- );
- printf("Zombie stumbles: %d\n"
- "Victim flees: %d\n"
- "Hunter seeks: %d\n\n",
- stumble, flee, seek
- );
- printf("Total zombified: %d\n"
- "Victims zombified: %d\n"
- "Hunters zombified: %d\n"
- "Total kills: %d\n"
- "Single zombie kills: %d\n"
- "Double zombie kills: %d\n\n",
- zombtotal, zombvictim, zombhunter,
- killtotal, singlekill, doublekill
- );
- if (!(y+z))
- printf("The zombies have won!\n");
- else if (!x)
- printf("The living have won!\n");
- else if ((abs(zombtotal - killtotal) <= 5))
- printf("The zombies and the living have tied!\n");
- else if (zombtotal > killtotal > 5)
- printf("The zombies have won!\n");
- else
- printf("The living have won!\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement