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;
- using System.Threading;
- namespace RunnerInMap
- {
- public enum Direction
- {
- Top, Right, Bottom, Left, None
- }
- abstract class MapObject
- {
- public int x, y;
- }
- class Wall : MapObject
- {
- public static char Sym = '#';
- public Wall(int coordX, int coordY) { x = coordX; y = coordY; }
- }
- class ExitDoor : MapObject
- {
- public static char Sym = '%';
- public ExitDoor(int coordX, int coordY) { x = coordX; y = coordY; }
- }
- class Monster : MapObject
- {
- public static char Sym = 'X';
- public Monster(int coordX, int coordY) { x = coordX; y = coordY; }
- public void Move(MapObject[,] map, Direction direction)
- {
- int newX = x;
- int newY = y;
- switch (direction)
- {
- case Direction.Top:
- newY = y - 1;
- break;
- case Direction.Right:
- newX = x + 1;
- break;
- case Direction.Bottom:
- newY = y + 1;
- break;
- case Direction.Left:
- newX = x - 1;
- break;
- }
- if (map[newX, newY] is Wall) // в стену идти нельзя
- return;
- if (map[newX, newY] is ExitDoor)
- return;
- map[newX, newY] = new Monster(newX, newY);
- map[x, y] = null;
- }
- public void RandomMove(MapObject[,] map)
- {
- Random rnd = new Random();
- Direction direction = (Direction)rnd.Next(0, 4);
- Move(map, 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 Player(int coordX, int coordY) { x = coordX; y = coordY; }
- public void Move(MapObject[,] map, Direction direction)
- {
- int newX = x;
- int newY = y;
- switch (direction)
- {
- case Direction.Top:
- newY = y - 1;
- break;
- case Direction.Right:
- newX = x + 1;
- break;
- case Direction.Bottom:
- newY = y + 1;
- break;
- case Direction.Left:
- newX = x - 1;
- break;
- }
- if (map[newX, newY] is Wall) // в стену идти нельзя
- return;
- if (map[newX, newY] is ExitDoor)
- Program.WinGame();
- map[newX, newY] = new Player(newX, newY);
- map[x, y] = null;
- }
- public bool CheckCollisionMob(MapObject[,] map, int rad)
- {
- for (int j = 0; j < map.GetLength(1); j++)
- {
- for (int i = 0; i < map.GetLength(0); i++)
- {
- if (i > x - rad && j > y - rad && i < x + rad && j < y + rad)
- {
- if (map[i, j] is Monster)
- return true;
- }
- }
- }
- return false;
- }
- 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;
- }
- }
- class Money
- {
- public static char Sym = '$';
- }
- class Program
- {
- public static void WriteMapWarFog(MapObject[,] map, int sizeFog)
- {
- Console.Clear();
- List<int[]> coordPlayer = Player.FindObjects(map);
- int xPlayer = coordPlayer[0][0];
- int yPlayer = coordPlayer[0][1];
- StringBuilder StringMap = new StringBuilder();
- for (int j = 0; j < map.GetLength(1); j++)
- {
- for (int i = 0; i < map.GetLength(0); i++)
- {
- 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 j = 0; j < map.GetLength(1); j++)
- {
- for (int i = 0; i < map.GetLength(0); i++)
- {
- 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)
- {
- Console.CursorVisible = false;
- string path = "../../map.txt";
- string[] lines = File.ReadAllLines(path);
- MapObject[,] map = new MapObject[lines[0].Length, lines.Length];
- Console.SetWindowSize(lines[0].Length + 2, lines.Length + 2);
- for (int j = 0; j < lines.Length; j++)
- {
- for (int i = 0; i < lines[0].Length; i++)
- {
- if (lines[j][i] == Wall.Sym)
- map[i, j] = new Wall(i, j);
- else if (lines[j][i] == Player.Sym)
- map[i, j] = new Player(i, j);
- else if (lines[j][i] == Monster.Sym)
- map[i, j] = new Monster(i, j);
- else if (lines[j][i] == ExitDoor.Sym)
- map[i, j] = new ExitDoor(i, j);
- else
- map[i, j] = null;
- }
- }
- List<int[]> coordPlayer = Player.FindObjects(map);
- List<int[]> coordMonsters = Monster.FindObjects(map);
- int sleepTime = 100;
- int monsterSleepTime = 200 * sleepTime;
- int monsterTicks = monsterSleepTime;
- while(true)
- {
- if (isGameOver) break;
- //WriteMapWarFog(map, 5);
- WriteMap(map);
- if (monsterTicks != 0) monsterTicks -= sleepTime;
- else
- {
- monsterTicks = monsterSleepTime;
- coordMonsters = Monster.FindObjects(map);
- for (int i = 0; i < coordMonsters.Count; i++)
- {
- ((Monster)map[coordMonsters[i][0], coordMonsters[i][1]]).RandomMove(map);
- }
- }
- if (!Console.KeyAvailable) continue;
- ConsoleKey[] allowedKeys = new[] { ConsoleKey.LeftArrow, ConsoleKey.RightArrow, ConsoleKey.UpArrow, ConsoleKey.DownArrow };
- var key = Console.ReadKey(true).Key;
- if (!allowedKeys.Contains(key)) continue;
- Direction direction = Direction.None;
- if (key == ConsoleKey.LeftArrow) direction = Direction.Left;
- if (key == ConsoleKey.RightArrow) direction = Direction.Right;
- if (key == ConsoleKey.UpArrow) direction = Direction.Top;
- if (key == ConsoleKey.DownArrow) direction = Direction.Bottom;
- if (key == ConsoleKey.Escape) break;
- if (direction != Direction.None)
- {
- coordPlayer = Player.FindObjects(map);
- if (((Player)map[coordPlayer[0][0], coordPlayer[0][1]]).CheckCollisionMob(map, 3))
- {
- Console.Clear();
- Console.WriteLine("You LOSE!");
- break;
- }
- ((Player)map[coordPlayer[0][0], coordPlayer[0][1]]).Move(map, direction);
- }
- Thread.Sleep((int)sleepTime);
- }
- }
- public static void WinGame()
- {
- isGameOver = true;
- Console.Clear();
- Console.WriteLine("You WIN!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement