This commit is contained in:
Tim Wundenberg
2018-05-26 09:13:22 +02:00

View File

@@ -1,11 +1,14 @@
using CoopSweeper.GameTypes; using CoopSweeper.GameTypes;
using System; using System;
using System.Text; using System.Text;
using System.Threading;
namespace CoopSweeper namespace CoopSweeper
{ {
class Program class Program
{ {
const int MAP_POS_X = 3;
const int MAP_POS_Y = 3;
static void Main(string[] args) static void Main(string[] args)
{ {
@@ -14,12 +17,26 @@ namespace CoopSweeper
Console.BackgroundColor = ConsoleColor.Black; Console.BackgroundColor = ConsoleColor.Black;
Console.Clear(); Console.Clear();
var game = new Game(); var game = new Game();
game.GenerateGame(15, 10, 15);
var cursorPosX = 0; var cursorPosX = 0;
var cursorPosY = 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);
Console.ReadKey();
StartNewGame(game, cursorPosX, cursorPosY);
};
ConsoleKey key = (ConsoleKey)(-1); ConsoleKey key = (ConsoleKey)(-1);
do do
{ {
var oldCursorPosX = cursorPosX;
var oldCursorPosY = cursorPosY;
bool fullRedraw = false;
switch(key) switch(key)
{ {
case ConsoleKey.UpArrow: case ConsoleKey.UpArrow:
@@ -31,16 +48,27 @@ namespace CoopSweeper
case ConsoleKey.RightArrow: case ConsoleKey.RightArrow:
cursorPosX++; break; cursorPosX++; break;
case ConsoleKey.Spacebar: case ConsoleKey.Spacebar:
fullRedraw = true;
game.Reveal(cursorPosX, cursorPosY); break; game.Reveal(cursorPosX, cursorPosY); break;
case ConsoleKey.F: case ConsoleKey.F:
fullRedraw = true;
game.ToggleMark(cursorPosX, cursorPosY); break; game.ToggleMark(cursorPosX, cursorPosY); break;
} }
if (cursorPosX < 0) cursorPosX = 0; if (cursorPosX < 0) cursorPosX = 0;
if (cursorPosY < 0) cursorPosY = 0; if (cursorPosY < 0) cursorPosY = 0;
if (cursorPosX >= game.Map.GetLength(0)) cursorPosX = game.Map.GetLength(0) - 1; 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 (cursorPosY >= game.Map.GetLength(1)) cursorPosY = game.Map.GetLength(1) - 1;
DrawBorder(2, 2, game.Map.GetLength(0) + 2, game.Map.GetLength(1) + 2); if (fullRedraw)
DrawMap(3, 3, game.Map, cursorPosX, cursorPosY); DrawMap(3, 3, game.Map, cursorPosX, cursorPosY);
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;
} while ((key = Console.ReadKey().Key) != ConsoleKey.Escape); } while ((key = Console.ReadKey().Key) != ConsoleKey.Escape);
} }
@@ -57,11 +85,16 @@ namespace CoopSweeper
c = ' '; c = ' ';
break; break;
case DisplayState.NONE: case DisplayState.NONE:
bgChanged = true;
fgChanged = true; fgChanged = true;
Console.ForegroundColor = ConsoleColor.Gray; Console.ForegroundColor = ConsoleColor.Black;
c = '◌'; Console.BackgroundColor = ConsoleColor.Gray;
//c = '◌';
c = ' ';
break; break;
case DisplayState.QUESTIONMARK: case DisplayState.QUESTIONMARK:
bgChanged = true;
Console.BackgroundColor = ConsoleColor.Gray;
c = '?'; c = '?';
break; break;
case DisplayState.BOMB: case DisplayState.BOMB:
@@ -74,7 +107,9 @@ namespace CoopSweeper
break; break;
case DisplayState.FLAG: case DisplayState.FLAG:
fgChanged = true; fgChanged = true;
Console.ForegroundColor = ConsoleColor.Cyan; bgChanged = true;
Console.BackgroundColor = ConsoleColor.Gray;
Console.ForegroundColor = ConsoleColor.DarkRed;
c = 'F'; c = 'F';
break; break;
case DisplayState.NUMBER1: case DisplayState.NUMBER1:
@@ -86,7 +121,24 @@ namespace CoopSweeper
case DisplayState.NUMBER7: case DisplayState.NUMBER7:
case DisplayState.NUMBER8: case DisplayState.NUMBER8:
fgChanged = true; fgChanged = true;
switch(state)
{
case DisplayState.NUMBER1:
Console.ForegroundColor = ConsoleColor.Green;
break;
case DisplayState.NUMBER2:
Console.ForegroundColor = ConsoleColor.Yellow; 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]; c = ((int)state).ToString()[0];
break; break;
} }
@@ -105,6 +157,15 @@ namespace CoopSweeper
Console.BackgroundColor = oldBg; 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);
}
private static void DrawMap(int posX, int posY, IField[,] map, int cursorX, int cursorY) private static void DrawMap(int posX, int posY, IField[,] map, int cursorX, int cursorY)
{ {
var res = new string[map.GetLength(1)]; var res = new string[map.GetLength(1)];