Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.16 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4.  
  5. namespace GridTest
  6. {
  7.     public class Program
  8.     {
  9.         public const int SizeX = 100;
  10.         public const int SizeY = 20;
  11.         public const int Delay = 100;
  12.         private static Random random = new Random();
  13.         private static bool[,] map = new bool[SizeX,SizeY];
  14.         private static int playerX = -1;
  15.         private static int playerY = -1;
  16.         private static bool[,] memory = new bool[SizeX,SizeY];
  17.         private static Stack<Direction> moves = new Stack<Direction>();
  18.  
  19.         public static void Main(string[] args)
  20.         {
  21.             Console.CursorVisible = false;
  22.            
  23.             for(int i = 0; i < SizeX; i++)
  24.             {
  25.                 for(int j = 0; j < SizeY; j++)
  26.                 {
  27.                     if(random.Next(5) == 0) //Densité de 1 case remplie sur 5
  28.                         SetBlock(i, j);
  29.                 }
  30.             }
  31.  
  32.             //On place le joueur dans la première case vide trouvée
  33.             bool ready = false;
  34.  
  35.             for(int x = 0; x < SizeX; x++)
  36.             {
  37.                 for(int y = 0; y < SizeY; y++)
  38.                 {
  39.                     if(!map[x, y])
  40.                     {
  41.                         SetPlayerPos(x, y);
  42.                         ready = true;
  43.                     }
  44.  
  45.                     if(ready)
  46.                         break;
  47.                 }
  48.  
  49.                 if(ready)
  50.                     break;
  51.             }
  52.  
  53.             while(true)
  54.             {
  55.                 List<Direction> m = ListPossibleMoves();
  56.  
  57.                 if(m.Count > 0)
  58.                     Move(m[random.Next(m.Count)]);
  59.                 else if(moves.Count > 0)
  60.                     StepBack();
  61.                 else break;
  62.                
  63.                 Thread.Sleep(Delay);
  64.             }
  65.  
  66.             Console.ReadKey(); //Attente d'une entrée utilisateur avant de fermer
  67.         }
  68.  
  69.         public static void SetBlock(int x, int y)
  70.         {
  71.             map[x, y] = true;
  72.             Console.SetCursorPosition(x, y);
  73.             Console.BackgroundColor = ConsoleColor.White;
  74.             Console.Write(' ');
  75.             Console.BackgroundColor = ConsoleColor.Black;
  76.         }
  77.  
  78.         public static void SetPlayerPos(int x, int y)
  79.         {
  80.             if(playerX != -1)
  81.             {
  82.                 Console.SetCursorPosition(playerX, playerY);
  83.                 Console.Write(' ');
  84.             }
  85.  
  86.             playerX = x;
  87.             playerY = y;
  88.             Console.SetCursorPosition(playerX, playerY);
  89.             Console.BackgroundColor = ConsoleColor.Yellow;
  90.             Console.Write(' ');
  91.             Console.BackgroundColor = ConsoleColor.Black;
  92.         }
  93.        
  94.         public static void Move(Direction dir)
  95.         {
  96.             if(playerX != -1)
  97.             {
  98.                 memory[playerX, playerY] = true;
  99.                 moves.Push(dir);
  100.                 Console.SetCursorPosition(playerX, playerY);
  101.                 Console.BackgroundColor = ConsoleColor.Green;
  102.                 Console.Write(' ');
  103.             }
  104.  
  105.             if(dir == Direction.TOP)
  106.                 playerY--;
  107.             else if(dir == Direction.LEFT)
  108.                 playerX--;
  109.             else if(dir == Direction.BOTTOM)
  110.                 playerY++;
  111.             else if(dir == Direction.RIGHT)
  112.                 playerX++;
  113.  
  114.             Console.SetCursorPosition(playerX, playerY);
  115.             Console.BackgroundColor = ConsoleColor.Yellow;
  116.             Console.Write(' ');
  117.             Console.BackgroundColor = ConsoleColor.Black;
  118.         }
  119.        
  120.         public static void StepBack()
  121.         {
  122.             if(moves.Count > 0)
  123.             {
  124.                 memory[playerX, playerY] = true;
  125.                 Direction move = GetOpposite(moves.Pop());
  126.                
  127.                 Console.SetCursorPosition(playerX, playerY);
  128.                 Console.BackgroundColor = ConsoleColor.Green;
  129.                 Console.Write(' ');
  130.  
  131.                 if(move == Direction.TOP)
  132.                     playerY--;
  133.                 else if(move == Direction.LEFT)
  134.                     playerX--;
  135.                 else if(move == Direction.BOTTOM)
  136.                     playerY++;
  137.                 else if(move == Direction.RIGHT)
  138.                     playerX++;
  139.                
  140.                 Console.SetCursorPosition(playerX, playerY);
  141.                 Console.BackgroundColor = ConsoleColor.Yellow;
  142.                 Console.Write(' ');
  143.                 Console.BackgroundColor = ConsoleColor.Black;
  144.             }
  145.         }
  146.  
  147.         public static bool IsOptionAvailable()
  148.         {
  149.             return (playerY > 0 && !map[playerX, playerY - 1] && !memory[playerX, playerY - 1]) ||
  150.                    (playerX < SizeX - 1 && !map[playerX + 1, playerY] && !memory[playerX + 1, playerY]) ||
  151.                    (playerY < SizeY - 1 && !map[playerX, playerY + 1] && !memory[playerX, playerY + 1]) ||
  152.                    (playerX > 0 && !map[playerX - 1, playerY] && !memory[playerX - 1, playerY]);
  153.         }
  154.  
  155.         public static bool CanMove(Direction dir)
  156.         {
  157.             if(dir == Direction.TOP)
  158.                 return playerY > 0 && !map[playerX, playerY - 1];
  159.             else if(dir == Direction.RIGHT)
  160.                 return playerX < SizeX - 1 && !map[playerX + 1, playerY];
  161.             else if(dir == Direction.BOTTOM)
  162.                 return playerY < SizeY - 1 && !map[playerX, playerY + 1];
  163.             else if(dir == Direction.LEFT)
  164.                 return playerX > 0 && !map[playerX - 1, playerY];
  165.             else return false;
  166.         }
  167.        
  168.         public static List<Direction> ListPossibleMoves()
  169.         {
  170.             List<Direction> dirs = new List<Direction>();
  171.            
  172.             if(playerY > 0 && !map[playerX, playerY - 1] && !memory[playerX, playerY - 1])
  173.                 dirs.Add(Direction.TOP);
  174.             if(playerX < SizeX - 1 && !map[playerX + 1, playerY] && !memory[playerX + 1, playerY])
  175.                 dirs.Add(Direction.RIGHT);
  176.             if(playerY < SizeY - 1 && !map[playerX, playerY + 1] && !memory[playerX, playerY + 1])
  177.                 dirs.Add(Direction.BOTTOM);
  178.             if(playerX > 0 && !map[playerX - 1, playerY] && !memory[playerX - 1, playerY])
  179.                 dirs.Add(Direction.LEFT);
  180.  
  181.             return dirs;
  182.         }
  183.  
  184.         public static Direction GetOpposite(Direction dir)
  185.         {
  186.             if(dir == Direction.TOP)
  187.                 return Direction.BOTTOM;
  188.             else if(dir == Direction.RIGHT)
  189.                 return Direction.LEFT;
  190.             else if(dir == Direction.BOTTOM)
  191.                 return Direction.TOP;
  192.             else if(dir == Direction.LEFT)
  193.                 return Direction.RIGHT;
  194.             else throw new ArgumentException();
  195.         }
  196.     }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement