152 lines
5.8 KiB
C#
152 lines
5.8 KiB
C#
using CoopSweeper.GameTypes;
|
|
using System;
|
|
using System.Text;
|
|
|
|
namespace CoopSweeper
|
|
{
|
|
class Program
|
|
{
|
|
|
|
static void Main(string[] args)
|
|
{
|
|
Console.OutputEncoding = Encoding.UTF8;
|
|
Console.ForegroundColor = ConsoleColor.White;
|
|
Console.BackgroundColor = ConsoleColor.Black;
|
|
Console.Clear();
|
|
var game = new Game();
|
|
game.GenerateGame(15, 10, 15);
|
|
var cursorPosX = 0;
|
|
var cursorPosY = 0;
|
|
ConsoleKey key = (ConsoleKey)(-1);
|
|
do
|
|
{
|
|
switch(key)
|
|
{
|
|
case ConsoleKey.UpArrow:
|
|
cursorPosY--; break;
|
|
case ConsoleKey.DownArrow:
|
|
cursorPosY++; break;
|
|
case ConsoleKey.LeftArrow:
|
|
cursorPosX--; break;
|
|
case ConsoleKey.RightArrow:
|
|
cursorPosX++; break;
|
|
case ConsoleKey.Spacebar:
|
|
game.Reveal(cursorPosX, cursorPosY); break;
|
|
case ConsoleKey.F:
|
|
game.SetFlag(cursorPosX, cursorPosY); break;
|
|
}
|
|
if (cursorPosX < 0) cursorPosX = 0;
|
|
if (cursorPosY < 0) cursorPosY = 0;
|
|
if (cursorPosX >= game.Map.GetLength(0)) cursorPosX = game.Map.GetLength(0) - 1;
|
|
if (cursorPosY >= game.Map.GetLength(1)) cursorPosY = game.Map.GetLength(1) - 1;
|
|
DrawBorder(2, 2, game.Map.GetLength(0) + 2, game.Map.GetLength(1) + 2);
|
|
DrawMap(3, 3, game.Map, cursorPosX, cursorPosY);
|
|
} while ((key = Console.ReadKey().Key) != ConsoleKey.Escape);
|
|
}
|
|
|
|
private static void DrawChar(IField f, bool isCursor)
|
|
{
|
|
var oldBg = Console.BackgroundColor;
|
|
var oldFg = Console.ForegroundColor;
|
|
bool fgChanged = false;
|
|
bool bgChanged = false;
|
|
var c = 'E';
|
|
var state = f.GetDisplayState();
|
|
switch (state) {
|
|
case DisplayState.EMPTY:
|
|
c = ' ';
|
|
break;
|
|
case DisplayState.NONE:
|
|
fgChanged = true;
|
|
Console.ForegroundColor = ConsoleColor.Gray;
|
|
c = '◌';
|
|
break;
|
|
case DisplayState.QUESTIONMARK:
|
|
c = '?';
|
|
break;
|
|
case DisplayState.BOMB:
|
|
fgChanged = true;
|
|
Console.ForegroundColor = ConsoleColor.Red;
|
|
c = '☼';
|
|
break;
|
|
case DisplayState.ERROR:
|
|
c = 'e';
|
|
break;
|
|
case DisplayState.FLAG:
|
|
fgChanged = true;
|
|
Console.ForegroundColor = ConsoleColor.Cyan;
|
|
c = 'F';
|
|
break;
|
|
case DisplayState.NUMBER1:
|
|
case DisplayState.NUMBER2:
|
|
case DisplayState.NUMBER3:
|
|
case DisplayState.NUMBER4:
|
|
case DisplayState.NUMBER5:
|
|
case DisplayState.NUMBER6:
|
|
case DisplayState.NUMBER7:
|
|
case DisplayState.NUMBER8:
|
|
fgChanged = true;
|
|
Console.ForegroundColor = ConsoleColor.Yellow;
|
|
c = state.ToString()[0];
|
|
break;
|
|
}
|
|
if (isCursor)
|
|
{
|
|
fgChanged = true;
|
|
bgChanged = true;
|
|
var s = Console.ForegroundColor;
|
|
Console.ForegroundColor = Console.BackgroundColor;
|
|
Console.BackgroundColor = s;
|
|
}
|
|
Console.Write(c);
|
|
if (fgChanged)
|
|
Console.ForegroundColor = oldFg;
|
|
if (bgChanged)
|
|
Console.BackgroundColor = oldBg;
|
|
}
|
|
|
|
private static void DrawMap(int posX, int posY, IField[,] map, int cursorX, int cursorY)
|
|
{
|
|
var res = new string[map.GetLength(1)];
|
|
for (var y = 0; y < map.GetLength(1); y++)
|
|
{
|
|
Console.SetCursorPosition(posX, posY + y);
|
|
for (var x = 0; x < map.GetLength(0); x++)
|
|
{
|
|
DrawChar(map[x, y], x == cursorX && y == cursorY);
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void DrawBorder(int posX, int posY, int width, int height)
|
|
{
|
|
Console.SetCursorPosition(posX, posY);
|
|
var linebuilder = new StringBuilder();
|
|
linebuilder.Append("╔");
|
|
for (var x = 0; x < width - 2; x++)
|
|
linebuilder.Append("═");
|
|
linebuilder.Append("╗");
|
|
Console.Write(linebuilder);
|
|
for (var y = 1; y < height - 1; y++)
|
|
{
|
|
Console.SetCursorPosition(posX, posY + y);
|
|
Console.Write("║");
|
|
Console.SetCursorPosition(posX + width - 1, posY + y);
|
|
Console.Write("║");
|
|
}
|
|
Console.SetCursorPosition(posX, posY + height - 1);
|
|
linebuilder = new StringBuilder();
|
|
linebuilder.Append("╚");
|
|
for (var x = 0; x < width - 2; x++)
|
|
linebuilder.Append("═");
|
|
linebuilder.Append("╝");
|
|
Console.Write(linebuilder);
|
|
// ┌──┬──┐ ╔══╦══╗ ╒══╤══╕ ╓──╥──╖
|
|
// │ │ │ ║ ║ ║ │ │ │ ║ ║ ║
|
|
// ├──┼──┤ ╠══╬══╣ ╞══╪══╡ ╟──╫──╢
|
|
// │ │ │ ║ ║ ║ │ │ │ ║ ║ ║
|
|
// └──┴──┘ ╚══╩══╝ ╘══╧══╛ ╙──╨──╜
|
|
}
|
|
}
|
|
}
|