From 2ccbdcc98d02ad0ef52e5440c9c49b283d42c931 Mon Sep 17 00:00:00 2001 From: Andrew Cooper Date: Wed, 7 Sep 2022 20:41:54 +1000 Subject: [PATCH] Move display to Board --- 56_Life_for_Two/csharp/Board.cs | 28 +++++++++++++++++++++++++- 56_Life_for_Two/csharp/Program.cs | 33 +++++++++++-------------------- 2 files changed, 38 insertions(+), 23 deletions(-) diff --git a/56_Life_for_Two/csharp/Board.cs b/56_Life_for_Two/csharp/Board.cs index dbe7ece9..7c5d53c5 100644 --- a/56_Life_for_Two/csharp/Board.cs +++ b/56_Life_for_Two/csharp/Board.cs @@ -2,7 +2,10 @@ namespace LifeforTwo; internal class Board { - private const int PieceMask = 0x1100; + private const int Empty = 0x0000; + private const int Player1 = 0x0100; + private const int Player2 = 0x1000; + private const int PieceMask = Player1 | Player2; private const int NeighbourValueOffset = 8; private readonly int[,] _cells = new int[7,7]; @@ -36,4 +39,27 @@ internal class Board } } } + + public void Display(IReadWrite io) + { + for (var y = 0; y <= 6; y++) + { + io.WriteLine(); + for (var x = 0; x <= 6; x++) + { + io.Write(GetDisplay(x, y)); + } + } + } + + private string GetDisplay(int x, int y) => + (x, y, this[x, y]) switch + { + (0 or 6, _, _) => $" {y % 6} ", + (_, 0 or 6, _) => $" {x % 6} ", + (_, _, Empty) => " ", + (_, _, Player1) => " * ", + (_, _, Player2) => " # ", + _ => throw new InvalidOperationException($"Unexpected cell value at ({x}, {y}): {this[x, y]}") + }; } \ No newline at end of file diff --git a/56_Life_for_Two/csharp/Program.cs b/56_Life_for_Two/csharp/Program.cs index e63db901..1754cdc5 100644 --- a/56_Life_for_Two/csharp/Program.cs +++ b/56_Life_for_Two/csharp/Program.cs @@ -12,32 +12,20 @@ var _willLive = new[] { 0x0003, 0x0102, 0x0103, 0x0120, 0x0130, 0x0121, 0x0112, var _coordinates = new Coordinates[3]; int _player1Count, _player2Count; -void CalculateAndDisplayNext() +void CalculateNext() { _player1Count = _player2Count = 0; - for (var y = 0; y <= 6; y++) + for (var y = 1; y <= 5; y++) { - io.WriteLine(); - for (var x = 0; x <= 6; x++) + for (var x = 1; x <= 5; x++) { - if (y % 6 == 0) - { - io.Write($" {x % 6} "); - } - else if (x % 6 == 0) - { - io.Write($" {y % 6} "); - } - else - { - CalculateAndDisplayCell(x, y); - } + CalculateNextCell(x, y); } } return; } -void CalculateAndDisplayCell(int x, int y) +void CalculateNextCell(int x, int y) { if (_board[x, y] >= 3) { @@ -47,11 +35,11 @@ void CalculateAndDisplayCell(int x, int y) { if (o < 9) { - _board[x, y] = 0x0100; _player1Count++; io.Write(" * "); + _board[x, y] = 0x0100; _player1Count++; } else { - _board[x, y] = 0x1000; _player2Count++; io.Write(" # "); + _board[x, y] = 0x1000; _player2Count++; } return; } @@ -59,7 +47,6 @@ void CalculateAndDisplayCell(int x, int y) } _board[x, y] = 0; - io.Write(" "); } for (var _player = 1; _player <= 2; _player++) @@ -73,13 +60,15 @@ for (var _player = 1; _player <= 2; _player++) } } -CalculateAndDisplayNext(); +CalculateNext(); +_board.Display(io); while (true) { io.WriteLine(); _board.CalculateNeighbours(); - CalculateAndDisplayNext(); + CalculateNext(); + _board.Display(io); if (_player1Count == 0 || _player2Count == 0) { break; }