[ create a new paste ] login | about

Link: http://codepad.org/pq5wkKZm    [ raw code | output | fork ]

C, pasted on Aug 7:
#include <stdlib.h>
#include <stdio.h>

#define MAP_SIZE 100
#define STOPPER 1

char map[MAP_SIZE];

int adjust_position(int p) {
  if(p > MAP_SIZE) {
    return p - MAP_SIZE;
  }
  return p;
}

int is_goal(int p) {
  if(p == MAP_SIZE) { return 1; } else { return 0; };
}

int is_stopper(int p) {
  if(map[p] == STOPPER) { return 1; } else { return 0; };
}

void make_stopper() {
  int i;
  for(i = 0; i < MAP_SIZE; i++) {
    if(STOPPER == dice()) {
      map[i] = STOPPER;
    }
  }
}

void init_rand() {
  struct timeval tv;
  gettimeofday(&tv, NULL);
  srand(tv.tv_sec);
}

int dice() {
  return (rand() % 6 + 1);
}

int main() {

  int position = 1;  // position: 1 .. 100
  int n;
  int cnt;

  init_rand();

  make_stopper();

  while(1) {
    printf("turn: %d\n", cnt++);
    n = dice();
    printf("dice: %d\n", n);

    position += n;

    if( is_goal(position) ) {
      printf("your position: %d\n", position);
      printf("\n\ngoal!\n\n");
      exit(0);
    }

    position = adjust_position( position );

    printf("your position: %d\n", position);

    if( is_stopper(position) ) {
      cnt++;
      printf("you must stop here.\n\n");
      sleep(1);
      continue;
    }

    puts("");
    sleep(1);
  }

}


Output:
1
Disallowed system call: SYS_nanosleep


Create a new paste based on this one


Comments: