Hola gente del foro!! Comparto un programita q hice q simula, como dice el titulo, la famosa carrera entre la tortuga y la liebre.. lo q represento en los vectores correspondientes a cada animal, son las acciones de los mismos en la carrera, q pueden ser avanzar, retroceder o quedarse dormido segun el num aleatorio q salga.. cuando ambos competidores compartan el mismo cuadro la tortuga mordera a la liebre.. y ovbiamente ganara el q 1ro llegue a la meta..
Bueno espero comentarios y sugerencias!!
Saludos!!
P.D: el codigo lo hice en linux por lo tanto el system("clear") tira error si no se ejecuta en ese OS
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define SIZE 71
int printRace(int [][SIZE], int, int, int);
main()
{
srand(time(NULL));
int row, column, raceTrack[3][SIZE];
int tortoise = 1, hare = 1, finish;
printf("BANG!!%63s%ds\nAND THEY 'RE OFF!!\n\n", "Time: ", 0);
finish = printRace(raceTrack, tortoise, hare, SIZE);
if (finish == 1)
printf("\nTORTOISE WINS\n");
else if (finish == 2)
printf("\nHare wins\n");
else
printf("\nIt's a tie\n");
return 0;
}
int printRace(int rt[][SIZE], int t, int h, int size)
{
void wait(int);
int tortoiseArray[10] = {3, 3, 3, 3, 3, -6, -6, 1, 1, 1};
int hareArray[10] = {0, 0, 9, 9, -12, 1, 1, 1, -2, -2};
int timer = 1, row, column;
while (t < size - 1 && h < size - 1){
for (row = 1; row < 3; row++){
for (column = 1; column < size; column++){
if (t != h || t == 1){
if (row == 1 && column == t)
printf("%c", 'T');
else if (row == 2 && column == h)
printf("%c", 'H');
else
printf("_");
}
else{
if (row == 1 && column == t)
printf("%c", 'T');
else if (row == 2 && column == h)
printf("%s", "OUCH!!!");
else
printf("_");
}
}
printf("\n");
}
t += tortoiseArray[rand() % 10];
h += hareArray[rand() % 10];
if (t < 1)
t = 1;
if (h < 1)
h = 1;
if (t > size - 1)
t = size - 1;
if (h > size - 1)
h = size - 1;
wait(1);
printf("%69s%ds","Time: ", timer++);
system("clear");
printf("\n\n\n");
}
for (row = 1; row < 3; row++){
for (column = 1; column < size; column++){
if (row == 1 && column == t)
printf("%c", 'T');
else if (row == 2 && column == h)
printf("%c", 'H');
else
printf("_");
}
printf("\n");
}
if (t == size - 1 && h == size -1)
return 0;
else if (t == size -1)
return 1;
else
return 2;
}
void wait(int seconds)
{
clock_t endwait;
endwait = clock() + seconds * CLOCKS_PER_SEC;
while (clock() < endwait);
}
Ya lo prove se ve demasiado divertido jeje... felicidades