Вот код программы, написанной на C# и работающей исключительно в консоли:
Код: Выделить всё
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 sides = new Dictionary() { {'N', 1}, { 'S', 2 }, { 'E', 4 }, { 'W', 8 } };
static Dictionary directionX = new Dictionary() { { 'E', 1 }, { 'W', -1 }, { 'N', 0 }, { 'S', 0 } };
static Dictionary directionY = new Dictionary() { { 'E', 0 }, { 'W', 0 }, { 'N', -1 }, { 'S', 1 } };
static Dictionary opposite = new Dictionary() { { '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();
}
}
}
Можно ли как-нибудь отобразить 2 символа ASCII в одном квадрате в консоли?< /п>
Подробнее здесь: https://stackoverflow.com/questions/786 ... in-console