diff --git a/77_Salvo/csharp/Extensions/IOExtensions.cs b/77_Salvo/csharp/Extensions/IOExtensions.cs index b9eaa2c4..6d021deb 100644 --- a/77_Salvo/csharp/Extensions/IOExtensions.cs +++ b/77_Salvo/csharp/Extensions/IOExtensions.cs @@ -12,7 +12,7 @@ internal static class IOExtensions { return position; } - io.WriteLine("ILLEGAL, ENTER AGAIN."); + io.Write(Streams.Illegal); } } diff --git a/77_Salvo/csharp/Game.cs b/77_Salvo/csharp/Game.cs index 2afac1bd..2f3ebe8e 100644 --- a/77_Salvo/csharp/Game.cs +++ b/77_Salvo/csharp/Game.cs @@ -29,50 +29,48 @@ internal class Game var computerGrid = new Grid(_random); var humanGrid = new Grid(_io); var humanShotSelector = new HumanShotSelector(humanGrid, computerGrid); - var startResponse = _io.ReadString("DO YOU WANT TO START"); - while (startResponse == "WHERE ARE YOUR SHIPS?") + var startResponse = _io.ReadString(Prompts.Start); + while (startResponse == Strings.WhereAreYourShips) { foreach (var ship in computerGrid.Ships) { _io.WriteLine(ship); } - startResponse = _io.ReadString("DO YOU WANT TO START"); + startResponse = _io.ReadString(Prompts.Start); } L1890: var turnNumber=0; -L1900: var seeShotsResponse = _io.ReadString("DO YOU WANT TO SEE MY SHOTS"); +L1900: var seeShotsResponse = _io.ReadString(Prompts.SeeShots); L1920: _io.WriteLine(); L1930: if (startResponse != "YES") { goto L2620; } L1950: if (startResponse != "YES") { goto L1990; } L1960: turnNumber++; -L1970: _io.WriteLine(); -L1980: _io.WriteLine($"TURN {turnNumber}"); -L1990: var maxShotCount = humanShotSelector.GetShotCount(); -L2220: _io.WriteLine($"YOU HAVE {maxShotCount} SHOTS."); - if (maxShotCount == 0) { goto L2270; } -L2230: if (maxShotCount > computerGrid.UntriedSquareCount) +L1980: _io.Write(Strings.Turn(turnNumber)); +L1990: var numberOfShots = humanShotSelector.GetShotCount(); +L2220: _io.Write(Strings.YouHaveShots(numberOfShots)); + if (numberOfShots == 0) { goto L2270; } +L2230: if (numberOfShots > computerGrid.UntriedSquareCount) { - _io.WriteLine("YOU HAVE MORE SHOTS THAN THERE ARE BLANK SQUARES."); + _io.WriteLine(Streams.YouHaveMoreShotsThanSquares); L2250: goto L2890; } foreach (var shot1 in humanShotSelector.GetShots(_io)) { if (computerGrid.IsHit(shot1, turnNumber, out var shipName)) { - _io.WriteLine($"YOU HIT MY {shipName}."); + _io.Write(Strings.YouHit(shipName)); } } L2620: if (startResponse == "YES") { goto L2670; } L2640: turnNumber++; -L2650: _io.WriteLine(); -L2660: _io.WriteLine($"TURN {turnNumber}"); -L2670: maxShotCount = computerGrid.Ships.Sum(s => s.Shots); -L2840: _io.WriteLine($"I HAVE {maxShotCount} SHOTS."); -L2850: if (humanGrid.UntriedSquareCount > maxShotCount) { goto L2880; } -L2860: _io.WriteLine("I HAVE MORE SHOTS THAN BLANK SQUARES."); -L2270: _io.WriteLine("I HAVE WON."); +L2660: _io.Write(Strings.Turn(turnNumber)); +L2670: numberOfShots = computerGrid.Ships.Sum(s => s.Shots); +L2840: _io.Write(Strings.IHaveShots(numberOfShots)); +L2850: if (humanGrid.UntriedSquareCount > numberOfShots) { goto L2880; } +L2860: _io.Write(Streams.IHaveMoreShotsThanSquares); +L2270: _io.Write(Streams.IWon); return; -L2880: if (maxShotCount != 0) { goto L2960; } -L2890: _io.WriteLine("YOU HAVE WON."); +L2880: if (numberOfShots != 0) { goto L2960; } +L2890: _io.Write(Streams.YouWon); L2900: return; L2960: for (var i = 1; i <= 12; i++) @@ -91,7 +89,7 @@ L3070: shot = shot.BringIntoRange(_random); L3170: goto L3270; // record shot L3180: temp[shotCount]=shot; -L3200: if (shotCount==maxShotCount) { goto L3380; } +L3200: if (shotCount==numberOfShots) { goto L3380; } L3210: if (strategyNumber==6) { goto L3030; } L3240: //DATA 1,1,-1,1,1,-3,1,1,0,2,-1,1 var data = new Offset[] { new(1,1),new(-1,1),new(1,-3),new(1,1),new(0,2),new(-1,1) }; @@ -111,11 +109,11 @@ L3360: shotCount++; L3370: goto L3180; // display shots L3380: if (seeShotsResponse != "YES") { goto L3420; } -L3390: for (var i = 1; i <= maxShotCount; i++) +L3390: for (var i = 1; i <= numberOfShots; i++) { L3400: _io.WriteLine(temp[i]); } -L3420: for (var i = 1; i <= maxShotCount; i++) +L3420: for (var i = 1; i <= numberOfShots; i++) { L3430: if (humanGrid[temp[i]] == 3) { @@ -197,22 +195,22 @@ L3860: for (var i = 1; i <= 12; i++) } } } -L4030: for (var i = 1; i <= maxShotCount; i++) +L4030: for (var i = 1; i <= numberOfShots; i++) { L4040: temp[i]=i; } foreach (var position in Position.All) { L4090: var Q9=1; -L4100: for (var i = 1; i <= maxShotCount; i++) +L4100: for (var i = 1; i <= numberOfShots; i++) { L4110: if (tempGrid[temp[i]]>=tempGrid[temp[Q9]]) { continue; } L4120: Q9=i; } -L4131: if (position.X>maxShotCount) { goto L4140; } +L4131: if (position.X>numberOfShots) { goto L4140; } L4132: if (position.IsOnDiagonal) { goto L4210; } L4140: if (tempGrid[position] _shots.TryGetValue(position, out turnTargeted); - internal bool IsHit(Position position, int turnNumber, out string? shipName) + internal bool IsHit(Position position, int turnNumber, [NotNullWhen(true)] out string? shipName) { shipName = null; _shots[position] = turnNumber; var ship = _ships.FirstOrDefault(s => s.IsHit(position)); if (ship == null) { return false; } + shipName = ship.Name; if (ship.IsDestroyed) { _ships.Remove(ship); } diff --git a/77_Salvo/csharp/Resources/Coordinates.txt b/77_Salvo/csharp/Resources/Coordinates.txt new file mode 100644 index 00000000..387d7a6b --- /dev/null +++ b/77_Salvo/csharp/Resources/Coordinates.txt @@ -0,0 +1 @@ +Enter coordinates for... diff --git a/77_Salvo/csharp/Resources/IHaveMoreShotsThanSquares.txt b/77_Salvo/csharp/Resources/IHaveMoreShotsThanSquares.txt new file mode 100644 index 00000000..a6f37110 --- /dev/null +++ b/77_Salvo/csharp/Resources/IHaveMoreShotsThanSquares.txt @@ -0,0 +1 @@ +I have more shots than blank squares. diff --git a/77_Salvo/csharp/Resources/IHaveShots.txt b/77_Salvo/csharp/Resources/IHaveShots.txt new file mode 100644 index 00000000..9157e1a7 --- /dev/null +++ b/77_Salvo/csharp/Resources/IHaveShots.txt @@ -0,0 +1 @@ +I have {0} shots. diff --git a/77_Salvo/csharp/Resources/IHit.txt b/77_Salvo/csharp/Resources/IHit.txt new file mode 100644 index 00000000..a3f3dccf --- /dev/null +++ b/77_Salvo/csharp/Resources/IHit.txt @@ -0,0 +1 @@ +I hot your {0} diff --git a/77_Salvo/csharp/Resources/IWon.txt b/77_Salvo/csharp/Resources/IWon.txt new file mode 100644 index 00000000..b3f50634 --- /dev/null +++ b/77_Salvo/csharp/Resources/IWon.txt @@ -0,0 +1 @@ +I have won. diff --git a/77_Salvo/csharp/Resources/Illegal.txt b/77_Salvo/csharp/Resources/Illegal.txt new file mode 100644 index 00000000..6a71787a --- /dev/null +++ b/77_Salvo/csharp/Resources/Illegal.txt @@ -0,0 +1 @@ +Illegal, enter again. diff --git a/77_Salvo/csharp/Resources/Resource.cs b/77_Salvo/csharp/Resources/Resource.cs index 0cc55034..c3a0d8ca 100644 --- a/77_Salvo/csharp/Resources/Resource.cs +++ b/77_Salvo/csharp/Resources/Resource.cs @@ -8,13 +8,32 @@ internal static class Resource internal static class Streams { public static Stream Title => GetStream(); + public static Stream YouHaveMoreShotsThanSquares => GetStream(); + public static Stream YouWon => GetStream(); + public static Stream IHaveMoreShotsThanSquares => GetStream(); + public static Stream IWon => GetStream(); + public static Stream Illegal => GetStream(); + } + + internal static class Strings + { + public static string WhereAreYourShips => GetString(); + public static string YouHaveShots(int number) => Format(number); + public static string IHaveShots(int number) => Format(number); + public static string YouHit(string shipName) => Format(shipName); + public static string ShotBefore(int turnNumber) => Format(turnNumber); + public static string Turn(int number) => Format(number); } internal static class Prompts { + public static string Coordinates => GetString(); + public static string Start => GetString(); + public static string SeeShots => GetString(); } - private static string GetPrompt([CallerMemberName] string? name = null) => GetString($"{name}Prompt"); + private static string Format(T value, [CallerMemberName] string? name = null) + => string.Format(GetString(name), value); private static string GetString([CallerMemberName] string? name = null) { diff --git a/77_Salvo/csharp/Resources/SeeShots.txt b/77_Salvo/csharp/Resources/SeeShots.txt new file mode 100644 index 00000000..7f608b0b --- /dev/null +++ b/77_Salvo/csharp/Resources/SeeShots.txt @@ -0,0 +1 @@ +Do you want to see my shots \ No newline at end of file diff --git a/77_Salvo/csharp/Resources/ShotBefore.txt b/77_Salvo/csharp/Resources/ShotBefore.txt new file mode 100644 index 00000000..18e90fcb --- /dev/null +++ b/77_Salvo/csharp/Resources/ShotBefore.txt @@ -0,0 +1 @@ +You shot there before on turn {0} diff --git a/77_Salvo/csharp/Resources/Start.txt b/77_Salvo/csharp/Resources/Start.txt new file mode 100644 index 00000000..283a0fbb --- /dev/null +++ b/77_Salvo/csharp/Resources/Start.txt @@ -0,0 +1 @@ +Do you want to start \ No newline at end of file diff --git a/77_Salvo/csharp/Resources/Turn.txt b/77_Salvo/csharp/Resources/Turn.txt new file mode 100644 index 00000000..14bf316e --- /dev/null +++ b/77_Salvo/csharp/Resources/Turn.txt @@ -0,0 +1,2 @@ + +Turn {0} diff --git a/77_Salvo/csharp/Resources/WhereAreYourShips.txt b/77_Salvo/csharp/Resources/WhereAreYourShips.txt new file mode 100644 index 00000000..797c1144 --- /dev/null +++ b/77_Salvo/csharp/Resources/WhereAreYourShips.txt @@ -0,0 +1 @@ +Where are your ships? diff --git a/77_Salvo/csharp/Resources/YouHaveMoreShotsThanSquares.txt b/77_Salvo/csharp/Resources/YouHaveMoreShotsThanSquares.txt new file mode 100644 index 00000000..37c06812 --- /dev/null +++ b/77_Salvo/csharp/Resources/YouHaveMoreShotsThanSquares.txt @@ -0,0 +1 @@ +You have more shots that there are blank squares. diff --git a/77_Salvo/csharp/Resources/YouHaveShots.txt b/77_Salvo/csharp/Resources/YouHaveShots.txt new file mode 100644 index 00000000..43abeda8 --- /dev/null +++ b/77_Salvo/csharp/Resources/YouHaveShots.txt @@ -0,0 +1 @@ +You have {0} shots. diff --git a/77_Salvo/csharp/Resources/YouHit.txt b/77_Salvo/csharp/Resources/YouHit.txt new file mode 100644 index 00000000..b5b5d761 --- /dev/null +++ b/77_Salvo/csharp/Resources/YouHit.txt @@ -0,0 +1 @@ +You hit my {0}. diff --git a/77_Salvo/csharp/Resources/YouWon.txt b/77_Salvo/csharp/Resources/YouWon.txt new file mode 100644 index 00000000..1f71343b --- /dev/null +++ b/77_Salvo/csharp/Resources/YouWon.txt @@ -0,0 +1 @@ +You have won.