// world.cpp -- 太阳系模拟
// 2020/3/2 copyright by ywr
#include <iostream>
#include <Windows.h>
using namespace std;
#define FARS 21
#define MAX_PLANETS 9
// 各种物质标识
enum matter {vacuo, sun, planet};
// 行星的坐标
struct Coord {
int x;
int y;
};
// 太阳系
struct World {
Coord planet[MAX_PLANETS];
char world_map[FARS][FARS+1];
};
// 太阳坐标
const int centre_x = (FARS - 1) / 2;
const int centre_y = (FARS - 1) / 2;
void init_world(World *world);
void show_world(World *world);
void update_world(World *world);
bool judge_world(World *world);
// 初始化世界
void init_world(World *world) {
int i;
int planets_x = centre_x;
int planets_y = centre_y;
// 初始化
memset(world->world_map, 0, sizeof(world->world_map));
// 初始化太阳
world->world_map[planets_x][planets_y--] = 1;
// 初始化行星坐标
for (i = 0; i < MAX_PLANETS; i++, planets_y--) {
world->planet[i].x = planets_x;
world->planet[i].y = planets_y;
}
// 初始化行星
for (i = 0; i < MAX_PLANETS; i++) {
world->world_map[world->planet[i].x][world->planet[i].y] = 2;
}
}
// 显示世界
void show_world(World *world) {
for (int i = 0; i < FARS; i++) {
for (int j = 0; j < FARS; j++) {
switch (world->world_map[i][j]) {
case vacuo: cout << " "; break;
case sun: cout << "⊙"; break;
case planet: cout << "●"; break;
}
}
cout << endl;
}
}
// 更新世界
void update_world(World *world) {
for (int i = 0; i < MAX_PLANETS; i++) {
if (world->planet[i].x == centre_x &&
world->planet[i].y < centre_y) {
world->world_map[world->planet[i].x++][world->planet[i].y++] = 0;
world->world_map[world->planet[i].x][world->planet[i].y] = 2;
}
else if (world->planet[i].x == centre_x &&
world->planet[i].y > centre_y) {
world->world_map[world->planet[i].x--][world->planet[i].y--] = 0;
world->world_map[world->planet[i].x][world->planet[i].y] = 2;
}
else if (world->planet[i].x < centre_x &&
world->planet[i].y == centre_y) {
world->world_map[world->planet[i].x++][world->planet[i].y--] = 0;
world->world_map[world->planet[i].x][world->planet[i].y] = 2;
}
else if (world->planet[i].x > centre_x &&
world->planet[i].y == centre_y) {
world->world_map[world->planet[i].x--][world->planet[i].y++] = 0;
world->world_map[world->planet[i].x][world->planet[i].y] = 2;
}
else if (world->planet[i].x < centre_x &&
world->planet[i].y < centre_y) {
world->world_map[world->planet[i].x++][world->planet[i].y--] = 0;
world->world_map[world->planet[i].x][world->planet[i].y] = 2;
}
else if (world->planet[i].x > centre_x &&
world->planet[i].y < centre_y) {
world->world_map[world->planet[i].x++][world->planet[i].y++] = 0;
world->world_map[world->planet[i].x][world->planet[i].y] = 2;
}
else if (world->planet[i].x > centre_x &&
world->planet[i].y > centre_y) {
world->world_map[world->planet[i].x--][world->planet[i].y++] = 0;
world->world_map[world->planet[i].x][world->planet[i].y] = 2;
}
else if (world->planet[i].x < centre_x &&
world->planet[i].y > centre_y) {
world->world_map[world->planet[i].x--][world->planet[i].y--] = 0;
world->world_map[world->planet[i].x][world->planet[i].y] = 2;
}
}
}
// 判断是否天下归一
bool judge_world(World *world) {
int planet_x = centre_x;
int planet_y = centre_y - 1;
for (int i = 0; i < MAX_PLANETS; i++, planet_y--) {
if (world->planet[i].x != planet_x ||
world->planet[i].y != planet_y)
return false;
}
return true;
}
int main() {
cin.get();
World solar; // 太阳系
init_world(&solar);
int step = 0; // 步数
while (1) {
system("CLS");
if (judge_world(&solar) && step)
break;
show_world(&solar);
update_world(&solar);
step++;
cout << "第 " << step << " 步。\n";
Sleep(200);
}
show_world(&solar);
cout << step << " 步后,天下归一。\n";
return 0;
}
day0 发布了7 篇原创文章 · 获赞 5 · 访问量 3005 私信 关注