Compare commits

...

10 Commits

Author SHA1 Message Date
Marvin Rohrbach
35e3010a33 Add new Views system 2018-12-16 11:47:26 +01:00
Marvin Rohrbach
624f6d1ea6 Fix automatic reveal on restart 2018-12-15 16:39:12 +01:00
Marvin Rohrbach
07eeec1032 Merge branch 'master' of https://gitlab.com/timwundenberg/minesweeper-coop 2018-12-15 16:07:24 +01:00
Marvin Rohrbach
e0a05da06e Implement Change Detection Drawing 2018-12-15 16:07:03 +01:00
Tim Wundenberg
ea512f2e9a Merge branch 'master' of https://gitlab.com/timwundenberg/minesweeper-coop 2018-05-26 09:13:22 +02:00
Tim Wundenberg
1886538e1e auto reveal 2018-05-26 09:13:05 +02:00
Marvin Rohrbach
e7fda99c11 Merge branch 'master' of https://gitlab.com/timwundenberg/minesweeper-coop 2018-05-26 09:13:03 +02:00
Marvin Rohrbach
7712aa4a0f Improve rendering and gameplay 2018-05-26 09:12:54 +02:00
Tim Wundenberg
e70f2031b9 Reveal Bombs when loose 2018-05-25 11:38:24 +02:00
Tim Wundenberg
ce9e703c6e auto reveal 2018-05-25 11:33:00 +02:00
11 changed files with 489 additions and 156 deletions

View File

@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace CoopSweeper
{
public static class ArrayHelpers
{
public static T[,] Clone2D<T>(T[,] data) where T : class, ICloneable<T>
{
if (data == null)
return null;
var newArray = new T[data.GetLength(0), data.GetLength(1)];
for (var x = 0; x< data.GetLength(0); x++)
for (var y = 0; y < data.GetLength(1); y++)
newArray[x,y] = data[x,y].Clone();
return newArray;
}
}
}

View File

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

View File

@@ -21,20 +21,34 @@ namespace CoopSweeper.GameTypes
public int CheckID { get; set; }
public DisplayState GetDisplayState()
public DisplayState DisplayState
{
switch(State)
get
{
case FieldState.QUESTIONMARK:
case FieldState.NONE:
case FieldState.FLAG:
switch (State)
{
case FieldState.QUESTIONMARK:
case FieldState.NONE:
case FieldState.FLAG:
return (DisplayState)State;
case FieldState.REVEALED:
if(ContainsBomb)
return DisplayState.BOMB;
return (DisplayState)SurroundingBombs;
case FieldState.REVEALED:
if (ContainsBomb)
return DisplayState.BOMB;
return (DisplayState)SurroundingBombs;
}
return DisplayState.ERROR;
}
return DisplayState.ERROR;
}
public IField Clone()
{
return new Field()
{
State = State,
ContainsBomb = ContainsBomb,
SurroundingBombs = SurroundingBombs,
CheckID = CheckID
};
}
}
}

View File

@@ -5,7 +5,7 @@ using System.Text;
namespace CoopSweeper.GameTypes
{
class Game
public class Game
{
private readonly Random _random = new Random();
private static int _checkID = 0;
@@ -43,7 +43,7 @@ namespace CoopSweeper.GameTypes
for (int j = 0; j < Map.GetLength(1); j++)
{
int bombCounter = 0;
foreach (var point in GetSorroundedFields(i, j))
foreach (var point in GetSurroundedFields(i, j))
{
if (Map[point.X, point.Y].ContainsBomb)
bombCounter++;
@@ -54,7 +54,7 @@ namespace CoopSweeper.GameTypes
}
}
private List<Point> GetSorroundedFields(int x, int y)
private List<Point> GetSurroundedFields(int x, int y)
{
var points = new List<Point>();
points.Add(new Point(x - 1, y - 1));
@@ -108,26 +108,90 @@ namespace CoopSweeper.GameTypes
return true;
}
private void InternalReveal(int x, int y, bool revealSurroundings)
{
var field = Map[x, y];
if (field.CheckID == _checkID)
return;
field.CheckID = _checkID;
if (field.State != FieldState.REVEALED)
{
if (field.State == FieldState.FLAG)
return;
field.State = FieldState.REVEALED;
if (field.ContainsBomb)
{
FinishGame(false);
return;
}
if (field.SurroundingBombs == 0)
foreach (var surField in GetSurroundedFields(x, y))
{
InternalReveal(surField.X, surField.Y, false);
}
}
else
{
if (revealSurroundings)
RevealSurroindings(x, y);
}
if (CheckGameFinished())
{
FinishGame(true);
return;
}
}
private void RevealSurroindings(int x, int y)
{
int bombs = Map[x, y].SurroundingBombs;
int bombsFlagged = 0;
foreach (var point in GetSurroundedFields(x, y))
{
if (Map[point.X, point.Y].State == FieldState.FLAG)
bombsFlagged++;
}
if (bombsFlagged == bombs)
foreach (var point in GetSurroundedFields(x, y))
{
if (Map[point.X, point.Y].State != FieldState.FLAG)
InternalReveal(point.X, point.Y, false);
}
}
private void RevealBombs()
{
for (int i = 0; i < Map.GetLength(0); i++)
{
for (int j = 0; j < Map.GetLength(1); j++)
{
if (Map[i, j].ContainsBomb)
Map[i, j].State = FieldState.REVEALED;
}
}
}
private void FinishGame(bool isWon)
{
RevealBombs();
GameFinished?.Invoke(isWon);
}
public void Reveal(int x, int y)
{
CheckMap();
var field = Map[x, y];
//if (field.CheckID < _checkID)
// return;
if (field.State != FieldState.REVEALED)
{
field.State = FieldState.REVEALED;
if (field.ContainsBomb)
GameFinished?.Invoke(false);
}
if (CheckGameFinished())
GameFinished?.Invoke(true);
//_checkID
_checkID++;
InternalReveal(x, y, true);
}
public void ToggleMark(int x, int y)

View File

@@ -30,7 +30,7 @@ namespace CoopSweeper.GameTypes
FLAG = DisplayState.FLAG
}
public interface IField
public interface IField : ICloneable<IField>
{
bool ContainsBomb { get; set; }
@@ -41,6 +41,6 @@ namespace CoopSweeper.GameTypes
FieldState State { get; set; }
DisplayState GetDisplayState();
DisplayState DisplayState { get; }
}
}

11
CoopSweeper/ICloneable.cs Normal file
View File

@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace CoopSweeper
{
public interface ICloneable<T> where T : class, ICloneable<T>
{
T Clone();
}
}

View File

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

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;
}
}