簡単なソース

Listing. 1: 棒倒し法の簡単なソース
"Main.cpp"
# include <Siv3D.hpp> // Siv3D v0.6.14
 
const Size BLOCK_SIZE{ 32, 32 };
const Size MazeSize{ 21, 15 };//サイズは奇数でなければいけない
 
enum OBJS
{
	WALL, BAR, FLOOR, MAX_OBJS
};
 
//二次元配列 MazeData[MazeSize.y][MazeSize.x]{FLOORで初期化}
std::vector<std::vector<int>> MazeData(MazeSize.y, std::vector<int>(MazeSize.x, FLOOR));
 
 
void MakeWall(int w, int h);
void MakeMaze(int w, int h);
void PushDownBar(int w, int h);
void DrawMaze(int w, int h);
 
//w, h 迷路の幅と高さ
void MakeMaze(int w, int h)
{
	MakeWall(w, h);
	PushDownBar(w, h);
}
 
 
 
 
void MakeWall(int w, int h)
{
	for (int j = 0; j < h; j++)
	{
		for (int i = 0; i < w; i++)
		{
			if (i == 0 || j == 0 || i == w - 1 || j == h - 1)
				MazeData[j][i] = WALL;
			continue;
		}
	}
}
 
void PushDownBar(int w, int h)
{
	for (int j = 0; j < h; j++)
	{
		for (int i = 0; i < w; i++)
		{
			if (!(i == 0 || j == 0 || i == w - 1 || j == h - 1))
			{
				if (i % 2 == 0 && j % 2 == 0)
				{
					MazeData[j][i] = BAR;
					//棒を立てたらすかさず倒す
					int rnd = rand() % 4;//0:↑ 1:→ 2:↓ 3:←
					Vec2 Dir[]{ {0,-1},{1, 0},{0,1},{-1,0} };
					MazeData[j + (int)Dir[rnd].y][i + (int)Dir[rnd].x] = BAR;
				}
			}
		}
	}
}
 
void DrawMaze(int w, int h)
{
	for (int j = 0; j < h; j++)
	{
		for (int i = 0; i < w; i++)
		{
			Color col[MAX_OBJS]{ Palette::Firebrick,  Palette::Darkorange, Palette::Black };
			//if (MazeData[j][i] != FLOOR)
				Rect{ i * BLOCK_SIZE.x, j * BLOCK_SIZE.y, BLOCK_SIZE }.draw(col[MazeData[j][i]]);
		}
	}
}
 
 
void Main()
{
	// 背景の色を設定する | Set the background color
	Scene::SetBackground(Palette::Cornflowerblue);
 
	MakeMaze(MazeSize.x, MazeSize.y);
 
	while (System::Update())
	{
		DrawMaze(MazeSize.x, MazeSize.y);
	}
}