Add new Views system

This commit is contained in:
Marvin Rohrbach
2018-12-16 11:47:26 +01:00
parent 624f6d1ea6
commit 35e3010a33
9 changed files with 338 additions and 191 deletions

View File

@@ -6,7 +6,7 @@ namespace CoopSweeper
{ {
public static class ArrayHelpers public static class ArrayHelpers
{ {
public static T[,] Clone2D<T>(T[,] data) where T : ICloneable<T> public static T[,] Clone2D<T>(T[,] data) where T : class, ICloneable<T>
{ {
if (data == null) if (data == null)
return null; return null;

View File

@@ -5,10 +5,6 @@
<TargetFramework>netcoreapp2.0</TargetFramework> <TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<Folder Include="View\" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="ReadLine" Version="2.0.0" /> <PackageReference Include="ReadLine" Version="2.0.0" />
</ItemGroup> </ItemGroup>

View File

@@ -5,7 +5,7 @@ using System.Text;
namespace CoopSweeper.GameTypes namespace CoopSweeper.GameTypes
{ {
class Game public class Game
{ {
private readonly Random _random = new Random(); private readonly Random _random = new Random();
private static int _checkID = 0; private static int _checkID = 0;

View File

@@ -4,7 +4,7 @@ using System.Text;
namespace CoopSweeper namespace CoopSweeper
{ {
public interface ICloneable<T> where T : ICloneable<T> public interface ICloneable<T> where T : class, ICloneable<T>
{ {
T Clone(); T Clone();
} }

View File

@@ -1,5 +1,7 @@
using CoopSweeper.GameTypes; using CoopSweeper.GameTypes;
using CoopSweeper.View;
using System; using System;
using System.Collections.Generic;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
@@ -7,20 +9,23 @@ namespace CoopSweeper
{ {
class Program class Program
{ {
static IField[,] currentMap = null;
const int MAP_POS_X = 3; const int MAP_POS_X = 3;
const int MAP_POS_Y = 3; const int MAP_POS_Y = 3;
static List<IView> _views = new List<IView>();
static void Main(string[] args) static void Main(string[] args)
{ {
Console.OutputEncoding = Encoding.UTF8; Console.OutputEncoding = Encoding.UTF8;
Console.ForegroundColor = ConsoleColor.White; Console.ForegroundColor = ConsoleColor.White;
Console.BackgroundColor = ConsoleColor.Black; Console.BackgroundColor = ConsoleColor.Black;
Console.SetCursorPosition(0, 0);
Console.CursorVisible = false;
Console.Clear(); Console.Clear();
var game = new Game(); var game = new Game();
var cursorPosX = 0; _views.Add(new GameView(game) { Position = new Position(1, 3) });
var cursorPosY = 0; _views.Add(new MenuBarView() { Position = new Position(0, 2) });
StartNewGame(game, cursorPosX, cursorPosY); StartNewGame(game);
game.GameFinished += won => game.GameFinished += won =>
{ {
Console.SetCursorPosition(0, 0); Console.SetCursorPosition(0, 0);
@@ -28,201 +33,40 @@ namespace CoopSweeper
Console.Write("You won!"); Console.Write("You won!");
else else
Console.Write("You lost!"); Console.Write("You lost!");
DrawMap(MAP_POS_X, MAP_POS_Y, game.Map, cursorPosX, cursorPosY, true); Draw(false);
Console.ReadKey(); Console.ReadKey(true);
StartNewGame(game, cursorPosX, cursorPosY); StartNewGame(game);
}; };
ConsoleKey key = (ConsoleKey)(-1); ConsoleKey key = (ConsoleKey)(-1);
while ((key = Console.ReadKey().Key) != ConsoleKey.Escape) Draw(true);
while ((key = Console.ReadKey(true).Key) != ConsoleKey.Escape)
{ {
var oldCursorPosX = cursorPosX; HandleKeyEvent(key);
var oldCursorPosY = cursorPosY; Draw(false);
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.SetCursorPosition(0, 0);
Console.CursorVisible = false; Console.CursorVisible = false;
} }
} }
private static void DrawChar(IField f, bool isCursor) private static void Draw(bool fullRedraw)
{ {
var oldBg = Console.BackgroundColor; foreach(var view in _views)
var oldFg = Console.ForegroundColor; view.Draw(fullRedraw);
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) private static void HandleKeyEvent(ConsoleKey key)
{
foreach(var view in _views)
if (view.HandleKeyEvent(key))
break;
}
public static void StartNewGame(Game game)
{ {
game.GenerateGame(100, 30, 15); game.GenerateGame(100, 30, 15);
Console.Clear(); Console.Clear();
Console.SetCursorPosition(0, 0); Console.SetCursorPosition(0, 0);
DrawBorder(MAP_POS_X - 1, MAP_POS_X - 1, game.Map.GetLength(0) + 2, game.Map.GetLength(1) + 2); Draw(true);
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);
// ┌──┬──┐ ╔══╦══╗ ╒══╤══╕ ╓──╥──╖
// │ │ │ ║ ║ ║ │ │ │ ║ ║ ║
// ├──┼──┤ ╠══╬══╣ ╞══╪══╡ ╟──╫──╢
// │ │ │ ║ ║ ║ │ │ │ ║ ║ ║
// └──┴──┘ ╚══╩══╝ ╘══╧══╛ ╙──╨──╜
} }
} }
} }

View File

@@ -0,0 +1,225 @@
using CoopSweeper.GameTypes;
using System;
using System.Collections.Generic;
using System.Text;
namespace CoopSweeper.View
{
public class GameView : IView
{
private readonly Game _game;
private IField[,] _currentMap = null;
public GameView(Game game)
{
_game = game;
}
public Position Position { get; set; }
public Position Cursor { get; set; }
public Position MapPositon
{
get => new Position(Position.X + 1, Position.Y + 1);
set => Position = new Position(value.X - 1, value.Y - 1);
}
public void Draw(bool fullRedraw)
{
if (fullRedraw)
DrawBorder();
DrawMap(fullRedraw);
}
private void DrawMap(bool fullRedraw)
{
for (var y = 0; y < _game.Map.GetLength(1); y++)
{
if (_currentMap == null || fullRedraw) Console.SetCursorPosition(MapPositon.X, MapPositon.Y + y);
for (var x = 0; x < _game.Map.GetLength(0); x++)
{
if (_currentMap != null && !fullRedraw)
{
var currentField = _currentMap[x, y];
var newField = _game.Map[x, y];
if (currentField == null || currentField.DisplayState != newField.DisplayState)
{
Console.SetCursorPosition(MapPositon.X + x, MapPositon.Y + y);
DrawChar(_game.Map[x, y], x == Cursor.X && y == Cursor.Y);
}
}
else
{
DrawChar(_game.Map[x, y], x == Cursor.X && y == Cursor.Y);
}
}
}
_currentMap = ArrayHelpers.Clone2D(_game.Map);
}
private 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;
}
private void DrawBorder()
{
var width = _game.Map.GetLength(0) + 2;
var height = _game.Map.GetLength(1) + 2;
Console.SetCursorPosition(Position.X, Position.Y);
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(Position.X, Position.Y + y);
Console.Write("║");
Console.SetCursorPosition(Position.X + width - 1, Position.Y + y);
Console.Write("║");
}
Console.SetCursorPosition(Position.X, Position.Y + height - 1);
linebuilder = new StringBuilder();
linebuilder.Append("╚");
for (var x = 0; x < width - 2; x++)
linebuilder.Append("═");
linebuilder.Append("╝");
Console.Write(linebuilder);
// ┌──┬──┐ ╔══╦══╗ ╒══╤══╕ ╓──╥──╖
// │ │ │ ║ ║ ║ │ │ │ ║ ║ ║
// ├──┼──┤ ╠══╬══╣ ╞══╪══╡ ╟──╫──╢
// │ │ │ ║ ║ ║ │ │ │ ║ ║ ║
// └──┴──┘ ╚══╩══╝ ╘══╧══╛ ╙──╨──╜
}
public bool HandleKeyEvent(ConsoleKey key)
{
var oldCursorPos = Cursor;
bool fullRedraw = false;
bool keyHandled = true;
switch (key)
{
case ConsoleKey.UpArrow:
Cursor = new Position(Cursor.X, Math.Max(Cursor.Y - 1, 0));
break;
case ConsoleKey.DownArrow:
Cursor = new Position(Cursor.X, Math.Min(Cursor.Y + 1, _game.Map.GetLength(1) - 1));
break;
case ConsoleKey.LeftArrow:
Cursor = new Position(Math.Max(Cursor.X - 1, 0), Cursor.Y);
break;
case ConsoleKey.RightArrow:
Cursor = new Position(Math.Min(Cursor.X + 1, _game.Map.GetLength(0) - 1), Cursor.Y);
break;
case ConsoleKey.Spacebar:
fullRedraw = true;
_game.Reveal(Cursor.X, Cursor.Y);
break;
case ConsoleKey.F:
fullRedraw = true;
_game.ToggleMark(Cursor.X, Cursor.Y);
break;
default:
keyHandled = false;
break;
}
if (fullRedraw)
{
DrawMap(false);
}
else if (oldCursorPos != Cursor)
{
Console.SetCursorPosition(oldCursorPos.X + MapPositon.X, oldCursorPos.Y + MapPositon.Y);
DrawChar(_game.Map[oldCursorPos.X, oldCursorPos.Y], false);
Console.SetCursorPosition(Cursor.X + MapPositon.X, Cursor.Y + MapPositon.Y);
DrawChar(_game.Map[Cursor.X, Cursor.Y], true);
}
return keyHandled;
}
}
}

15
CoopSweeper/View/IView.cs Normal file
View File

@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace CoopSweeper.View
{
public interface IView
{
bool HandleKeyEvent(ConsoleKey key);
Position Position { get; set; }
void Draw(bool fullRedraw);
}
}

View File

@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace CoopSweeper.View
{
public class MenuBarView : IView
{
public Position Position { get; set; }
public void Draw(bool fullRedraw)
{
Console.SetCursorPosition(Position.X, Position.Y);
Console.Write(" ");
Console.BackgroundColor = ConsoleColor.Gray;
Console.ForegroundColor = ConsoleColor.Black;
Console.Write("Configure Restart Exit");
Console.ForegroundColor = ConsoleColor.Gray;
Console.BackgroundColor = ConsoleColor.Black;
Console.Write(" ");
}
public bool HandleKeyEvent(ConsoleKey key)
{
return false;
}
}
}

View File

@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace CoopSweeper.View
{
public struct Position
{
public Position(int x, int y)
{
X = x;
Y = y;
}
public int X { get; set; }
public int Y { get; set; }
public override bool Equals(object obj)
{
if (!(obj is Position))
return false;
var position = (Position)obj;
return X == position.X &&
Y == position.Y;
}
public override int GetHashCode()
{
var hashCode = 1861411795;
hashCode = hashCode * -1521134295 + X.GetHashCode();
hashCode = hashCode * -1521134295 + Y.GetHashCode();
return hashCode;
}
public static bool operator ==(Position a, Position b) => a.X == b.X && a.Y == b.Y;
public static bool operator !=(Position a, Position b) => a.X != b.X || a.Y != b.Y;
}
}