Implement Change Detection Drawing

This commit is contained in:
Marvin Rohrbach
2018-12-15 16:07:03 +01:00
parent e7fda99c11
commit e0a05da06e
5 changed files with 81 additions and 20 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 : 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

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

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 : ICloneable<T>
{
T Clone();
}
}

View File

@@ -7,6 +7,7 @@ namespace CoopSweeper
{
class Program
{
static IField[,] currentMap = null;
const int MAP_POS_X = 3;
const int MAP_POS_Y = 3;
@@ -27,7 +28,7 @@ namespace CoopSweeper
Console.Write("You won!");
else
Console.Write("You lost!");
DrawMap(MAP_POS_X, MAP_POS_Y, game.Map, cursorPosX, cursorPosY);
DrawMap(MAP_POS_X, MAP_POS_Y, game.Map, cursorPosX, cursorPosY, true);
Console.ReadKey();
StartNewGame(game, cursorPosX, cursorPosY);
};
@@ -59,7 +60,7 @@ namespace CoopSweeper
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);
DrawMap(3, 3, game.Map, cursorPosX, cursorPosY, true);
else if(oldCursorPosX != cursorPosX || oldCursorPosY != cursorPosY)
{
Console.SetCursorPosition(oldCursorPosX + 3, oldCursorPosY + 3);
@@ -79,7 +80,7 @@ namespace CoopSweeper
bool fgChanged = false;
bool bgChanged = false;
var c = 'E';
var state = f.GetDisplayState();
var state = f.DisplayState;
switch (state) {
case DisplayState.EMPTY:
c = ' ';
@@ -93,8 +94,10 @@ namespace CoopSweeper
c = ' ';
break;
case DisplayState.QUESTIONMARK:
fgChanged = true;
bgChanged = true;
Console.BackgroundColor = ConsoleColor.Gray;
Console.ForegroundColor = ConsoleColor.DarkBlue;
c = '?';
break;
case DisplayState.BOMB:
@@ -163,21 +166,34 @@ namespace CoopSweeper
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);
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)
private static void DrawMap(int posX, int posY, IField[,] map, int cursorX, int cursorY, bool doChangeDetection)
{
var res = new string[map.GetLength(1)];
for (var y = 0; y < map.GetLength(1); y++)
{
Console.SetCursorPosition(posX, posY + 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)
{