From 3042247e064a97f52ca7a20fc378e81aecf097cb Mon Sep 17 00:00:00 2001 From: Andrew Cooper Date: Thu, 8 Sep 2022 17:02:17 +1000 Subject: [PATCH] Create Game class --- 56_Life_for_Two/csharp/Game.cs | 56 +++++++++++++++++++++ 56_Life_for_Two/csharp/IOExtensions.cs | 2 +- 56_Life_for_Two/csharp/Program.cs | 46 +---------------- 56_Life_for_Two/csharp/Resources/Player.txt | 2 +- 4 files changed, 59 insertions(+), 47 deletions(-) create mode 100644 56_Life_for_Two/csharp/Game.cs diff --git a/56_Life_for_Two/csharp/Game.cs b/56_Life_for_Two/csharp/Game.cs new file mode 100644 index 00000000..0b2704bf --- /dev/null +++ b/56_Life_for_Two/csharp/Game.cs @@ -0,0 +1,56 @@ +internal class Game +{ + private readonly IReadWrite _io; + + public Game(IReadWrite io) + { + _io = io; + } + + public void Play() + { + _io.Write(Streams.Title); + + var _board = new Board(); + + for (var _player = 1; _player <= 2; _player++) + { + var P1 = _player == 2 ? 0x30 : 0x03; + _io.WriteLine(Formats.InitialPieces, _player); + for (var i = 1; i <= 3; i++) + { + _board[_io.ReadCoordinates(_board)] = P1; + } + } + + _board.CalculateNextGeneration(); + _board.Display(_io); + + while (true) + { + _io.WriteLine(); + _board.CalculateNeighbours(); + _board.CalculateNextGeneration(); + _board.Display(_io); + + if (_board.Result is not null) { break; } + + var player1Coordinate = _io.ReadCoordinates(1, _board); + var player2Coordinate = _io.ReadCoordinates(2, _board); + + if (player1Coordinate == player2Coordinate) + { + _io.Write(Streams.SameCoords); + // This is a bug existing in the original code. The line should be _board[_coordinates[_player]] = 0; + _board[player1Coordinate + 1] = 0; + } + else + { + _board[player1Coordinate] = 0x0100; + _board[player2Coordinate] = 0x1000; + } + } + + _io.WriteLine(_board.Result); + } +} \ No newline at end of file diff --git a/56_Life_for_Two/csharp/IOExtensions.cs b/56_Life_for_Two/csharp/IOExtensions.cs index 5f361ef8..7d8bfc62 100644 --- a/56_Life_for_Two/csharp/IOExtensions.cs +++ b/56_Life_for_Two/csharp/IOExtensions.cs @@ -2,7 +2,7 @@ internal static class IOExtensions { internal static Coordinates ReadCoordinates(this IReadWrite io, int player, Board board) { - io.WriteLine(Formats.Player, player); + io.Write(Formats.Player, player); return io.ReadCoordinates(board); } diff --git a/56_Life_for_Two/csharp/Program.cs b/56_Life_for_Two/csharp/Program.cs index bc2a3440..4c399811 100644 --- a/56_Life_for_Two/csharp/Program.cs +++ b/56_Life_for_Two/csharp/Program.cs @@ -2,48 +2,4 @@ global using Games.Common.IO; global using static LifeforTwo.Resources.Resource; global using LifeforTwo; -var io = new ConsoleIO(); - -io.Write(Streams.Title); - -var _board = new Board(); - -for (var _player = 1; _player <= 2; _player++) -{ - var P1 = _player == 2 ? 0x30 : 0x03; - io.WriteLine(Formats.InitialPieces, _player); - for (var i = 1; i <= 3; i++) - { - _board[io.ReadCoordinates(_board)] = P1; - } -} - -_board.CalculateNextGeneration(); -_board.Display(io); - -while (true) -{ - io.WriteLine(); - _board.CalculateNeighbours(); - _board.CalculateNextGeneration(); - _board.Display(io); - - if (_board.Result is not null) { break; } - - var player1Coordinate = io.ReadCoordinates(1, _board); - var player2Coordinate = io.ReadCoordinates(2, _board); - - if (player1Coordinate == player2Coordinate) - { - io.Write(Streams.SameCoords); - // This is a bug existing in the original code. The line should be _board[_coordinates[_player]] = 0; - _board[player1Coordinate + 1] = 0; - } - else - { - _board[player1Coordinate] = 0x0100; - _board[player2Coordinate] = 0x1000; - } -} - -io.WriteLine(_board.Result); \ No newline at end of file +new Game(new ConsoleIO()).Play(); diff --git a/56_Life_for_Two/csharp/Resources/Player.txt b/56_Life_for_Two/csharp/Resources/Player.txt index 73fbf366..f920e489 100644 --- a/56_Life_for_Two/csharp/Resources/Player.txt +++ b/56_Life_for_Two/csharp/Resources/Player.txt @@ -1,3 +1,3 @@ -Player {0} \ No newline at end of file +Player {0} \ No newline at end of file