Advertisement
Guest User

Untitled

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