So our contenders begin the race at "square 1" of 70 square.Each square represents a possible position along the race course.The finish line is at square 70.The course weaves its way up the side of a slippery mountain, so occasionally the contenders lose ground.There is a clock that ticks once per second.With each tick of the clock, this program will adjust the position of the animals according to the following rules:
Animal | Move type | Percentage of the time | Actual move |
---|---|---|---|
Tortoise | Fast plod | 50% | 3 squares to the right |
Slip | 20% | 6 squares to the left | |
Slow pod | 30% | 1 squares to the right | |
Hare | Sleep | 20% | No move at all |
Big hop | 20% | 9 squares to the right | |
Big slip | 10% | 12 squares to the left | |
Small hop | 30% | 1 square to the right | |
Small slip | 20% | 2 squares to the left |
/* * Title: Pointers 717 : Simulasi Kura-kura dan Arnab * Author: aeriqusyairi * Date: 29Feb 2012 */ #include<stdio.h> #include<stdlib.h> #include<time.h> #define FINISH 70 void moveTortoise( int* tortoisePtr ); void moveHare( int* harePtr ); void printPosition( const int* const tortoisePtr, const int* const harePtr ); int main( void ){ int tortoise = 1, hare = 1, timer = 0; srand( time( 0 ) ); //interface printf("Tortoise and Hare\n*****************\n\n"); printf("BANG !!!!!\nAND THEY'RE OFF !!!!\n\n"); //move untill either contender finish the race while(tortoise != FINISH && hare != FINISH){ //move the tortoise moveTortoise( &tortoise ); //move the hare moveHare( &hare ); //print current position printPosition( &tortoise, &hare ); //calculate the timer timer++; } //the result if(tortoise > hare){ printf("TORTOISE WIN !!! YAY !!!\n"); }else if(tortoise < hare){ printf("HARE WIN !!! YUCH !!!\n"); } printf("Time Elapsed: %d seconds\n", timer); system("pause"); return 0; } void moveTortoise( int* tortoisePtr ){ int i; i = rand() % 10; if(i >= 1 && i <= 5){ //1 - 5 : Fast plod *tortoisePtr += 3; //move 3 square to the right }else if(i >= 6 && i <= 7){ //6 - 7 : Slip *tortoisePtr -= 6; //move 6 square to the left }else{ //8 - 10 : Slow plod *tortoisePtr += 1; //move 1 square to the right } if(*tortoisePtr < 1) *tortoisePtr = 1; else if(*tortoisePtr > FINISH) *tortoisePtr = FINISH; } void moveHare( int* harePtr ){ int i; i = rand() % 10; if(i >= 1 && i <= 2){ //1 - 2 : Sleep *harePtr = *harePtr; }else if(i >= 3 && i <= 4){ //3 - 4 : Big hop *harePtr += 9; }else if(i == 5){ //5 : Big slip *harePtr -= 12; }else if(i >= 6 && i<= 8){ //6 - 8 : Small hop *harePtr += 1; }else{ //9 - 10 : Small slip *harePtr -= 2; } if(*harePtr < 1) *harePtr = 1; else if(*harePtr > FINISH) *harePtr = FINISH; } void printPosition( const int* const tortoisePtr, const int* const harePtr ){ int i; if(*tortoisePtr == *harePtr){ for(i = 1; i < *tortoisePtr; i++){ printf("%s", " " ); } printf("OUCH!!!"); }else if(*tortoisePtr < *harePtr){ for(i = 1; i < *tortoisePtr; i++ ){ printf("%s", " " ); } printf("T"); for(i = 1; i < (*harePtr - *tortoisePtr); i++){ printf("%s", " " ); } printf("H"); }else{ for(i = 1; i < *harePtr; i++){ printf("%s", " "); } printf("H"); for( i = 0; i < (*tortoisePtr - *harePtr); i++){ printf("%s", " " ); } printf("T"); } printf("\n"); }
Dude thanks so much... very clear on pointers
ReplyDelete