mo49
11/10/2019 - 4:36 PM

SampleMazeGame.md

using System;

namespace SampleMazeGame
{
    class Program
    {
        static char[] map =
        {
            '#','#','#','#','#',
            '#',' ',' ',' ','#',
            '#',' ','#',' ','#',
            '#','P','#','G','#',
            '#','#','#','#','#',
        };

        static void DrawMap()
        {
            for(int i=0; i < map.Length; i++)
            {
                Console.Write(map[i]);
                if ((i+1)%5==0)
                {
                    Console.Write(System.Environment.NewLine);
                }
            }
        }
        
        static void MovePlayer(string key)
        {
            int playerPos = Array.IndexOf(map, 'P');
            int playerNextPos = 0;

            if (key == "a") playerNextPos = playerPos - 1;
            if (key == "d") playerNextPos = playerPos + 1;
            if (key == "w") playerNextPos = playerPos - 5;
            if (key == "s") playerNextPos = playerPos + 5;
            if (map[playerNextPos] != '#')
            {
                map[playerNextPos] = 'P';
                map[playerPos] = ' ';
                playerPos = playerNextPos;
            };
        }

        static bool CheckGoal()
        {
            return Array.IndexOf(map, 'G') < 0;
        }

        static void Main(string[] args)
        {
            DrawMap();

            while (true)
            {
                MovePlayer(Console.ReadLine());

                DrawMap();

                if (CheckGoal()) break;
            }
            Console.WriteLine("Goal!!");
        }
    }
}