Repository navigation
Expand file tree
/
Copy pathGridPrinter.cs
More file actions
76 lines (71 loc) · 2.32 KB
/
Copy pathGridPrinter.cs
File metadata and controls
76 lines (71 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System;
using System.Collections.Generic;
using System.Text;
namespace MinesweeperConsole
{
static class GridPrinter
{
/// <summary>
/// Prints the grid in its current state, or revealing all if showAll is true
/// </summary>
/// <param name="cells"></param>
/// <param name="showAll">Shows all cells as if all were uncovered</param>
public static void Print(Cell[,] cells, bool showAll = false)
{
for (int y = 0; y <= cells.GetUpperBound(1); y++)
{
for (int x = 0; x <= cells.GetUpperBound(0); x++)
{
GetSprite(cells[x, y], showAll).Draw();
}
Console.ResetColor();
Console.Write('\n');
}
Console.ResetColor();
}
private static CellSprite GetSprite(Cell cell, bool showAll = false)
{
if (!showAll)
{
if (cell.State == CellState.Covered)
return new CellSprite
{
BG = ConsoleColor.White,
FG = ConsoleColor.DarkGray,
Char = '?',
};
if (cell.State == CellState.Flagged)
return new CellSprite
{
BG = ConsoleColor.White,
FG = ConsoleColor.Red,
Char = 'F',
};
}
if (cell.IsBomb)
return new CellSprite
{
BG = ConsoleColor.Red,
FG = ConsoleColor.White,
Char = 'B',
};
char c = cell.AdjacentBombsCount.ToString()[0];
return new CellSprite
{
Char = c, // c == '0' ? ' ' : c,
};
}
class CellSprite
{
public ConsoleColor BG { get; set; } = ConsoleColor.Black;
public ConsoleColor FG { get; set; } = ConsoleColor.White;
public char Char { get; set; }
public void Draw()
{
Console.BackgroundColor = BG;
Console.ForegroundColor = FG;
Console.Write(Char);
}
}
}
}