using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace maze
{
class move
{
public string xyz;
int direction;
public move()
{
}
public move(string[,] a, int[] b, int c)
{
string xzy = a[b[0], b[1]];
direction = c;
}
public int[] up(int[] x)
{
int[] temp = new int[2];
temp[0] = x[0] - 1;
temp[1] = x[1];
return temp;
}
public int[] down(int[] x)
{
int[] temp = new int[2];
temp[0] = x[0] + 1;
temp[1] = x[1];
return temp;
}
public int[] right(int[] x)
{
int[] temp = new int[2];
temp[0] = x[0];
temp[1] = x[1]+1;
return temp;
}
public int[] left(int[] x)
{
int[] temp = new int[2];
temp[0] = x[0];
temp[1] = x[1] - 1;
return temp;
}
public int turn(int x)
{
if (x == 1) //up
{
return 2; //right
}
else if (x == 2)
{
return 3; //down
}
else if (x == 3)
{
return 4; //left
}
else
{
return 1;
}
}
public int reverseturn(int x)
{
if (x == 1) //up
{
return 4; //left
}
else if (x == 2)
{
return 1; //down
}
else if (x == 3)
{
return 2; //left
}
else
{
return 3;
}
}
public string lookindirection(int x, string[,] y, int[] z)
{
int[] lookposition = new int[2];
if (x == 1)
{
lookposition = up(z);
}
else if (x == 2)
{
lookposition = right(z);
}
else if (x == 3)
{
lookposition = down(z);
}
else
{
lookposition = left(z);
}
return y[lookposition[0], lookposition[1]];
}
public int[] step(int x, int[] y)
{
if (x == 1)
{
return up(y);
}
else if (x == 2)
{
return right(y);
}
else if (x == 3)
{
return down(y);
}
else
{
return left(y);
}
}
public int[] wall(int x, int[] y)
{
if (x == 2) //wall is alway on left hand side
{
return up(y);
}
else if (x == 3)
{
return right(y);
}
else if (x == 4)
{
return down(y);
}
else
{
return left(y);
}
}
}
class Program
{
static void Main(string[] args)
{
/*string[,] maze ={{"#","#","#","#","#","#","#","#","#","#","#","#"},
{"#"," "," "," ","#"," "," "," "," "," "," ","#"},
{" "," ","#"," ","#"," ","#","#","#","#"," ","#"},
{"#","#","#"," ","#"," "," "," "," ","#"," ","#"},
{"#"," "," "," "," ","#","#","#"," ","#"," "," "},
{"#","#","#","#"," ","#"," ","#"," ","#"," ","#"},
{"#"," "," ","#"," ","#"," ","#"," ","#"," ","#"},
{"#","#"," ","#"," ","#"," ","#"," ","#"," ","#"},
{"#"," "," "," "," "," "," "," "," ","#"," ","#"},
{"#","#","#","#","#","#"," ","#","#","#"," ","#"},
{"#"," "," "," "," "," "," ","#"," "," "," ","#"},
{"#","#","#","#","#","#","#","#","#","#","#","#"}};*/
string[,] maze ={{"#","#","#","#","#","#","#","#","#","#","#","#"},
{"#","#","#"," "," "," "," "," ","#","#","#","#"},
{"#"," "," "," ","#"," ","#","#","#","#","#","#"},
{"#"," ","#"," "," "," ","#"," "," "," "," ","#"},
{" "," ","#","#","#","#","#"," ","#","#"," ","#"},
{"#"," ","#"," "," "," ","#"," "," "," "," "," "},
{"#"," ","#"," "," "," ","#","#","#","#"," ","#"},
{"#"," "," "," "," "," ","#","#","#","#"," ","#"},
{"#"," "," ","#","#","#","#","#","#","#"," ","#"},
{"#","#"," ","#"," "," ","#","#","#","#"," ","#"},
{"#"," "," ","#","#","#","#","#","#","#"," ","#"},
{"#"," "," ","#","#"," "," "," "," ","#"," ","#"},
{"#"," ","#"," ","#"," ","#","#"," ","#"," ","#"},
{"#"," ","#"," ","#"," "," ","#"," ","#"," ","#"},
{"#"," ","#"," ","#","#"," ","#"," ","#"," ","#"},
{"#"," ","#"," "," "," "," ","#"," ","#"," ","#"},
{"#"," ","#","#","#","#","#","#"," "," "," ","#"},
{"#"," "," "," "," "," "," "," "," ","#"," ","#"},
{"#","#","#","#","#","#","#","#","#","#","#","#"}};
for (int i = 0; i < maze.GetLength(0); i++)
{
for (int j = 0; j < maze.GetLength(1); j++)
{
Console.Write(maze[i, j]);
}
Console.WriteLine();
}
int[] begin = new int[] { 4, 1 };
//Console.Write(maze[2, 0]); maze[row,colume]
int direction = 1; //1 up, 2 right, 3 down, 4 left
pathfinder(ref maze, begin, direction);
for (int i = 0; i < maze.GetLength(0); i++)
{
for (int j = 0; j < maze.GetLength(1); j++)
{
Console.Write(maze[i, j]);
}
Console.WriteLine();
}
Console.Read();
}
static void pathfinder(ref string[,] x, int[] y, int z) //maze, start position, start direction
{
Console.WriteLine(y[0] + " " + y[1]+" "+z);
move go = new move();
int direction = z;
int[] position = y, wallposition;
int[] exit = new int[]{5,11};
if (x[position[0], position[1]] == "*")
{
x[position[0], position[1]] = "o";
}
else
{
x[position[0], position[1]] = "*";
}
if(position[0]==exit[0] && position[1]==exit[1]){return;}
if (go.lookindirection(direction, x, position) == "#") //if hit wall
{
wallposition = go.wall(direction, position);
if (x[wallposition[0], wallposition[1]] == "#")
{
direction = go.turn(direction);
if (go.lookindirection(direction, x, position) == "#")//deadend
{
direction = go.turn(direction);
position = go.step(direction, position); //uturn
}
}
else
{
direction = go.reverseturn(direction); //90 turn
position = go.step(direction, position);
}
}
else
{
wallposition = go.wall(direction, position);//not blocked by wall
if (go.lookindirection(direction, x, wallposition) == "#")//if 1 step ahead, wall is on left
{
position = go.step(direction, position);//go straight
}
else
{
position = go.step(direction, wallposition);//if wall turns 90, follow wall turn 90
direction = go.reverseturn(direction);
}
}
pathfinder(ref x, position, direction);
}
}
}
No comments:
Post a Comment