Skip to content

Instantly share code, notes, and snippets.

@jaz303
Created March 28, 2011 12:17
Show Gist options
  • Select an option

  • Save jaz303/890362 to your computer and use it in GitHub Desktop.

Select an option

Save jaz303/890362 to your computer and use it in GitHub Desktop.
Snakes and Ladders!
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int squares[] = {
0, 36, 0, 10, 0, 0, 0, 0, 22, 0,
0, 0, 0, 0, 0, 0, -10, 0, 0, 0,
21, 0, 0, 0, 0, 0, 0, 56, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16, 0, 0, -20, 0, 0, 0, 0, 0, 0,
0, -43, 0, -4, 0, 0, 0, 0, 0, 0,
20, 0, 0, 0, 0, 0, 0, 0, 0, 20,
0, 0, 0, 0, 0, 0, -63, 0, 0, 0,
0, 0, -20, 0, -20, 0, 0, -19, 0, 0,
0, 0, 0, 0, 0 // five extra fo' shizzle
};
int players[4] = { 0, 0, 0, 0 };
int main(int argc, char *argv[]) {
srand(time(NULL));
while (1) {
int i;
for (i = 0; i < (sizeof(players) / sizeof(int)); i++) {
int move = ((rand() % 6) + 1);
printf("player %d rolls a %d\n", i, move);
players[i] += move;
while (squares[players[i]] != 0) {
int mod = squares[players[i]];
if (mod < 0)
printf("player %d hits a snake! back %d places\n", i, -mod);
else
printf("player %d hits a ladder! forward %d places\n", i, mod);
players[i] += mod;
}
if (players[i] >= 99) {
printf("player %d is the winner\n", i);
exit(0);
}
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment