So, I am attempting to make a maze game for some coursework at college. I have managed to get a maze generation algorithm working which makes use of recursive backtracking. However, the issue with this algorithm is that is does not use separate spaces (I don’t know what else to call them) in the console to allow for space for a character to move within the maze.
Here is the code for the program which is written in C# and runs exclusively within console:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
namespace Prototype
{
internal class Program
{
static int N = 1;
static int S = 2;
static int E = 4;
static int W = 8;
static Dictionary<char, int> sides = new Dictionary<char, int>() { {'N', 1}, { 'S', 2 }, { 'E', 4 }, { 'W', 8 } };
static Dictionary<char, int> directionX = new Dictionary<char, int>() { { 'E', 1 }, { 'W', -1 }, { 'N', 0 }, { 'S', 0 } };
static Dictionary<char, int> directionY = new Dictionary<char, int>() { { 'E', 0 }, { 'W', 0 }, { 'N', -1 }, { 'S', 1 } };
static Dictionary<char, int> opposite = new Dictionary<char, int>() { { 'E', W }, { 'W', E }, { 'N', S }, { 'S', N } };
static Random rand = new Random();
static void RandomDirection(char[] array)
{
for (int i = array.Length - 1; i > 0; i--)
{
int j = rand.Next(i + 1);
char temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
static void MazeGen(int cx, int cy, int[,] grid)
{
char[] directions = new char[] { 'N', 'S', 'E', 'W' };
RandomDirection(directions);
for (int i = 0; i < directions.Length; i++)
{
char direction = directions[i];
int nx = cx + directionX[direction];
int ny = cy + directionY[direction];
if ((ny >= 0 && ny < grid.GetLength(0)) && (nx >= 0 && nx < grid.GetLength(1)) && grid[ny, nx] == 0)
{
grid[cy, cx] |= sides[direction];
grid[ny, nx] |= opposite[direction];
MazeGen(nx, ny, grid);
}
}
}
static string PrintMaze(int[,] grid, int startRow, int finishRow)
{
StringBuilder sb = new StringBuilder();
sb.Append(" ");
for (int i = 0; i < (grid.GetLength(1) * 2 - 1); i++)
{
sb.Append("_");
}
sb.AppendLine();
for (int i = 0; i < grid.GetLength(0); i++)
{
if (i != startRow)
{
sb.Append("|");
}
else
{
sb.Append(" ");
}
for (int j = 0; j < grid.GetLength(1); j++)
{
if ((grid[i, j] & S) != 0)
{
sb.Append(" ");
}
else
{
sb.Append("_");
}
if ((grid[i, j] & E) != 0)
{
if (((grid[i, j] | grid[i, j + 1]) & S) != 0)
{
sb.Append(" ");
}
else
{
sb.Append("_");
}
}
else
{
if (i == finishRow && j == grid.GetLength(0) - 1)
{
sb.Append(" ");
}
else
{
sb.Append("|");
}
}
}
sb.AppendLine();
}
return sb.ToString();
}
static void Main(string[] args)
{
Console.WriteLine("Enter the size of the maze: ");
int mazeSizeInput = int.Parse(Console.ReadLine());
int[,] grid = new int[mazeSizeInput, mazeSizeInput];
MazeGen(0, 0, grid);
int startRow = rand.Next(mazeSizeInput);
int finishRow = rand.Next(mazeSizeInput);
string maze = PrintMaze(grid, startRow, finishRow);
Console.WriteLine(maze);
Console.ReadKey();
}
}
}
This image represents what is displayed in console when the program is run. As you can see, the “_” characters used for the floor do not leave any room for another character representing the player to be displayed.
Is there anyway to display 2 ASCII characters within the same square in the console?
Maskd is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.