Advertisement
Guest User

4.4

a guest
May 21st, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.50 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 Les3
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Console.CursorVisible = false;
  14.             Console.Title = "4.4 Brave new world";
  15.  
  16.             char[,] map = GetMap(30, 10);
  17.  
  18.             int userX = 3, userY = 3;
  19.             while (true)
  20.             {
  21.                 DrawMap(ref map);
  22.  
  23.                 Console.SetCursorPosition(userY, userX);
  24.                 Console.Write('@');
  25.                 ConsoleKeyInfo charKey = Console.ReadKey(true);
  26.  
  27.                 switch (charKey.Key)
  28.                 {
  29.                     case ConsoleKey.LeftArrow:
  30.                         if (map[userX, userY - 1] != '#')
  31.                             userY--;
  32.                         break;
  33.                     case ConsoleKey.RightArrow:
  34.                         if (map[userX, userY + 1] != '#')
  35.                             userY++;
  36.                         break;
  37.                     case ConsoleKey.UpArrow:
  38.                         if (map[userX - 1, userY] != '#')
  39.                             userX--;
  40.                         break;
  41.                     case ConsoleKey.DownArrow:
  42.                         if (map[userX + 1, userY] != '#')
  43.                             userX++;
  44.                         break;
  45.                 }
  46.             }
  47.         }
  48.  
  49.         static char[,] GetMap(int y, int x)
  50.         {
  51.             char[,] map = new char[x, y];
  52.             for (int h = 1; h < map.GetLength(0); h++)
  53.             {
  54.                 for (int w = 0; w < map.GetLength(1); w++)
  55.                 {
  56.                     map[h, w] = '.';
  57.                 }
  58.             }
  59.  
  60.             for (int i = 0; i < map.GetLength(1); i++)
  61.             {
  62.                 map[0, i] = '#';
  63.                 map[map.GetLength(0) - 1, i] = '#';
  64.             }
  65.  
  66.             for (int i = 0; i < map.GetLength(0); i++)
  67.             {
  68.                 map[i, 0] = '#';
  69.                 map[i, map.GetLength(1) - 1] = '#';
  70.             }
  71.  
  72.             return map;
  73.         }
  74.  
  75.         static void DrawMap(ref char[,] map)
  76.         {
  77.             Console.SetCursorPosition(0, 0);
  78.             for (int i = 0; i < map.GetLength(0); i++)
  79.             {
  80.                 for (int j = 0; j < map.GetLength(1); j++)
  81.                 {
  82.                     Console.Write(map[i, j]);
  83.                 }
  84.                 Console.WriteLine();
  85.             }
  86.         }
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement