Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace RunnerInMap
- {
- public enum Direction
- {
- Top, Right, Bottom, Left
- }
- abstract class MapObject
- {
- }
- class Wall : MapObject
- {
- public static char Sym = '#';
- }
- class ExitDoor : MapObject
- {
- public static char Sym = '%';
- }
- class Monster : MapObject
- {
- public static char Sym = 'X';
- public static void Move(MapObject[,] map, int x, int y, Direction direction)
- {
- int newX = x;
- int newY = y;
- switch (direction)
- {
- case Direction.Top:
- newX = x - 1;
- break;
- case Direction.Right:
- newY = y + 1;
- break;
- case Direction.Bottom:
- newX = x + 1;
- break;
- case Direction.Left:
- newY = y - 1;
- break;
- }
- if (map[newX, newY] is Wall) // в стену идти нельзя
- return;
- if (map[newX, newY] is ExitDoor || map[newX, newY] is Monster)
- return;
- map[newX, newY] = map[x, y];
- map[x, y] = new Empty();
- }
- public static void RandomMove(MapObject[,] map)
- {
- List<int[]> coordMobs = FindObjects(map);
- Random rnd = new Random();
- for (int i = 0; i < coordMobs.Count; i++)
- {
- Direction direction = (Direction)rnd.Next(0, 3);
- Move(map, coordMobs[i][0], coordMobs[i][1], direction);
- }
- }
- public static List<int[]> FindObjects(MapObject[,] map)
- {
- List<int[]> ListObjects = new List<int[]>();
- for (int i = 0; i < map.GetLength(0); i++)
- for (int j = 0; j < map.GetLength(1); j++)
- if (map[i, j] is Monster)
- ListObjects.Add(new int[] { i, j });
- if (ListObjects.Count > 0)
- return ListObjects;
- return null;
- }
- }
- class Player : MapObject
- {
- public static char Sym = '@';
- public static void Move(MapObject[,] map, Direction direction)
- {
- List<int[]> playerPos = Player.FindObjects(map);
- int x = playerPos[0][0];
- int y = playerPos[0][1];
- int newX = x;
- int newY = y;
- switch (direction)
- {
- case Direction.Top:
- newX = x - 1;
- break;
- case Direction.Right:
- newY = y + 1;
- break;
- case Direction.Bottom:
- newX = x + 1;
- break;
- case Direction.Left:
- newY = y - 1;
- break;
- }
- if (map[newX, newY] is Wall) // в стену идти нельзя
- return;
- if (map[newX, newY] is ExitDoor)
- Program.WinGame();
- map[newX, newY] = map[x, y];
- map[x, y] = new Empty();
- }
- public static List<int[]> FindObjects(MapObject[,] map)
- {
- List<int[]> ListObjects = new List<int[]>();
- for (int i = 0; i < map.GetLength(0); i++)
- for (int j = 0; j < map.GetLength(1); j++)
- if (map[i, j] is Player)
- ListObjects.Add(new int[] { i, j });
- if (ListObjects.Count > 0)
- return ListObjects;
- return null;
- }
- public static bool CheckCollisionMob(MapObject[,] map, int rad)
- {
- List<int[]> playerPos = Player.FindObjects(map);
- int xPlayer = playerPos[0][0], yPlayer = playerPos[0][1];
- for (int i = 0; i < map.GetLength(0); i++)
- {
- for (int j = 0; j < map.GetLength(1); j++)
- {
- if (i > xPlayer - rad && j > yPlayer - rad && i < xPlayer + rad && j < yPlayer + rad)
- {
- if (map[i, j] is Monster)
- return true;
- }
- }
- }
- return false;
- }
- }
- class Empty : MapObject
- {
- public static char Sym = ' ';
- }
- class Money
- {
- public static char Sym = '$';
- }
- class Program
- {
- public static void WriteMapWarFog(MapObject[,] map, int sizeFog)
- {
- Console.Clear();
- List<int[]> playerPos = Player.FindObjects(map);
- int xPlayer = playerPos[0][0], yPlayer = playerPos[0][1];
- StringBuilder StringMap = new StringBuilder();
- for (int i = 0; i < map.GetLength(0); i++)
- {
- for (int j = 0; j < map.GetLength(1); j++)
- {
- if (i > xPlayer - sizeFog && j > yPlayer - sizeFog && i < xPlayer + sizeFog && j < yPlayer + sizeFog)
- {
- if (map[i, j] is Wall)
- StringMap.Append(Wall.Sym);
- else if (map[i, j] is Player)
- StringMap.Append(Player.Sym);
- else if (map[i, j] is Monster)
- StringMap.Append(Monster.Sym);
- else if (map[i, j] is ExitDoor)
- StringMap.Append(ExitDoor.Sym);
- else StringMap.Append(' ');
- }
- else
- StringMap.Append(' ');
- }
- StringMap.Append("\n");
- }
- Console.WriteLine(StringMap);
- }
- public static void WriteMap(MapObject[,] map)
- {
- Console.Clear();
- StringBuilder StringMap = new StringBuilder();
- for (int i = 0; i < map.GetLength(0); i++)
- {
- for (int j = 0; j < map.GetLength(1); j++)
- {
- if (map[i, j] is Wall)
- StringMap.Append(Wall.Sym);
- else if (map[i, j] is Player)
- StringMap.Append(Player.Sym);
- else if (map[i, j] is Monster)
- StringMap.Append(Monster.Sym);
- else if (map[i, j] is ExitDoor)
- StringMap.Append(ExitDoor.Sym);
- else StringMap.Append(' ');
- }
- StringMap.Append("\n");
- }
- Console.WriteLine(StringMap);
- }
- static bool isGameOver = false;
- static void Main(string[] args)
- {
- string path = "../../map.txt";
- string[] lines = File.ReadAllLines(path);
- MapObject[,] map = new MapObject[lines.Length, lines[0].Length];
- Console.SetWindowSize(lines[0].Length + 2, lines.Length + 2);
- List<int[]> coordMobs = new List<int[]>();
- for (int i = 0; i < lines.Length; i++)
- for (int j = 0; j < lines[0].Length; j++)
- if (lines[i][j] == Wall.Sym)
- map[i, j] = new Wall();
- else if (lines[i][j] == Player.Sym)
- map[i, j] = new Player();
- else if (lines[i][j] == Monster.Sym)
- map[i, j] = new Monster();
- else if (lines[i][j] == ExitDoor.Sym)
- map[i, j] = new ExitDoor();
- else
- map[i, j] = null;
- ConsoleKeyInfo key;
- do
- {
- if (isGameOver) break;
- //WriteMapWarFog(map, 5);
- WriteMap(map);
- key = Console.ReadKey();
- Direction direction = Direction.Top;
- if (key.Key == ConsoleKey.LeftArrow) direction = Direction.Left;
- if (key.Key == ConsoleKey.RightArrow) direction = Direction.Right;
- if (key.Key == ConsoleKey.UpArrow) direction = Direction.Top;
- if (key.Key == ConsoleKey.DownArrow) direction = Direction.Bottom;
- Player.Move(map, direction);
- Monster.RandomMove(map);
- if (Player.CheckCollisionMob(map, 3))
- {
- Console.Clear();
- Console.WriteLine("You LOSE!");
- break;
- }
- }
- while (key.Key != ConsoleKey.Escape);
- }
- public static void WinGame()
- {
- isGameOver = true;
- Console.Clear();
- Console.WriteLine("You WIN!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement