Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.15 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3.  
  4. namespace GridTest
  5. {
  6.     public class Program
  7.     {
  8.         public const int SizeX = 100;
  9.         public const int SizeY = 20;
  10.         private static Random random = new Random();
  11.         private static bool[,] map = new bool[SizeX,SizeY];
  12.         private static int playerX = -1;
  13.         private static int playerY = -1;
  14.         private static bool[,] memory = new bool[SizeX,SizeY];
  15.  
  16.         public static void Main(string[] args)
  17.         {
  18.             Console.CursorVisible = false;
  19.            
  20.             for(int i = 0; i < SizeX; i++)
  21.             {
  22.                 for(int j = 0; j < SizeY; j++)
  23.                 {
  24.                     if(random.Next(5) == 0)
  25.                         SetBlock(i, j);
  26.                 }
  27.                 Console.Write('\n');
  28.             }
  29.  
  30.             bool ready = false;
  31.  
  32.             for(int i = 0; i < SizeX; i++)
  33.             {
  34.                 for(int j = 0; j < SizeY; j++)
  35.                 {
  36.                     if(!map[i, j])
  37.                     {
  38.                         SetPlayerPos(i, j);
  39.                         ready = true;
  40.                     }
  41.  
  42.                     if(ready)
  43.                         break;
  44.                 }
  45.  
  46.                 if(ready)
  47.                     break;
  48.             }
  49.  
  50.             while(true)
  51.             {
  52.                 Direction dir = (Direction)random.Next(4);
  53.  
  54.                 if(CanMove(dir))
  55.                 {
  56.                     Move(dir);
  57.                     Thread.Sleep(100);
  58.                 }
  59.                
  60.             }
  61.  
  62.             Console.ReadKey();
  63.         }
  64.  
  65.         public static void SetBlock(int x, int y)
  66.         {
  67.             map[x, y] = true;
  68.             Console.SetCursorPosition(x, y);
  69.             Console.BackgroundColor = ConsoleColor.White;
  70.             Console.Write(' ');
  71.             Console.BackgroundColor = ConsoleColor.Black;
  72.         }
  73.  
  74.         public static void SetPlayerPos(int x, int y)
  75.         {
  76.             if(playerX != -1)
  77.             {
  78.                 Console.SetCursorPosition(playerX, playerY);
  79.                 Console.Write(' ');
  80.             }
  81.  
  82.             playerX = x;
  83.             playerY = y;
  84.             Console.SetCursorPosition(playerX, playerY);
  85.             Console.BackgroundColor = ConsoleColor.Yellow;
  86.             Console.Write(' ');
  87.             Console.BackgroundColor = ConsoleColor.Black;
  88.         }
  89.  
  90.         public static bool CanMove(Direction dir)
  91.         {
  92.             if(dir == Direction.TOP)
  93.                 return playerY > 0 && !map[playerX, playerY - 1];
  94.             else if(dir == Direction.RIGHT)
  95.                 return playerX < SizeX - 1 && !map[playerX + 1, playerY];
  96.             else if(dir == Direction.BOTTOM)
  97.                 return playerY < SizeY - 1 && !map[playerX, playerY + 1];
  98.             else if(dir == Direction.LEFT)
  99.                 return playerX > 0 && !map[playerX - 1, playerY];
  100.             else return false;
  101.         }
  102.  
  103.         public static void Discover(Direction dir)
  104.         {
  105.             if(dir == Direction.TOP)
  106.             {
  107.                 if(playerY > 0)
  108.                     memory[playerX, playerY - 1] = true;
  109.             }
  110.             else if(dir == Direction.RIGHT)
  111.             {
  112.                 if(playerX < SizeX - 1)
  113.                     memory[playerX + 1, playerY] = true;
  114.             }
  115.             else if(dir == Direction.BOTTOM)
  116.             {
  117.                 if(playerY < SizeY - 1)
  118.                     memory[playerX, playerY + 1] = true;
  119.             }
  120.             else if(dir == Direction.LEFT)
  121.             {
  122.                 if(playerX > 0)
  123.                     memory[playerX - 1, playerY] = true;
  124.             }
  125.         }
  126.        
  127.         public static void Move(Direction dir)
  128.         {
  129.             if(dir == Direction.TOP)
  130.             {
  131.                 if(playerY > 0)
  132.                     SetPlayerPos(playerX, playerY - 1);
  133.             }
  134.             else if(dir == Direction.RIGHT)
  135.             {
  136.                 if(playerX < SizeX - 1)
  137.                     SetPlayerPos(playerX + 1, playerY);
  138.             }
  139.             else if(dir == Direction.BOTTOM)
  140.             {
  141.                 if(playerY < SizeY - 1)
  142.                     SetPlayerPos(playerX, playerY + 1);
  143.             }
  144.             else if(dir == Direction.LEFT)
  145.             {
  146.                 if(playerX > 0)
  147.                     SetPlayerPos(playerX - 1, playerY);
  148.             }
  149.         }
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement