Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading;
- namespace GridTest
- {
- public class Program
- {
- public const int SizeX = 100;
- public const int SizeY = 20;
- private static Random random = new Random();
- private static bool[,] map = new bool[SizeX,SizeY];
- private static int playerX = -1;
- private static int playerY = -1;
- private static bool[,] memory = new bool[SizeX,SizeY];
- public static void Main(string[] args)
- {
- Console.CursorVisible = false;
- for(int i = 0; i < SizeX; i++)
- {
- for(int j = 0; j < SizeY; j++)
- {
- if(random.Next(5) == 0)
- SetBlock(i, j);
- }
- Console.Write('\n');
- }
- bool ready = false;
- for(int i = 0; i < SizeX; i++)
- {
- for(int j = 0; j < SizeY; j++)
- {
- if(!map[i, j])
- {
- SetPlayerPos(i, j);
- ready = true;
- }
- if(ready)
- break;
- }
- if(ready)
- break;
- }
- while(true)
- {
- Direction dir = (Direction)random.Next(4);
- if(CanMove(dir))
- {
- Move(dir);
- Thread.Sleep(100);
- }
- }
- Console.ReadKey();
- }
- public static void SetBlock(int x, int y)
- {
- map[x, y] = true;
- Console.SetCursorPosition(x, y);
- Console.BackgroundColor = ConsoleColor.White;
- Console.Write(' ');
- Console.BackgroundColor = ConsoleColor.Black;
- }
- public static void SetPlayerPos(int x, int y)
- {
- if(playerX != -1)
- {
- Console.SetCursorPosition(playerX, playerY);
- Console.Write(' ');
- }
- playerX = x;
- playerY = y;
- Console.SetCursorPosition(playerX, playerY);
- Console.BackgroundColor = ConsoleColor.Yellow;
- Console.Write(' ');
- Console.BackgroundColor = ConsoleColor.Black;
- }
- public static bool CanMove(Direction dir)
- {
- if(dir == Direction.TOP)
- return playerY > 0 && !map[playerX, playerY - 1];
- else if(dir == Direction.RIGHT)
- return playerX < SizeX - 1 && !map[playerX + 1, playerY];
- else if(dir == Direction.BOTTOM)
- return playerY < SizeY - 1 && !map[playerX, playerY + 1];
- else if(dir == Direction.LEFT)
- return playerX > 0 && !map[playerX - 1, playerY];
- else return false;
- }
- public static void Discover(Direction dir)
- {
- if(dir == Direction.TOP)
- {
- if(playerY > 0)
- memory[playerX, playerY - 1] = true;
- }
- else if(dir == Direction.RIGHT)
- {
- if(playerX < SizeX - 1)
- memory[playerX + 1, playerY] = true;
- }
- else if(dir == Direction.BOTTOM)
- {
- if(playerY < SizeY - 1)
- memory[playerX, playerY + 1] = true;
- }
- else if(dir == Direction.LEFT)
- {
- if(playerX > 0)
- memory[playerX - 1, playerY] = true;
- }
- }
- public static void Move(Direction dir)
- {
- if(dir == Direction.TOP)
- {
- if(playerY > 0)
- SetPlayerPos(playerX, playerY - 1);
- }
- else if(dir == Direction.RIGHT)
- {
- if(playerX < SizeX - 1)
- SetPlayerPos(playerX + 1, playerY);
- }
- else if(dir == Direction.BOTTOM)
- {
- if(playerY < SizeY - 1)
- SetPlayerPos(playerX, playerY + 1);
- }
- else if(dir == Direction.LEFT)
- {
- if(playerX > 0)
- SetPlayerPos(playerX - 1, playerY);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement