fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4.  
  5. // 1. Define the types of blocks available
  6. enum BlockType { AIR, DIRT, GRASS, STONE };
  7.  
  8. struct Block {
  9. BlockType type;
  10.  
  11. // Minecraft uses "metadata" to store extra info (like orientation)
  12. std::string getBlockName() {
  13. switch(type) {
  14. case GRASS: return "Grass Block";
  15. case STONE: return "Stone Block";
  16. case DIRT: return "Dirt Block";
  17. default: return "Air";
  18. }
  19. }
  20. };
  21.  
  22. // 2. The Chunk - A 16x16x16 area (simplified for this example)
  23. class Chunk {
  24. public:
  25. static const int SIZE = 16;
  26. Block blocks[SIZE][SIZE][SIZE];
  27.  
  28. void generateTerrain() {
  29. for (int x = 0; x < SIZE; x++) {
  30. for (int z = 0; z < SIZE; z++) {
  31. // Simple flat world generation
  32. blocks[x][0][z] = {STONE};
  33. blocks[x][1][z] = {DIRT};
  34. blocks[x][2][z] = {GRASS};
  35. // Everything else remains AIR by default
  36. }
  37. }
  38. }
  39. };
  40.  
  41. // 3. The World - Manages multiple chunks
  42. class World {
  43. public:
  44. std::vector<Chunk> loadedChunks;
  45.  
  46. void addChunk() {
  47. Chunk newChunk;
  48. newChunk.generateTerrain();
  49. loadedChunks.push_back(newChunk);
  50. }
  51.  
  52. void getBlockAt(int x, int y, int z) {
  53. // In a real game, you'd calculate which chunk (x,y,z) is in
  54. std::cout << "Block at " << x << "," << y << "," << z << " is: "
  55. << loadedChunks[0].blocks[x][y][z].getBlockName() << std::endl;
  56. }
  57. };
  58.  
  59. int main() {
  60. World myWorld;
  61. myWorld.addChunk();
  62.  
  63. // Check what is at the surface (y=2)
  64. myWorld.getBlockAt(5, 2, 5);
  65.  
  66. return 0;
  67. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Block at 5,2,5 is: Grass Block