using System;
using System.Text;
namespace Memory_Maze
{
class Program
{
static char[,] map = new char[8, 8];
static Random random = new Random();
struct Point
{
public int x;
public int y;
}
static Point player;
static void showStartText()
{
Console.Write("~~~Memory Maze~~~\n\n");
Console.Write("How good is your short term memory? Time to put it to the test!\n\n");
Console.Write("Instructions: You will be shown a map. The 'S' is the start, and the 'F' is the end.\n\n");
Console.Write("You will be shown the maze for a few seconds, and then you must try to navigate from start to finish.\n\n");
Console.Write("To move in a direction, hit that arrow key. If you run into a wall, you lose.\n\n");
}
static void generateMap()
{
for (int x = 0; x < map.GetLength(0); ++x)
for (int y = 0; y < map.GetLength(1); ++y)
{
map[x, y] = 'W';
}
int tunnelX = 1;
int tunnelY = 1;
map[tunnelX, tunnelY] = 'S';
while (true)
{
if (random.Next(0, 2) == 1)
{
if (tunnelX + 1 < map.GetLength(0) - 1)
{
++tunnelX;
map[tunnelX, tunnelY] = ' ';
continue;
}
else
{
if (tunnelY + 1 < map.GetLength(1) - 1)
{
++tunnelY;
map[tunnelX, tunnelY] = ' ';
}
else
{
map[tunnelX, tunnelY] = 'F';
break;
}
}
}
else
{
if (tunnelY + 1 < map.GetLength(1) - 1)
{
++tunnelY;
map[tunnelX, tunnelY] = ' ';
continue;
}
else
{
if (tunnelX + 1 < map.GetLength(0) - 1)
{
++tunnelX;
map[tunnelX, tunnelY] = ' ';
}
else
{
map[tunnelX, tunnelY] = 'F';
break;
}
}
}
}
}
static void drawMap()
{
for (int x = map.GetLength(0) - 1; x >= 0; --x)
{
for (int y = 0; y < map.GetLength(1); ++y)
{
switch (map[x, y])
{
case 'W':
Console.ForegroundColor = ConsoleColor.Red;
break;
case 'P':
Console.ForegroundColor = ConsoleColor.Green;
break;
case 'S':
Console.ForegroundColor = ConsoleColor.Cyan;
break;
case 'F':
Console.ForegroundColor = ConsoleColor.Magenta;
break;
}
Console.Write(map[x, y]);
}
Console.Write("\n");
}
}
static bool quit()
{
while (true)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("Would you like to play again? y/n\n");
Console.ForegroundColor = ConsoleColor.Green;
string input = Console.ReadLine();
Console.ForegroundColor = ConsoleColor.Gray;
if (input.ToLowerInvariant() == "y")
return false;
else if (input.ToLowerInvariant() == "n")
return true;
else
{
Console.Write("\n\nInvalid input\n\n");
continue;
}
}
}
static bool movePlayer()
{
var key = Console.ReadKey();
if (key.Key == ConsoleKey.RightArrow)
{
++player.y;
if (map[player.x, player.y] == 'W')
return false;
else
return true;
}
else if (key.Key == ConsoleKey.UpArrow)
{
++player.x;
if (map[player.x, player.y] == 'W')
return false;
else
return true;
}
return true;
}
static bool won()
{
return map[player.x, player.y] == 'F';
}
static void Main(string[] args)
{
Program program = new Program();
showStartText();
Console.Write("Press enter to continue.");
Console.ReadLine();
Console.Clear();
while (true)
{
Console.Clear();
generateMap();
player.x = 1;
player.y = 1;
drawMap();
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write("\n\nSeconds left: 6");
Console.ForegroundColor = ConsoleColor.Green;
for (int i = 6; i >= 0; --i)
{
Console.Write("\b");
Console.Write(i);
System.Threading.Thread.Sleep(1000);
}
Console.ForegroundColor = ConsoleColor.Gray;
Console.Clear();
while (true)
{
Console.Clear();
Console.Write("Press an arrow key to go that direction\n\n");
if (movePlayer())
{
if (won())
{
Console.Write("\bCongratulations, you won!\n\n");
if (quit())
return;
else
break;
}
continue;
}
else
{
Console.Write("\bYou ran into a wall.\n\n");
if (quit())
return;
else
break;
}
}
}
}
}
}