#include <stdio.h>
#include <stdlib.h>
#define WORLD_WIDTH 40
#define WORLD_HEIGHT 20
float earth_height[WORLD_WIDTH];
float object_height[WORLD_WIDTH];
int steps;
void display(void)
{
for (int y=WORLD_HEIGHT; y>=0; y--)
{
printf("#");
for (int x=0; x<WORLD_WIDTH; x++)
if (y == earth_height[x])
printf("#");
else if ((int)object_height[x] == y)
printf("*");
else
printf(" ");
printf("\n");
}
printf("\n");
}
void fall_gravity(void)
{
for (int i=0; i<WORLD_WIDTH; i++)
object_height[i] -= 1;
}
void fall_flat(void)
{
for (int i=0; i<WORLD_WIDTH; i++)
earth_height[i] += 1;
}
// Simple detection if an object and the ground have collided.
void detect_collisions(void)
{
for (int i=0; i<WORLD_WIDTH; i++)
{
if (object_height[i] == earth_height[i])
object_height[i] += 1;
}
}
void main(void)
{
for (int m=0; m<2; m++)
{
srand(123); // Same seed for every run.
steps = 0;
for (int i=0; i<WORLD_WIDTH; i++)
object_height[i] = rand()%WORLD_HEIGHT+1;
for (int i=0; i<WORLD_WIDTH; i++)
earth_height[i] = i/13;
for (int i=1; i<WORLD_HEIGHT; i++)
{
steps++;
if (m == 0)
fall_gravity();
else
fall_flat();
detect_collisions();
printf("Method %d, Step: %d\n", m, steps);
display();
}
}
}