using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace _95.Aplikace
{
class Place
{
public bool moved = false;
public int type = 0;
}
class Player
{
public int x;
public int y;
public string model = "☺";
public Stack<string> path = new Stack<string>();
public Player(int x, int y)
{
this.x = x;
this.y = y;
}
public void Move(Place[,] map)
{
this.path.Push(this.x + "-" + this.y);
map[this.x, this.y].moved = true;
if (this.CanMove(map) == true)
{
for (int i = 0; i < 1; i++)
{
if (this.x != map.GetLength(0) - 1) if (map[this.x + 1, this.y].type != 1 && map[this.x + 1, this.y].moved == false) { this.x++; break; }
if (this.y != 0) if (map[this.x, this.y - 1].type != 1 && map[this.x, this.y - 1].moved == false) { this.y--; break; }
if (this.y != map.GetLength(1) - 1) if (map[this.x, this.y + 1].type != 1 && map[this.x, this.y + 1].moved == false) { this.y++; break; }
if (this.x != 0) if (map[this.x - 1, this.y].type != 1 && map[this.x - 1, this.y].moved == false) { this.x--; break; }
}
Program.Debug(map, this); System.Threading.Thread.Sleep(50);
}
else { this.GoBack(map); this.GoBack(map); }
}
public bool CanMove(Place[,] map)
{
if (this.x != map.GetLength(0) - 1) if (map[this.x + 1, this.y].type != 1 && map[this.x + 1, this.y].moved == false) return true;
if (this.y != 0) if (map[this.x, this.y - 1].type != 1 && map[this.x, this.y - 1].moved == false) return true;
if (this.y != map.GetLength(1) - 1) if (map[this.x, this.y + 1].type != 1 && map[this.x, this.y + 1].moved == false) return true;
if (this.x != 0) if (map[this.x - 1, this.y].type != 1 && map[this.x - 1, this.y].moved == false) return true;
return false;
}
public void GoBack(Place[,] map)
{
string coords = this.path.Pop();
this.x = Convert.ToInt16(coords.Split('-')[0]);
this.y = Convert.ToInt16(coords.Split('-')[1]);
Program.Debug(map, this);
System.Threading.Thread.Sleep(15);
}
}
class Program
{
public static void Debug(Place[,] map, Player player)
{
Console.SetCursorPosition(0, 0);
for (int i = 0; i < map.GetLength(0); i++)
{
for (int ii = 0; ii < map.GetLength(1); ii++)
{
if (map[i, ii].type == 1)
{
Console.BackgroundColor = ConsoleColor.Gray;
Console.Write(map[i, ii].type);
Console.BackgroundColor = ConsoleColor.Black;
}
else if (map[i, ii].type == 0) Console.Write(".");
else if (map[i, ii].type == 2)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.Write(map[i, ii].type);
Console.ForegroundColor = ConsoleColor.Gray;
}
else if (map[i, ii].type == 3)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Write(map[i, ii].type);
Console.ForegroundColor = ConsoleColor.Gray;
}
else Console.Write(map[i, ii].type);
}
Console.WriteLine();
}
Console.SetCursorPosition(player.y, player.x);
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write(player.model);
Console.ForegroundColor = ConsoleColor.Gray;
Console.SetCursorPosition(0, map.GetLength(0));
}
static void Main(string[] args)
{
Console.CursorVisible = false;
Stream stream = new FileStream(@"C:\Users\DragonCz\Desktop\Visual Studio Projects\Soubory\95. Aplikace - Maze.txt", FileMode.Open, FileAccess.Read);
StreamReader read = new StreamReader(stream);
int height = 1;
int width = 0;
string radek = "";
Player player = new Player(0,0);
width = read.ReadLine().Length;
while ((radek = read.ReadLine()) != null)
height++;
Place[,] map = new Place[height, width];
stream.Seek(0, 0);
for (int i = 0; i < map.GetLength(0); i++)
{
radek = read.ReadLine();
for (int ii = 0; ii < map.GetLength(1); ii++)
{
map[i, ii] = new Place();
map[i, ii].type = Convert.ToInt16(radek[ii] - '0');
if (map[i, ii].type == 2) { player = new Player(i, ii); }
}
}
while (true)
{
player.Move(map);
if (map[player.x, player.y].type == 3)
{
Console.SetCursorPosition(0, height);
Console.WriteLine("Cesta úspěšně nalezena, délka: " + player.path.Count);
break;
}
else if (player.path.Count == 0 && player.CanMove(map) == false)
{
Console.SetCursorPosition(0, height);
Console.WriteLine("Cesta nenalezena");
break;
}
}
int length = player.path.Count;
for (int i = 0; i < length; i++)
{
int a = Convert.ToInt16(player.path.Peek().Split('-')[1]);
int b = Convert.ToInt16(player.path.Pop().Split('-')[0]);
Console.SetCursorPosition(a, b);
Console.ForegroundColor = ConsoleColor.Red;
Console.BackgroundColor = ConsoleColor.Green;
Console.Write("■");
System.Threading.Thread.Sleep(25);
}
Console.SetCursorPosition(0, height);
Console.Read();
}
}
}