【C#】迷路を出力する

通路を「0」、壁を「1」とした2次元配列で迷路を表現して、
通路を「 」(半角スペース)、壁を「#」として1行ごとに改行して出力する。

using System;

public class Maze {
    public const int PATH = 0;
    public const int WALL = 1;
    public static int[,] maze = new int[,] {
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
        {1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1},
        {1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1},
        {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
        {1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1},
        {1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1},
        {1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1},
        {1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1},
        {1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1},
        {1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1},
        {1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1},
        {1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1},
        {1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1},
        {1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1},
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
    };
    
    public static void print_maze() {
        for (int y = 0; y < Maze.maze.GetLength(0); ++y) {
            for (int x = 0; x < Maze.maze.GetLength(1); ++x) {
                if (Maze.maze[y,x] == Maze.WALL) {
                    Console.Write('#');
                } else if (Maze.maze[y,x] == Maze.PATH) {
                    Console.Write(' ');
                }
            }
            Console.WriteLine();
        }
    }
    
    public static void Main() {
        print_maze();
    }
}

今回は、以下のように出力される。

###############
# # #   # #   #
# # # ### ### #
#             #
# # ### ##### #
# # #       # #
# ########### #
#     #       #
# ### ##### # #
#   #     # # #
### ### # ### #
#     # # #   #
### ### ### # #
#     # #   # #
###############
タイトルとURLをコピーしました