同学发我的,不知道有没有病毒
#include <iostream>
#include <vector>
#include <conio.h>
const int WORLD_SIZE = 10;
enum Block {
AIR = 0,
STONE = 1,
DIRT = 2
};
class World {
public:
World() {
for (int x = 0; x < WORLD_SIZE; ++x) {
for (int y = 0; y < WORLD_SIZE; ++y) {
for (int z = 0; z < WORLD_SIZE; ++z) {
if (x == 0 || x == WORLD_SIZE - 1 || y == 0 || y == WORLD_SIZE - 1 || z == 0 || z == WORLD_SIZE - 1) {
blocks[x][y][z] = STONE;
} else {
blocks[x][y][z] = DIRT;
}
}
}
}
}
Block getBlock(int x, int y, int z) {
return blocks[x][y][z];
}
private:
std::vector<std::vector<std::vector<Block>>> blocks;
};
class Player {
public:
Player() : x(0), y(0), z(0) {}
void move(char direction) {
switch (direction) {
case 'w': --z; break;
case 's': ++z; break;
case 'a': --x; break;
case 'd': ++x; break;
default: break;
}
x = std::max(0, std::min(x, WORLD_SIZE - 1));
y = std::max(0, std::min(y, WORLD_SIZE - 1));
z = std::max(0, std::min(z, WORLD_SIZE - 1));
}
void getPosition(int& outX, int& outY, int& outZ) {
outX = x;
outY = y;
outZ = z;
}
private:
int x, y, z;
};
int main() {
World world;
Player player;
while (true) {
system("cls");
int x, y, z;
player.getPosition(x, y, z);
for (int i = -1; i <= 1; ++i) {
for (int j = -1; j <= 1; ++j) {
Block block = world.getBlock(x + i, y + j, z);
switch (block) {
case STONE: std::cout << "#"; break;
case DIRT: std::cout << "."; break;
case AIR: std::cout << " "; break;
}
}
std::cout << std::endl;
}
char direction = _getch();
player.move(direction);
}
return 0;
}