[2015-2-27] Challenge #203 [Hard] Minecraft: There and Back

[spoiler](/s"#include <stdio.h> /* printf, scanf, puts, NULL */

include <stdlib.h> /* srand, rand */

include <time.h> /* time */

define maxBlockID 5

define maxWorldLength 10

define maxWorldWidth 10

typedef enum { PLAYER = 0, AIR = 1, DIRT = 2, SAND = 3, LAVA = 4, DIAMOND = 5 } blockID_t; //Keep synced with maxBlockID

void worldBlockPlace(blockID_t* worldBlocks[maxWorldLength][maxWorldWidth], int x, int y, int block) { worldBlocks[y][x] = block; //printf("BLOCK PLACE: (X,Y) %i, %i BLOCK ID: %i\n", x, y, block); //Debug }

void worldGenerate(blockID_t* worldBlocks[maxWorldLength][maxWorldWidth]) { int i, ii; srand (time(NULL)); for(i = 0; i < maxWorldLength; i++) { //Start the bottom to the top for(ii = 0; ii < maxWorldWidth; ii++) { //Start placing blocks left to right

        worldBlockPlace(worldBlocks,ii,i,rand() % maxBlockID + 1); //Generates random number according to maxBlockID + 1
    }
}

}

void worldDisplay(blockID_t* worldBlocks[maxWorldLength][maxWorldWidth]) { int i, ii; for(i = 0; i < maxWorldLength; i++) { printf("%i: ", i); for(ii = 0; ii < maxWorldWidth; ii++) { switch((int)worldBlocks[i][ii]) { case PLAYER: printf("\x1b[31m@\x1b[0m"); break; case AIR: printf("\x1b[32mA\x1b[0m"); break; case DIRT: printf("\x1b[32mD\x1b[0m"); break; case SAND: printf("\x1b[33mS\x1b[0m"); break; case LAVA: printf("\x1b[31mL\x1b[0m"); break; case DIAMOND: printf("\x1b[36mX\x1b[0m"); break; } } printf("\n"); } }

void worldGravity(blockID_t* worldBlocks[maxWorldLength][maxWorldWidth]) { int i, ii, iii; for(i = 0; i < maxWorldLength; i++) { for(ii = 0; ii < maxWorldWidth; ii++) { switch((int)worldBlocks[i][ii]) { case SAND: for(iii = 1; i < maxWorldLength-i; iii++) { if(worldBlocks[i+iii][ii] == AIR || worldBlocks[i+iii][ii] == LAVA) { worldBlockPlace(worldBlocks, ii, i, AIR); worldBlockPlace(worldBlocks, ii, i+iii, SAND); printf("GRAVITY(X,Y): %i,%i(SAND) moved to %i,%i\n", ii, i, ii,i+1); } else { break; //Break when the block below isn't AIR } } break; case LAVA: for(iii = 1; i < maxWorldLength-i; iii++) { if(worldBlocks[i+iii][ii] == AIR) { worldBlockPlace(worldBlocks, ii, i+iii, LAVA); printf("GRAVITY(X,Y): %i,%i(LAVA) spread to %i,%i\n", ii, i, ii,i+1); } else { break; //Break when the block below isn't AIR } } break; case PLAYER: printf("To do!\n"); break; } } } }

int main(void) { blockID_t* worldBlocks[maxWorldLength][maxWorldWidth];

printf("First generation before gravity\n");
worldGenerate(worldBlocks);
worldDisplay(worldBlocks);

printf("\nFirst generation after one pass of gravity\n");
worldGravity(worldBlocks);
worldDisplay(worldBlocks);

}")

/r/dailyprogrammer Thread