From c82c40a577635024ce4b7acbb2f372a4df46b8f6 Mon Sep 17 00:00:00 2001 From: Marvin Rohrbach Date: Fri, 25 May 2018 10:47:19 +0200 Subject: [PATCH] Add basic rendering and cursor --- CoopSweeper/GameTypes/Field.cs | 12 +++++- CoopSweeper/Program.cs | 78 ++++++++++++++++++++++------------ 2 files changed, 61 insertions(+), 29 deletions(-) diff --git a/CoopSweeper/GameTypes/Field.cs b/CoopSweeper/GameTypes/Field.cs index ac69500..e32129a 100644 --- a/CoopSweeper/GameTypes/Field.cs +++ b/CoopSweeper/GameTypes/Field.cs @@ -18,7 +18,17 @@ namespace CoopSweeper.GameTypes public char ToChar() { - throw new NotImplementedException(); + switch(State) + { + case FieldState.FLAG: return 'F'; + case FieldState.NONE: return '◌'; + case FieldState.QUESTIONMARK: return '?'; + case FieldState.REVEALED: + if(ContainsBomb) + return '☼'; + return ' '; + } + return 'E'; } } } diff --git a/CoopSweeper/Program.cs b/CoopSweeper/Program.cs index 1fde0b9..3c57702 100644 --- a/CoopSweeper/Program.cs +++ b/CoopSweeper/Program.cs @@ -1,44 +1,66 @@ -using System; +using CoopSweeper.GameTypes; +using System; using System.Text; namespace CoopSweeper { class Program { + static void Main(string[] args) { - string[,] map = new string[,] { - {"X"," ","X"," "}, - {"X"," ","X"," "}, - {"X"," ","X"," "}, - {"X"," ","X"," "} - }; - DrawBorder(2, 2, map.GetLength(0) + 2, map.GetLength(1) + 2); - DrawMap(3,3,RenderMap(map)); - Console.ReadKey(); + var game = new Game(); + 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; + } + game.GenerateGame(15, 10, 15); + 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 string[] RenderMap(string[,] map) + private static void DrawChar(IField f, bool isCursor) + { + var oldBg = Console.BackgroundColor; + var oldFg = Console.ForegroundColor; + if(isCursor) + { + var c = Console.ForegroundColor; + Console.ForegroundColor = Console.BackgroundColor; + Console.BackgroundColor = c; + } + Console.Write(f.ToChar()); + if (isCursor) + { + var c = Console.ForegroundColor; + Console.ForegroundColor = Console.BackgroundColor; + Console.BackgroundColor = c; + } + } + + 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++) - { - var linebuilder = new StringBuilder(); - for (var x = 0; x < map.GetLength(0); x++) - { - linebuilder.Append(map[x, y]); - } - res[y] = linebuilder.ToString(); - } - return res; - } - - private static void DrawMap(int posX, int posY, string[] mapLines) - { - for(var y = 0; y < mapLines.Length; y++) + for (var y = 0; y < map.GetLength(1); y++) { Console.SetCursorPosition(posX, posY + y); - Console.Write(mapLines[y]); + for (var x = 0; x < map.GetLength(0); x++) + { + DrawChar(map[x, y], x == cursorX && y == cursorY); + } } } @@ -51,7 +73,7 @@ namespace CoopSweeper linebuilder.Append("═"); linebuilder.Append("╗"); Console.Write(linebuilder); - for(var y = 1; y < height - 1; y++) + for (var y = 1; y < height - 1; y++) { Console.SetCursorPosition(posX, posY + y); Console.Write("║");