Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Threading;
- namespace GridTest
- {
- public class Program
- {
- public const int SizeX = 100;
- public const int SizeY = 20;
- public const int Delay = 100;
- 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];
- private static Stack<Direction> moves = new Stack<Direction>();
- 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) //Densité de 1 case remplie sur 5
- SetBlock(i, j);
- }
- }
- //On place le joueur dans la première case vide trouvée
- bool ready = false;
- for(int x = 0; x < SizeX; x++)
- {
- for(int y = 0; y < SizeY; y++)
- {
- if(!map[x, y])
- {
- SetPlayerPos(x, y);
- ready = true;
- }
- if(ready)
- break;
- }
- if(ready)
- break;
- }
- while(true)
- {
- List<Direction> m = ListPossibleMoves();
- if(m.Count > 0)
- Move(m[random.Next(m.Count)]);
- else if(moves.Count > 0)
- StepBack();
- else break;
- Thread.Sleep(Delay);
- }
- Console.ReadKey(); //Attente d'une entrée utilisateur avant de fermer
- }
- 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 void Move(Direction dir)
- {
- if(playerX != -1)
- {
- memory[playerX, playerY] = true;
- moves.Push(dir);
- Console.SetCursorPosition(playerX, playerY);
- Console.BackgroundColor = ConsoleColor.Green;
- Console.Write(' ');
- }
- if(dir == Direction.TOP)
- playerY--;
- else if(dir == Direction.LEFT)
- playerX--;
- else if(dir == Direction.BOTTOM)
- playerY++;
- else if(dir == Direction.RIGHT)
- playerX++;
- Console.SetCursorPosition(playerX, playerY);
- Console.BackgroundColor = ConsoleColor.Yellow;
- Console.Write(' ');
- Console.BackgroundColor = ConsoleColor.Black;
- }
- public static void StepBack()
- {
- if(moves.Count > 0)
- {
- memory[playerX, playerY] = true;
- Direction move = GetOpposite(moves.Pop());
- Console.SetCursorPosition(playerX, playerY);
- Console.BackgroundColor = ConsoleColor.Green;
- Console.Write(' ');
- if(move == Direction.TOP)
- playerY--;
- else if(move == Direction.LEFT)
- playerX--;
- else if(move == Direction.BOTTOM)
- playerY++;
- else if(move == Direction.RIGHT)
- playerX++;
- Console.SetCursorPosition(playerX, playerY);
- Console.BackgroundColor = ConsoleColor.Yellow;
- Console.Write(' ');
- Console.BackgroundColor = ConsoleColor.Black;
- }
- }
- public static bool IsOptionAvailable()
- {
- return (playerY > 0 && !map[playerX, playerY - 1] && !memory[playerX, playerY - 1]) ||
- (playerX < SizeX - 1 && !map[playerX + 1, playerY] && !memory[playerX + 1, playerY]) ||
- (playerY < SizeY - 1 && !map[playerX, playerY + 1] && !memory[playerX, playerY + 1]) ||
- (playerX > 0 && !map[playerX - 1, playerY] && !memory[playerX - 1, playerY]);
- }
- 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 List<Direction> ListPossibleMoves()
- {
- List<Direction> dirs = new List<Direction>();
- if(playerY > 0 && !map[playerX, playerY - 1] && !memory[playerX, playerY - 1])
- dirs.Add(Direction.TOP);
- if(playerX < SizeX - 1 && !map[playerX + 1, playerY] && !memory[playerX + 1, playerY])
- dirs.Add(Direction.RIGHT);
- if(playerY < SizeY - 1 && !map[playerX, playerY + 1] && !memory[playerX, playerY + 1])
- dirs.Add(Direction.BOTTOM);
- if(playerX > 0 && !map[playerX - 1, playerY] && !memory[playerX - 1, playerY])
- dirs.Add(Direction.LEFT);
- return dirs;
- }
- public static Direction GetOpposite(Direction dir)
- {
- if(dir == Direction.TOP)
- return Direction.BOTTOM;
- else if(dir == Direction.RIGHT)
- return Direction.LEFT;
- else if(dir == Direction.BOTTOM)
- return Direction.TOP;
- else if(dir == Direction.LEFT)
- return Direction.RIGHT;
- else throw new ArgumentException();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement