training

Code I wrote during training
git clone git://git.bitsmanent.org/training
Log | Files | Refs | README

sim_TL.c (2263B)


      1 /* Exercise 7.17 */
      2 
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 #include <time.h>
      6 
      7 #include <unistd.h>
      8 
      9 #define LEN 70
     10 
     11 void move(int who[]);
     12 void show(int who[]);
     13 
     14 int main(void)
     15 {
     16    int who[2] = { 0, 0 };
     17 
     18    srand( time(NULL) );
     19 
     20    /* Start */
     21    printf("BANG !!!!!\nAND THEY'RE OF !!!!!");
     22 
     23    while(who[0] <= LEN && who[1] <= LEN) {
     24       printf("\n\n");
     25 
     26       move(who); /* move the players */
     27       show(who); /* show current position */
     28 
     29       sleep(1);
     30    }
     31    printf("\n\n");
     32 
     33    /* check the winner */
     34    if(who[0] > LEN && who[1] <= LEN) {
     35       printf("TORTOISE WINS!!! YAY!!!\n");
     36    }
     37    else if(who[1] > LEN && who[0] <= LEN) {
     38       printf("Hare wins. Yuch.\n");
     39    }
     40    else { /* There is no winner! */
     41       printf("who[0] = %d && who[1] = %d\n", who[0], who[1]);
     42       if( rand() % 2 ) {
     43          printf("It's a tie!\n");
     44       }
     45       else {
     46 	 printf("I want to win the turtle!! :-)\n");
     47       }
     48    }
     49 
     50    return 0;
     51 } /* E0F main */
     52 
     53 /* Move the players */
     54 void move(int who[])
     55 {
     56    int i = 1 + rand() % 10;
     57 
     58    /* Move the turtle (who[0]) */
     59    if(i <= 5) /* 50% limped fast */
     60       who[0] += 3;
     61 
     62    else if(i >= 6 && i <= 7)/* 20% slide */
     63       who[0] -= 6;
     64 
     65    else /* i >=8: 30% limpied slow */
     66       ++who[0];
     67 
     68    /* Check if the turtle is out of path */
     69    if(who[0] < 0)
     70       who[0] = 0;
     71 
     72 
     73    /* Move the hare (who[1]) */
     74    if(i <= 2) /* 20% (sleep) */
     75       ; /* stay in the same position */
     76 
     77    else if(i >= 3 && i <= 4) /* 20% long jump */
     78       who[1] += 9;
     79 
     80    else if(i == 5) /* 10% Long slide */
     81       who[1] -= 12;
     82 
     83    else if(i >= 6 && i <= 8) /* 30% short jump */
     84       ++who[1]; 
     85 
     86    else /* i >= 9: 20% short slide */
     87       who[1] -= 2;
     88 
     89    /* Check if the hare is out of path */
     90    if(who[1] < 0)
     91       who[1] = 0;
     92 
     93 
     94 } /* eof move() */
     95 
     96 /* Show the situation ;) */
     97 void show(int who[])
     98 {
     99    int i;
    100 
    101    /* Show the turtle position */
    102    for(i = 0; i < LEN; i++) {
    103 
    104       /* If are both here */
    105       if(i == who[0] && i == who[1]) {
    106 	 printf("OUCH!!!");
    107 	 i += 6; /* Align the path */ 
    108       }
    109 
    110       /* If is the turtle */
    111       else if(i == who[0]) 
    112 	 printf("T");
    113       
    114       /* If is the hare */
    115       else if(i == who[1])
    116 	 printf("L");
    117 
    118       /* If is empty */
    119       else
    120 	 printf(".");
    121 
    122    }
    123 
    124 } /* eof show() */
    125