using CoopSweeper.GameTypes; using System; using System.Text; using System.Threading; namespace CoopSweeper { class Program { static IField[,] currentMap = null; const int MAP_POS_X = 3; const int MAP_POS_Y = 3; static void Main(string[] args) { Console.OutputEncoding = Encoding.UTF8; Console.ForegroundColor = ConsoleColor.White; Console.BackgroundColor = ConsoleColor.Black; Console.Clear(); var game = new Game(); var cursorPosX = 0; var cursorPosY = 0; StartNewGame(game, cursorPosX, cursorPosY); game.GameFinished += won => { Console.SetCursorPosition(0, 0); if (won) Console.Write("You won!"); else Console.Write("You lost!"); DrawMap(MAP_POS_X, MAP_POS_Y, game.Map, cursorPosX, cursorPosY, true); Console.ReadKey(); StartNewGame(game, cursorPosX, cursorPosY); }; ConsoleKey key = (ConsoleKey)(-1); while ((key = Console.ReadKey().Key) != ConsoleKey.Escape) { var oldCursorPosX = cursorPosX; var oldCursorPosY = cursorPosY; bool fullRedraw = false; 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: fullRedraw = true; game.Reveal(cursorPosX, cursorPosY); break; case ConsoleKey.F: fullRedraw = true; game.ToggleMark(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; if (fullRedraw) DrawMap(3, 3, game.Map, cursorPosX, cursorPosY, true); else if(oldCursorPosX != cursorPosX || oldCursorPosY != cursorPosY) { Console.SetCursorPosition(oldCursorPosX + 3, oldCursorPosY + 3); DrawChar(game.Map[oldCursorPosX, oldCursorPosY], false); Console.SetCursorPosition(cursorPosX + 3, cursorPosY + 3); DrawChar(game.Map[cursorPosX, cursorPosY], true); } Console.SetCursorPosition(0, 0); Console.CursorVisible = false; } } 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.DisplayState; switch (state) { case DisplayState.EMPTY: c = ' '; break; case DisplayState.NONE: bgChanged = true; fgChanged = true; Console.ForegroundColor = ConsoleColor.Black; Console.BackgroundColor = ConsoleColor.Gray; //c = '◌'; c = ' '; break; case DisplayState.QUESTIONMARK: fgChanged = true; bgChanged = true; Console.BackgroundColor = ConsoleColor.Gray; Console.ForegroundColor = ConsoleColor.DarkBlue; c = '?'; break; case DisplayState.BOMB: fgChanged = true; Console.ForegroundColor = ConsoleColor.Red; c = '☼'; break; case DisplayState.ERROR: c = 'e'; break; case DisplayState.FLAG: fgChanged = true; bgChanged = true; Console.BackgroundColor = ConsoleColor.Gray; Console.ForegroundColor = ConsoleColor.DarkRed; 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; switch(state) { case DisplayState.NUMBER1: Console.ForegroundColor = ConsoleColor.Green; break; case DisplayState.NUMBER2: Console.ForegroundColor = ConsoleColor.Yellow; break; case DisplayState.NUMBER3: Console.ForegroundColor = ConsoleColor.DarkYellow; break; case DisplayState.NUMBER4: Console.ForegroundColor = ConsoleColor.DarkRed; break; default: Console.ForegroundColor = ConsoleColor.Red; break; } c = ((int)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; } public static void StartNewGame(Game game, int cursorPosX, int cursorPosY) { game.GenerateGame(100, 30, 15); Console.Clear(); Console.SetCursorPosition(0, 0); DrawBorder(MAP_POS_X - 1, MAP_POS_X - 1, game.Map.GetLength(0) + 2, game.Map.GetLength(1) + 2); DrawMap(MAP_POS_X, MAP_POS_Y, game.Map, cursorPosX, cursorPosY, false); } private static void DrawMap(int posX, int posY, IField[,] map, int cursorX, int cursorY, bool doChangeDetection) { for (var y = 0; y < map.GetLength(1); y++) { if(currentMap == null || !doChangeDetection) Console.SetCursorPosition(posX, posY + y); for (var x = 0; x < map.GetLength(0); x++) { if (currentMap != null && doChangeDetection) { var currentField = currentMap[x, y]; var newField = map[x, y]; if (currentField == null || currentField.DisplayState != newField.DisplayState) { Console.SetCursorPosition(posX + x, posY + y); DrawChar(map[x, y], x == cursorX && y == cursorY); } } else { DrawChar(map[x, y], x == cursorX && y == cursorY); } } } currentMap = ArrayHelpers.Clone2D(map); } 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); // ┌──┬──┐ ╔══╦══╗ ╒══╤══╕ ╓──╥──╖ // │ │ │ ║ ║ ║ │ │ │ ║ ║ ║ // ├──┼──┤ ╠══╬══╣ ╞══╪══╡ ╟──╫──╢ // │ │ │ ║ ║ ║ │ │ │ ║ ║ ║ // └──┴──┘ ╚══╩══╝ ╘══╧══╛ ╙──╨──╜ } } }