OldBeliver

Function_04ver02

Apr 1st, 2021 (edited)
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.82 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace easyMap_v02
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Console.CursorVisible = false;
  14.  
  15.             bool isPlaying = true;
  16.  
  17.             char theseus = '♂';
  18.             int theseusX;
  19.             int theseusY;
  20.             int alongX;
  21.             int alongY;
  22.  
  23.             char[,] maze = ReadMaze();
  24.  
  25.             DrawMaze(maze, theseus, out theseusX, out theseusY);
  26.  
  27.             while (isPlaying)
  28.             {
  29.                 if (Console.KeyAvailable)
  30.                 {
  31.                     ConsoleKeyInfo pressedKey = Console.ReadKey(true);
  32.  
  33.                     ChangeDirection(pressedKey, out alongX, out alongY);
  34.  
  35.                     if (maze[theseusX + alongX, theseusY + alongY] != '#')
  36.                     {
  37.                         Move(theseus, ref theseusX, ref theseusY, alongX, alongY);
  38.                     }
  39.                 }
  40.             }
  41.         }
  42.  
  43.         static char[,] ReadMaze()
  44.         {
  45.             char[,] map = new char[20, 20]
  46.             {
  47.                 {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'},
  48.                 {'#',' ',' ',' ',' ',' ','#',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ','#'},
  49.                 {'#',' ','#','#','#','#','#',' ','#','#','#','#',' ','#','#','#',' ','#','#','#'},
  50.                 {'#',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ','#',' ','#',' ','#'},
  51.                 {'#',' ','#',' ','#','#','#',' ','#','#','#','#','#','#',' ','#',' ','#',' ','#'},
  52.                 {'#',' ','#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ','#',' ','#',' ','#'},
  53.                 {'#',' ','#','#','#',' ','#','#','#','#','#','#',' ','#','#','#','#','#',' ','#'},
  54.                 {'#',' ','#','#','#',' ',' ',' ',' ','#',' ','#',' ','#',' ',' ',' ',' ',' ','#'},
  55.                 {'#',' ',' ',' ',' ','#','#','#',' ',' ',' ',' ',' ',' ',' ','#','#','#','#','#'},
  56.                 {'#','#','#','#','#',' ',' ',' ','#','#','#','#','#','#',' ',' ',' ',' ',' ','#'},
  57.                 {'#',' ',' ',' ','#','#','#',' ','#',' ','#',' ',' ',' ',' ','#',' ','#',' ','#'},
  58.                 {'#','#','#',' ','#','#','#',' ','#',' ','#',' ','#','#','#','#',' ','#',' ','#'},
  59.                 {'#',' ',' ',' ',' ',' ','#',' ','#',' ',' ',' ','#','#',' ','#',' ','#',' ','#'},
  60.                 {'#','#','#','#','#',' ',' ',' ',' ',' ','#','#','#','#',' ','#',' ','#','#','#'},
  61.                 {'#',' ',' ',' ',' ',' ','#',' ','#','#','#',' ',' ',' ',' ','#',' ',' ',' ','#'},
  62.                 {'#',' ','#','#','#','#','#',' ','#',' ','#','#','#','#',' ','#','#','#',' ','#'},
  63.                 {'#',' ','#',' ',' ',' ',' ',' ','#',' ',' ','#',' ','#',' ',' ',' ',' ',' ','#'},
  64.                 {'#',' ','#',' ','#','#','#','#','#','#',' ','#',' ','#','#','#','#','#',' ','#'},
  65.                 {'#',' ','#','♂',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
  66.                 {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'}
  67.             };
  68.             return map;
  69.         }
  70.  
  71.         static void DrawMaze(char[,] map, char theseus, out int theseusX, out int theseusY)
  72.         {
  73.             theseusX = 1;
  74.             theseusY = 1;
  75.  
  76.             for (int i = 0; i < map.GetLength(0); i++)
  77.             {
  78.                 for (int j = 0; j < map.GetLength(1); j++)
  79.                 {
  80.                     Console.Write($"{map[i, j]}");
  81.  
  82.                     if (map[i, j] == theseus)
  83.                     {
  84.                         theseusX = i;
  85.                         theseusY = j;
  86.                     }
  87.                 }
  88.                 Console.WriteLine();
  89.             }
  90.         }
  91.  
  92.         static void ChangeDirection(ConsoleKeyInfo key, out int alongX, out int alongY)
  93.         {
  94.             alongX = 0;
  95.             alongY = 0;
  96.  
  97.             switch (key.Key)
  98.             {
  99.                 case ConsoleKey.UpArrow:
  100.                     alongX = -1; alongY = 0;
  101.                     break;
  102.                 case ConsoleKey.DownArrow:
  103.                     alongX = 1; alongY = 0;
  104.                     break;
  105.                 case ConsoleKey.LeftArrow:
  106.                     alongX = 0; alongY = -1;
  107.                     break;
  108.                 case ConsoleKey.RightArrow:
  109.                     alongX = 0; alongY = 1;
  110.                     break;
  111.             }
  112.         }
  113.  
  114.         static void Move(char theseus, ref int theseusX, ref int theseusY, int alongX, int alongY)
  115.         {
  116.             Console.SetCursorPosition(theseusY, theseusX);
  117.             Console.Write(' ');
  118.  
  119.             theseusX += alongX;
  120.             theseusY += alongY;
  121.  
  122.             Console.SetCursorPosition(theseusY, theseusX);
  123.             Console.Write(theseus);
  124.         }
  125.     }
  126. }
Add Comment
Please, Sign In to add comment