Advertisement
Guest User

Untitled

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