I am trying to create a maze game, I'm using arrays to house my maze and while it was working fine a couple of test runs ago, VS is now giving me a null reference exception. I don't know how my array is null, and I could use all the help I can get. The specific place I get the null reference exception is in my DisplayMaze method. I decided to just give all of my code in case that helps.
class Maze
{
private string[] mazeMap;
private int step = 0;
private int playerx =1;
private int playery =1;
public int GetPlayerX()
{
return playerx;
}
public int GetPlayerY()
{
return playery;
}
public void SetPlayerX(int x)
{
playerx = x;
}
public void SetPlayerY(int y)
{
playery = y;
}
public void SetMaze(string[] map)
{
mazeMap = map;
}
public string[] GetMaze()
{
return mazeMap;
}
public int[] PlayerLocation()
{
int[] location = new int[] { playerx, playery };
return location;
}
public void DisplayMaze()
{
string[] map = GetMaze();
for (int x = 0; x < map.Length; x++)
{
Console.WriteLine(map[x]);
if (map[x] == map[playerx] && map[x] == map[playery])
{
Console.WriteLine("O");
}
}
}
public void QuitGame()
{
string answer;
Console.WriteLine("Are you sure you want to quit? Type Y - Yes or N - No");
answer = Console.ReadLine();
if (answer == "Y")
{
System.Environment.Exit(1);
}
else if (answer == "N")
{
}
else
Console.WriteLine("Please enter Y or N");
}
public void TitleScreen()
{
string[] map = title;
for (int x = 0; x < map.Length; x++)
{
Console.WriteLine(map[x]);
}
Console.WriteLine("Press ENTER to start");
Console.WriteLine("Quit");
ConsoleKey ckj;
ckj = Console.ReadKey(true).Key;
switch (ckj)
{
case ConsoleKey.Enter:
DisplayMaze();
break;
case ConsoleKey.Escape:
QuitGame();
break;
}
}
public void KeyMovement()
{
ConsoleKey cki;
cki = Console.ReadKey(true).Key;
switch (cki)
{
case ConsoleKey.UpArrow:
{
int[] location = PlayerLocation();
string[] maze = GetMaze();
if (maze[playery + 1].Equals(" "))
{
location[1] = location[1] + 1;
SetPlayerY(GetPlayerY() + 1);
step++;
}
else
Console.WriteLine("Invalid Move");
break;
}
case ConsoleKey.DownArrow:
{
int[] location = PlayerLocation();
string[] maze = GetMaze();
if (maze[playery - 1].Equals(" "))
{
location[1] = location[1] - 1;
SetPlayerY(GetPlayerY() - 1);
step++;
}
else
Console.WriteLine("Invalid Move");
break;
}
case ConsoleKey.LeftArrow:
{
int[] location = PlayerLocation();
string[] maze = GetMaze();
if (maze[playerx - 1].Equals(" "))
{
location[0] = location[0] - 1;
SetPlayerX(GetPlayerX() - 1);
step++;
}
else
Console.WriteLine("Invalid Move");
break;
}
case ConsoleKey.RightArrow:
{
int[] location = PlayerLocation();
string[] maze = GetMaze();
if (maze[playerx + 1].Equals(" "))
{
location[0] = location[0] + 1;
SetPlayerX(GetPlayerX() + 1);
step++;
}
else
Console.WriteLine("Invalid Move");
break;
}
}
}
}
public static void Main(string[] args)
{
Maze game = new Maze();
game.TitleScreen();
}
}
}