Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.10 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Threading;
  8.  
  9. namespace RunnerInMap
  10. {
  11.     public enum Direction
  12.     {
  13.         Top, Right, Bottom, Left, None
  14.     }
  15.     abstract class MapObject
  16.     {
  17.         public int x, y;
  18.     }
  19.     class Wall : MapObject
  20.     {
  21.         public static char Sym = '#';
  22.         public Wall(int coordX, int coordY) { x = coordX; y = coordY; }
  23.     }
  24.     class ExitDoor : MapObject
  25.     {
  26.         public static char Sym = '%';
  27.         public ExitDoor(int coordX, int coordY) { x = coordX; y = coordY; }
  28.     }
  29.     class Monster : MapObject
  30.     {
  31.         public static char Sym = 'X';
  32.         public Monster(int coordX, int coordY) { x = coordX; y = coordY; }
  33.         public void Move(MapObject[,] map, Direction direction)
  34.         {
  35.             int newX = x;
  36.             int newY = y;
  37.             switch (direction)
  38.             {
  39.                 case Direction.Top:
  40.                     newY = y - 1;
  41.                     break;
  42.                 case Direction.Right:
  43.                     newX = x + 1;
  44.                     break;
  45.                 case Direction.Bottom:
  46.                     newY = y + 1;
  47.                     break;
  48.                 case Direction.Left:
  49.                     newX = x - 1;
  50.                     break;
  51.             }
  52.             if (map[newX, newY] is Wall) // в стену идти нельзя
  53.                 return;
  54.             if (map[newX, newY] is ExitDoor)
  55.                 return;
  56.             map[newX, newY] = new Monster(newX, newY);
  57.             map[x, y] = null;
  58.         }
  59.         public void RandomMove(MapObject[,] map)
  60.         {
  61.             Random rnd = new Random();
  62.             Direction direction = (Direction)rnd.Next(0, 4);
  63.             Move(map, direction);
  64.         }
  65.         public static List<int[]> FindObjects(MapObject[,] map)
  66.         {
  67.             List<int[]> ListObjects = new List<int[]>();
  68.             for (int i = 0; i < map.GetLength(0); i++)
  69.                 for (int j = 0; j < map.GetLength(1); j++)
  70.                     if (map[i, j] is Monster)
  71.                         ListObjects.Add(new int[] { i, j });
  72.             if (ListObjects.Count > 0)
  73.                 return ListObjects;
  74.             return null;
  75.         }
  76.     }
  77.     class Player : MapObject
  78.     {
  79.         public static char Sym = '@';
  80.         public Player(int coordX, int coordY) { x = coordX; y = coordY; }
  81.         public void Move(MapObject[,] map, Direction direction)
  82.         {
  83.             int newX = x;
  84.             int newY = y;
  85.             switch (direction)
  86.             {
  87.                 case Direction.Top:
  88.                     newY = y - 1;
  89.                     break;
  90.                 case Direction.Right:
  91.                     newX = x + 1;
  92.                     break;
  93.                 case Direction.Bottom:
  94.                     newY = y + 1;
  95.                     break;
  96.                 case Direction.Left:
  97.                     newX = x - 1;
  98.                     break;
  99.             }
  100.             if (map[newX, newY] is Wall) // в стену идти нельзя
  101.                 return;
  102.             if (map[newX, newY] is ExitDoor)
  103.                 Program.WinGame();
  104.             map[newX, newY] = new Player(newX, newY);
  105.             map[x, y] = null;
  106.         }
  107.         public bool CheckCollisionMob(MapObject[,] map, int rad)
  108.         {
  109.             for (int j = 0; j < map.GetLength(1); j++)
  110.             {
  111.                 for (int i = 0; i < map.GetLength(0); i++)
  112.                 {
  113.                     if (i > x - rad && j > y - rad && i < x + rad && j < y + rad)
  114.                     {
  115.                         if (map[i, j] is Monster)
  116.                             return true;
  117.                     }
  118.  
  119.                 }
  120.             }
  121.             return false;
  122.  
  123.         }
  124.         public static List<int[]> FindObjects(MapObject[,] map)
  125.         {
  126.             List<int[]> ListObjects = new List<int[]>();
  127.             for (int i = 0; i < map.GetLength(0); i++)
  128.                 for (int j = 0; j < map.GetLength(1); j++)
  129.                     if (map[i, j] is Player)
  130.                         ListObjects.Add(new int[] { i, j });
  131.             if (ListObjects.Count > 0)
  132.                 return ListObjects;
  133.             return null;
  134.         }
  135.     }
  136.     class Money
  137.     {
  138.         public static char Sym = '$';
  139.     }
  140.     class Program
  141.     {
  142.         public static void WriteMapWarFog(MapObject[,] map, int sizeFog)
  143.         {
  144.             Console.Clear();
  145.  
  146.             List<int[]> coordPlayer = Player.FindObjects(map);
  147.             int xPlayer = coordPlayer[0][0];
  148.             int yPlayer = coordPlayer[0][1];
  149.  
  150.             StringBuilder StringMap = new StringBuilder();
  151.  
  152.             for (int j = 0; j < map.GetLength(1); j++)
  153.             {
  154.                 for (int i = 0; i < map.GetLength(0); i++)
  155.                 {
  156.                     if (i > xPlayer - sizeFog && j > yPlayer - sizeFog && i < xPlayer + sizeFog && j < yPlayer + sizeFog)
  157.                     {
  158.                         if (map[i, j] is Wall)
  159.                             StringMap.Append(Wall.Sym);
  160.                         else if (map[i, j] is Player)
  161.                             StringMap.Append(Player.Sym);
  162.                         else if (map[i, j] is Monster)
  163.                             StringMap.Append(Monster.Sym);
  164.                         else if (map[i, j] is ExitDoor)
  165.                             StringMap.Append(ExitDoor.Sym);
  166.                         else
  167.                             StringMap.Append(' ');
  168.                     }
  169.  
  170.                     else
  171.                         StringMap.Append(' ');
  172.  
  173.                 }
  174.                 StringMap.Append("\n");
  175.             }
  176.             Console.WriteLine(StringMap);
  177.         }
  178.         public static void WriteMap(MapObject[,] map)
  179.         {
  180.             Console.Clear();
  181.  
  182.             StringBuilder StringMap = new StringBuilder();
  183.  
  184.             for (int j = 0; j < map.GetLength(1); j++)
  185.             {
  186.                 for (int i = 0; i < map.GetLength(0); i++)
  187.                 {
  188.                     if (map[i, j] is Wall)
  189.                         StringMap.Append(Wall.Sym);
  190.                     else if (map[i, j] is Player)
  191.                         StringMap.Append(Player.Sym);
  192.                     else if (map[i, j] is Monster)
  193.                         StringMap.Append(Monster.Sym);
  194.                     else if (map[i, j] is ExitDoor)
  195.                         StringMap.Append(ExitDoor.Sym);
  196.                     else
  197.                         StringMap.Append(' ');
  198.                 }
  199.                 StringMap.Append("\n");
  200.             }
  201.  
  202.             Console.WriteLine(StringMap);
  203.         }
  204.  
  205.         static bool isGameOver = false;
  206.         static void Main(string[] args)
  207.         {
  208.             Console.CursorVisible = false;
  209.  
  210.             string path = "../../map.txt";
  211.             string[] lines = File.ReadAllLines(path);
  212.  
  213.             MapObject[,] map = new MapObject[lines[0].Length, lines.Length];
  214.             Console.SetWindowSize(lines[0].Length + 2, lines.Length + 2);
  215.  
  216.             for (int j = 0; j < lines.Length; j++)
  217.             {
  218.                 for (int i = 0; i < lines[0].Length; i++)
  219.                 {
  220.                     if (lines[j][i] == Wall.Sym)
  221.                         map[i, j] = new Wall(i, j);
  222.                     else if (lines[j][i] == Player.Sym)
  223.                         map[i, j] = new Player(i, j);
  224.                     else if (lines[j][i] == Monster.Sym)
  225.                         map[i, j] = new Monster(i, j);
  226.                     else if (lines[j][i] == ExitDoor.Sym)
  227.                         map[i, j] = new ExitDoor(i, j);
  228.                     else
  229.                         map[i, j] = null;
  230.                 }  
  231.             }
  232.  
  233.             List<int[]> coordPlayer = Player.FindObjects(map);
  234.             List<int[]> coordMonsters = Monster.FindObjects(map);
  235.  
  236.             int sleepTime = 100;
  237.             int monsterSleepTime = 200 * sleepTime;
  238.             int monsterTicks = monsterSleepTime;
  239.  
  240.             while(true)
  241.             {
  242.                 if (isGameOver) break;
  243.  
  244.                 //WriteMapWarFog(map, 5);
  245.                 WriteMap(map);
  246.  
  247.                 if (monsterTicks != 0) monsterTicks -= sleepTime;
  248.                 else
  249.                 {
  250.                     monsterTicks = monsterSleepTime;
  251.  
  252.                     coordMonsters = Monster.FindObjects(map);
  253.                     for (int i = 0; i < coordMonsters.Count; i++)
  254.                     {
  255.                         ((Monster)map[coordMonsters[i][0], coordMonsters[i][1]]).RandomMove(map);
  256.                     }
  257.                 }
  258.  
  259.                 if (!Console.KeyAvailable) continue;
  260.  
  261.  
  262.                 ConsoleKey[] allowedKeys = new[] { ConsoleKey.LeftArrow, ConsoleKey.RightArrow, ConsoleKey.UpArrow, ConsoleKey.DownArrow };
  263.  
  264.                 var key = Console.ReadKey(true).Key;
  265.                 if (!allowedKeys.Contains(key)) continue;
  266.  
  267.                 Direction direction = Direction.None;
  268.                 if (key == ConsoleKey.LeftArrow) direction = Direction.Left;
  269.                 if (key == ConsoleKey.RightArrow) direction = Direction.Right;
  270.                 if (key == ConsoleKey.UpArrow) direction = Direction.Top;
  271.                 if (key == ConsoleKey.DownArrow) direction = Direction.Bottom;
  272.                 if (key == ConsoleKey.Escape) break;
  273.  
  274.                 if (direction != Direction.None)
  275.                 {
  276.                     coordPlayer = Player.FindObjects(map);
  277.                     if (((Player)map[coordPlayer[0][0], coordPlayer[0][1]]).CheckCollisionMob(map, 3))
  278.                     {
  279.                         Console.Clear();
  280.                         Console.WriteLine("You LOSE!");
  281.                         break;
  282.                     }
  283.                     ((Player)map[coordPlayer[0][0], coordPlayer[0][1]]).Move(map, direction);
  284.                 }
  285.  
  286.                 Thread.Sleep((int)sleepTime);
  287.             }
  288.         }
  289.         public static void WinGame()
  290.         {
  291.             isGameOver = true;
  292.             Console.Clear();
  293.             Console.WriteLine("You WIN!");
  294.         }
  295.     }
  296. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement