Implement Change Detection Drawing
This commit is contained in:
@@ -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,20 +166,33 @@ 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++)
|
||||
{
|
||||
DrawChar(map[x, y], x == cursorX && y == cursorY);
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user