Last active
November 3, 2020 10:49
-
-
Save keithn/38b42f06e2e60e0367f68f1c2e55de63 to your computer and use it in GitHub Desktop.
Grids.....
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Grids | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var grid = new GameGrid(5, 7); | |
grid.Display(); | |
} | |
} | |
public class GameGrid | |
{ | |
private GameCell[,] _grid; | |
public GameGrid(int width, int height) | |
{ | |
_grid = new GameCell[width, height]; | |
for (int y = 0; y < height; y++) | |
{ | |
for (int x = 0; x < width; x++) | |
{ | |
_grid[x, y] = new GameCell() {Name = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[y] + (x + 1).ToString()}; | |
} | |
} | |
} | |
public void Display() | |
{ | |
for (int y = 0; y < _grid.GetLength(1); y++) | |
{ | |
for (int x = 0; x < _grid.GetLength(0); x++) | |
{ | |
var c = Console.BackgroundColor; | |
Console.BackgroundColor = ColorForState(_grid[x, y].State); | |
Console.Write($"{_grid[x, y].Name}"); | |
Console.BackgroundColor = c; | |
Console.Write(" "); | |
} | |
Console.WriteLine(); | |
} | |
} | |
private ConsoleColor ColorForState(GameCell.CellState state) | |
{ | |
switch (state) | |
{ | |
case GameCell.CellState.Hidden : return ConsoleColor.Gray; | |
case GameCell.CellState.Hit : return ConsoleColor.Red; | |
default: | |
return ConsoleColor.Blue; | |
} | |
} | |
} | |
internal class GameCell | |
{ | |
public enum CellState | |
{ | |
Hit, | |
Miss, | |
Hidden | |
} | |
public string Name { get; set; } | |
public CellState State { get; set; } = CellState.Hit; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
prints
A1 A2 A3 A4 A5
B1 B2 B3 B4 B5
C1 C2 C3 C4 C5
D1 D2 D3 D4 D5
E1 E2 E3 E4 E5
F1 F2 F3 F4 F5
G1 G2 G3 G4 G5