From 2bb6bff9b87e2f1549a83879feaefc3fbbe6da93 Mon Sep 17 00:00:00 2001 From: rbamforth <79797573+rbamforth@users.noreply.github.com> Date: Mon, 22 Mar 2021 11:59:50 +0000 Subject: [PATCH 1/8] Added initial files Added new Visual Studio solution. --- 94 War/csharp/War/War.sln | 25 +++++++++++++++++++++++++ 94 War/csharp/War/War/Program.cs | 12 ++++++++++++ 94 War/csharp/War/War/War.csproj | 8 ++++++++ 3 files changed, 45 insertions(+) create mode 100644 94 War/csharp/War/War.sln create mode 100644 94 War/csharp/War/War/Program.cs create mode 100644 94 War/csharp/War/War/War.csproj diff --git a/94 War/csharp/War/War.sln b/94 War/csharp/War/War.sln new file mode 100644 index 00000000..8f9560d8 --- /dev/null +++ b/94 War/csharp/War/War.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31112.23 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "War", "War\War.csproj", "{C13BE0FA-D8F7-4CA7-A95D-DA03A9DE8950}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C13BE0FA-D8F7-4CA7-A95D-DA03A9DE8950}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C13BE0FA-D8F7-4CA7-A95D-DA03A9DE8950}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C13BE0FA-D8F7-4CA7-A95D-DA03A9DE8950}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C13BE0FA-D8F7-4CA7-A95D-DA03A9DE8950}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {3FA273C6-089E-4F3D-8DC0-F9143D1CDF3A} + EndGlobalSection +EndGlobal diff --git a/94 War/csharp/War/War/Program.cs b/94 War/csharp/War/War/Program.cs new file mode 100644 index 00000000..cf4b0edf --- /dev/null +++ b/94 War/csharp/War/War/Program.cs @@ -0,0 +1,12 @@ +using System; + +namespace War +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello World!"); + } + } +} diff --git a/94 War/csharp/War/War/War.csproj b/94 War/csharp/War/War/War.csproj new file mode 100644 index 00000000..c73e0d16 --- /dev/null +++ b/94 War/csharp/War/War/War.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp3.1 + + + From 455755b6a433447cb3f74ec043908d981791c24a Mon Sep 17 00:00:00 2001 From: rbamforth <79797573+rbamforth@users.noreply.github.com> Date: Mon, 22 Mar 2021 12:00:26 +0000 Subject: [PATCH 2/8] Added Cards and Deck classes --- 94 War/csharp/War/War/Cards.cs | 113 +++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 94 War/csharp/War/War/Cards.cs diff --git a/94 War/csharp/War/War/Cards.cs b/94 War/csharp/War/War/Cards.cs new file mode 100644 index 00000000..ad84735a --- /dev/null +++ b/94 War/csharp/War/War/Cards.cs @@ -0,0 +1,113 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace War +{ + public enum Suit + { + none = 0, + clubs, + diamonds, + hearts, + spades + } + + public enum Rank + { + none = 0, + // Skip 1 because ace is high. + two = 2, + three, + four, + five, + six, + seven, + eight, + nine, + ten, + jack, + queen, + king, + ace + } + + // TODO Testing + + class Card + { + // TODO Turn into properties, maybe?? + private Suit suit; + private Rank rank; + + public Card(Suit suit, Rank rank) // immutable + { + this.suit = suit; + this.rank = rank; + } + + // would normally consider suit and rank but in this case we only want to compare rank. + public static bool operator ==(Card lhs, Card rhs) + { + return lhs.rank == rhs.rank; + } + + public static bool operator !=(Card lhs, Card rhs) + { + return !(lhs == rhs); + } + + public static bool operator <(Card lhs, Card rhs) + { + return lhs.rank < rhs.rank; + } + public static bool operator >(Card lhs, Card rhs) + { + return rhs < lhs; + } + public static bool operator <=(Card lhs, Card rhs) + { + return !(lhs > rhs); + } + public static bool operator >=(Card lhs, Card rhs) + { + return !(lhs < rhs); + } + + public override string ToString() + { + // TODO No need to create the dictionaries each time this is called. + // Also make it static. + // Is there a C# equivalent of an initializer list? + var suitNames = new Dictionary(); + + suitNames[Suit.none] = "N"; + suitNames[Suit.clubs] = "C"; + suitNames[Suit.diamonds] = "D"; + suitNames[Suit.hearts] = "H"; + suitNames[Suit.spades] = "S"; + + var rankNames = new Dictionary(); + rankNames[Rank.none] = "0"; + rankNames[Rank.two] = "2"; + rankNames[Rank.three] = "3"; + rankNames[Rank.four] = "4"; + rankNames[Rank.five] = "5"; + rankNames[Rank.six] = "6"; + rankNames[Rank.seven] = "7"; + rankNames[Rank.eight] = "8"; + rankNames[Rank.nine] = "9"; + rankNames[Rank.ten] = "10"; + rankNames[Rank.jack] = "J"; + rankNames[Rank.queen] = "Q"; + rankNames[Rank.king] = "K"; + rankNames[Rank.ace] = "A"; + + return $"{suitNames[suit]}-{rankNames[rank]}"; // string interpolation + } + } + + class Deck + { + } +} From d9ee2fa070c70ba09042e4a17ac706afdb6592df Mon Sep 17 00:00:00 2001 From: rbamforth <79797573+rbamforth@users.noreply.github.com> Date: Tue, 23 Mar 2021 11:45:38 +0000 Subject: [PATCH 3/8] Added Tester project and tests for the Card class. --- 94 War/csharp/War/War.sln | 8 +- 94 War/csharp/War/War/Cards.cs | 102 +++++++++++-------- 94 War/csharp/War/WarTester/Tests.cs | 90 ++++++++++++++++ 94 War/csharp/War/WarTester/WarTester.csproj | 20 ++++ 4 files changed, 179 insertions(+), 41 deletions(-) create mode 100644 94 War/csharp/War/WarTester/Tests.cs create mode 100644 94 War/csharp/War/WarTester/WarTester.csproj diff --git a/94 War/csharp/War/War.sln b/94 War/csharp/War/War.sln index 8f9560d8..a03a3a64 100644 --- a/94 War/csharp/War/War.sln +++ b/94 War/csharp/War/War.sln @@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 16 VisualStudioVersion = 16.0.31112.23 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "War", "War\War.csproj", "{C13BE0FA-D8F7-4CA7-A95D-DA03A9DE8950}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "War", "War\War.csproj", "{C13BE0FA-D8F7-4CA7-A95D-DA03A9DE8950}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WarTester", "WarTester\WarTester.csproj", "{ACB0D6AD-9675-4E72-BB97-BBB2241CB494}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -15,6 +17,10 @@ Global {C13BE0FA-D8F7-4CA7-A95D-DA03A9DE8950}.Debug|Any CPU.Build.0 = Debug|Any CPU {C13BE0FA-D8F7-4CA7-A95D-DA03A9DE8950}.Release|Any CPU.ActiveCfg = Release|Any CPU {C13BE0FA-D8F7-4CA7-A95D-DA03A9DE8950}.Release|Any CPU.Build.0 = Release|Any CPU + {ACB0D6AD-9675-4E72-BB97-BBB2241CB494}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {ACB0D6AD-9675-4E72-BB97-BBB2241CB494}.Debug|Any CPU.Build.0 = Debug|Any CPU + {ACB0D6AD-9675-4E72-BB97-BBB2241CB494}.Release|Any CPU.ActiveCfg = Release|Any CPU + {ACB0D6AD-9675-4E72-BB97-BBB2241CB494}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/94 War/csharp/War/War/Cards.cs b/94 War/csharp/War/War/Cards.cs index ad84735a..1efdc715 100644 --- a/94 War/csharp/War/War/Cards.cs +++ b/94 War/csharp/War/War/Cards.cs @@ -34,11 +34,37 @@ namespace War // TODO Testing - class Card + public class Card { - // TODO Turn into properties, maybe?? - private Suit suit; - private Rank rank; + private readonly Suit suit; + private readonly Rank rank; + + private static Dictionary suitNames = new Dictionary() + { + { Suit.none, "N"}, + { Suit.clubs, "C"}, + { Suit.diamonds, "D"}, + { Suit.hearts, "H"}, + { Suit.spades, "S"}, + }; + + private static Dictionary rankNames = new Dictionary() + { + { Rank.none, "0"}, + { Rank.two, "2"}, + { Rank.three, "3"}, + { Rank.four, "4"}, + { Rank.five, "5"}, + { Rank.six, "6"}, + { Rank.seven, "7"}, + { Rank.eight, "8"}, + { Rank.nine, "9"}, + { Rank.ten, "10"}, + { Rank.jack, "J"}, + { Rank.queen, "Q"}, + { Rank.king, "K"}, + { Rank.ace, "A"}, + }; public Card(Suit suit, Rank rank) // immutable { @@ -47,28 +73,31 @@ namespace War } // would normally consider suit and rank but in this case we only want to compare rank. - public static bool operator ==(Card lhs, Card rhs) - { - return lhs.rank == rhs.rank; - } + //public static bool operator ==(Card lhs, Card rhs) + //{ + // return lhs.rank == rhs.rank; + //} - public static bool operator !=(Card lhs, Card rhs) - { - return !(lhs == rhs); - } + //public static bool operator !=(Card lhs, Card rhs) + //{ + // return !(lhs == rhs); + //} public static bool operator <(Card lhs, Card rhs) { return lhs.rank < rhs.rank; } + public static bool operator >(Card lhs, Card rhs) { return rhs < lhs; } + public static bool operator <=(Card lhs, Card rhs) { return !(lhs > rhs); } + public static bool operator >=(Card lhs, Card rhs) { return !(lhs < rhs); @@ -76,38 +105,31 @@ namespace War public override string ToString() { - // TODO No need to create the dictionaries each time this is called. - // Also make it static. - // Is there a C# equivalent of an initializer list? - var suitNames = new Dictionary(); - - suitNames[Suit.none] = "N"; - suitNames[Suit.clubs] = "C"; - suitNames[Suit.diamonds] = "D"; - suitNames[Suit.hearts] = "H"; - suitNames[Suit.spades] = "S"; - - var rankNames = new Dictionary(); - rankNames[Rank.none] = "0"; - rankNames[Rank.two] = "2"; - rankNames[Rank.three] = "3"; - rankNames[Rank.four] = "4"; - rankNames[Rank.five] = "5"; - rankNames[Rank.six] = "6"; - rankNames[Rank.seven] = "7"; - rankNames[Rank.eight] = "8"; - rankNames[Rank.nine] = "9"; - rankNames[Rank.ten] = "10"; - rankNames[Rank.jack] = "J"; - rankNames[Rank.queen] = "Q"; - rankNames[Rank.king] = "K"; - rankNames[Rank.ace] = "A"; - return $"{suitNames[suit]}-{rankNames[rank]}"; // string interpolation } } - class Deck + public class Deck { + private const int deckSize = 52; + private Card[] theDeck = new Card[deckSize]; + + public Deck() + { + int i = 0; + for (Suit suit = Suit.clubs; suit <= Suit.spades; suit++) + { + for (Rank rank = Rank.two; rank <= Rank.ace; rank++) + { + theDeck[i] = new Card(suit, rank); + i++; + } + } + } + + public void Shuffle() + { + + } } } diff --git a/94 War/csharp/War/WarTester/Tests.cs b/94 War/csharp/War/WarTester/Tests.cs new file mode 100644 index 00000000..1e811c9e --- /dev/null +++ b/94 War/csharp/War/WarTester/Tests.cs @@ -0,0 +1,90 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using War; + + + +namespace WarTester +{ + [TestClass] + public class CardTest + { + Card c1 = new Card(Suit.clubs, Rank.two); + Card c2 = new Card(Suit.clubs, Rank.ten); + Card c3 = new Card(Suit.diamonds, Rank.ten); + Card c4 = new Card(Suit.diamonds, Rank.ten); + + [TestMethod] + public void LessThan_IsValid() + { + Assert.IsTrue(c1 < c2, "c1 < c2"); // Same suit, different rank. + Assert.IsFalse(c2 < c1, "c2 < c1"); // Same suit, different rank. + + Assert.IsFalse(c3 < c4, "c3 < c4"); // Same suit, same rank. + + Assert.IsTrue(c1 < c3, "c1 < c3"); // Different suit, different rank. + Assert.IsFalse(c3 < c1, "c3 < c1"); // Different suit, different rank. + + Assert.IsFalse(c2 < c4, "c2 < c4"); // Different suit, same rank. + Assert.IsFalse(c4 < c2, "c4 < c2"); // Different suit, same rank. + } + + [TestMethod] + public void GreaterThan_IsValid() + { + Assert.IsFalse(c1 > c2, "c1 > c2"); // Same suit, different rank. + Assert.IsTrue(c2 > c1, "c2 > c1"); // Same suit, different rank. + + Assert.IsFalse(c3 > c4, "c3 > c4"); // Same suit, same rank. + + Assert.IsFalse(c1 > c3, "c1 > c3"); // Different suit, different rank. + Assert.IsTrue(c3 > c1, "c3 > c1"); // Different suit, different rank. + + Assert.IsFalse(c2 > c4, "c2 > c4"); // Different suit, same rank. + Assert.IsFalse(c4 > c2, "c4 > c2"); // Different suit, same rank. + } + + [TestMethod] + public void LessThanEquals_IsValid() + { + Assert.IsTrue(c1 <= c2, "c1 <= c2"); // Same suit, different rank. + Assert.IsFalse(c2 <= c1, "c2 <= c1"); // Same suit, different rank. + + Assert.IsTrue(c3 <= c4, "c3 <= c4"); // Same suit, same rank. + + Assert.IsTrue(c1 <= c3, "c1 <= c3"); // Different suit, different rank. + Assert.IsFalse(c3 <= c1, "c3 <= c1"); // Different suit, different rank. + + Assert.IsTrue(c2 <= c4, "c2 <= c4"); // Different suit, same rank. + Assert.IsTrue(c4 <= c2, "c4 <= c2"); // Different suit, same rank. + } + + [TestMethod] + public void GreaterThanEquals_IsValid() + { + Assert.IsFalse(c1 >= c2, "c1 >= c2"); // Same suit, different rank. + Assert.IsTrue(c2 >= c1, "c2 >= c1"); // Same suit, different rank. + + Assert.IsTrue(c3 >= c4, "c3 >= c4"); // Same suit, same rank. + + Assert.IsFalse(c1 >= c3, "c1 >= c3"); // Different suit, different rank. + Assert.IsTrue(c3 >= c1, "c3 >= c1"); // Different suit, different rank. + + Assert.IsTrue(c2 >= c4, "c2 >= c4"); // Different suit, same rank. + Assert.IsTrue(c4 >= c2, "c4 >= c2"); // Different suit, same rank. + } + + [TestMethod] + public void ToString_IsValid() + { + string s1 = c1.ToString(); + string s2 = c3.ToString(); + string s3 = new Card(Suit.hearts, Rank.queen).ToString(); + string s4 = new Card(Suit.spades, Rank.ace).ToString(); + + Assert.IsTrue(s1 == "C-2", "s1 invalid"); + Assert.IsTrue(s2 == "D-10", "s2 invalid"); + Assert.IsTrue(s3 == "H-Q", "s3 invalid"); + Assert.IsTrue(s4 == "S-A", "s4 invalid"); + } + } +} diff --git a/94 War/csharp/War/WarTester/WarTester.csproj b/94 War/csharp/War/WarTester/WarTester.csproj new file mode 100644 index 00000000..84aa459a --- /dev/null +++ b/94 War/csharp/War/WarTester/WarTester.csproj @@ -0,0 +1,20 @@ + + + + netcoreapp3.1 + + false + + + + + + + + + + + + + + From d36adb2550cca901c90d33d81a38c329881e2c81 Mon Sep 17 00:00:00 2001 From: rbamforth <79797573+rbamforth@users.noreply.github.com> Date: Wed, 24 Mar 2021 12:53:26 +0000 Subject: [PATCH 4/8] Removed Tester project which wasn't working properly. Added shuffling the deck. --- 94 War/csharp/War/War.sln | 6 -- 94 War/csharp/War/War/Cards.cs | 15 +++- 94 War/csharp/War/WarTester/Tests.cs | 90 -------------------- 94 War/csharp/War/WarTester/WarTester.csproj | 20 ----- 4 files changed, 14 insertions(+), 117 deletions(-) delete mode 100644 94 War/csharp/War/WarTester/Tests.cs delete mode 100644 94 War/csharp/War/WarTester/WarTester.csproj diff --git a/94 War/csharp/War/War.sln b/94 War/csharp/War/War.sln index a03a3a64..fcb2d2c2 100644 --- a/94 War/csharp/War/War.sln +++ b/94 War/csharp/War/War.sln @@ -5,8 +5,6 @@ VisualStudioVersion = 16.0.31112.23 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "War", "War\War.csproj", "{C13BE0FA-D8F7-4CA7-A95D-DA03A9DE8950}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WarTester", "WarTester\WarTester.csproj", "{ACB0D6AD-9675-4E72-BB97-BBB2241CB494}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -17,10 +15,6 @@ Global {C13BE0FA-D8F7-4CA7-A95D-DA03A9DE8950}.Debug|Any CPU.Build.0 = Debug|Any CPU {C13BE0FA-D8F7-4CA7-A95D-DA03A9DE8950}.Release|Any CPU.ActiveCfg = Release|Any CPU {C13BE0FA-D8F7-4CA7-A95D-DA03A9DE8950}.Release|Any CPU.Build.0 = Release|Any CPU - {ACB0D6AD-9675-4E72-BB97-BBB2241CB494}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {ACB0D6AD-9675-4E72-BB97-BBB2241CB494}.Debug|Any CPU.Build.0 = Debug|Any CPU - {ACB0D6AD-9675-4E72-BB97-BBB2241CB494}.Release|Any CPU.ActiveCfg = Release|Any CPU - {ACB0D6AD-9675-4E72-BB97-BBB2241CB494}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/94 War/csharp/War/War/Cards.cs b/94 War/csharp/War/War/Cards.cs index 1efdc715..7c31c3b2 100644 --- a/94 War/csharp/War/War/Cards.cs +++ b/94 War/csharp/War/War/Cards.cs @@ -111,7 +111,8 @@ namespace War public class Deck { - private const int deckSize = 52; + public const int deckSize = 52; + private Card[] theDeck = new Card[deckSize]; public Deck() @@ -127,9 +128,21 @@ namespace War } } + public Card GetCard(int i) => theDeck[i]; + public void Shuffle() { + // https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle + for (int i = deckSize - 1; i >= 1; i--) + { + var rand = new Random(); + int j = rand.Next(0, i); + // Swap the cards at i and j + Card temp = theDeck[j]; + theDeck[j] = theDeck[i]; + theDeck[i] = temp; + } } } } diff --git a/94 War/csharp/War/WarTester/Tests.cs b/94 War/csharp/War/WarTester/Tests.cs deleted file mode 100644 index 1e811c9e..00000000 --- a/94 War/csharp/War/WarTester/Tests.cs +++ /dev/null @@ -1,90 +0,0 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using War; - - - -namespace WarTester -{ - [TestClass] - public class CardTest - { - Card c1 = new Card(Suit.clubs, Rank.two); - Card c2 = new Card(Suit.clubs, Rank.ten); - Card c3 = new Card(Suit.diamonds, Rank.ten); - Card c4 = new Card(Suit.diamonds, Rank.ten); - - [TestMethod] - public void LessThan_IsValid() - { - Assert.IsTrue(c1 < c2, "c1 < c2"); // Same suit, different rank. - Assert.IsFalse(c2 < c1, "c2 < c1"); // Same suit, different rank. - - Assert.IsFalse(c3 < c4, "c3 < c4"); // Same suit, same rank. - - Assert.IsTrue(c1 < c3, "c1 < c3"); // Different suit, different rank. - Assert.IsFalse(c3 < c1, "c3 < c1"); // Different suit, different rank. - - Assert.IsFalse(c2 < c4, "c2 < c4"); // Different suit, same rank. - Assert.IsFalse(c4 < c2, "c4 < c2"); // Different suit, same rank. - } - - [TestMethod] - public void GreaterThan_IsValid() - { - Assert.IsFalse(c1 > c2, "c1 > c2"); // Same suit, different rank. - Assert.IsTrue(c2 > c1, "c2 > c1"); // Same suit, different rank. - - Assert.IsFalse(c3 > c4, "c3 > c4"); // Same suit, same rank. - - Assert.IsFalse(c1 > c3, "c1 > c3"); // Different suit, different rank. - Assert.IsTrue(c3 > c1, "c3 > c1"); // Different suit, different rank. - - Assert.IsFalse(c2 > c4, "c2 > c4"); // Different suit, same rank. - Assert.IsFalse(c4 > c2, "c4 > c2"); // Different suit, same rank. - } - - [TestMethod] - public void LessThanEquals_IsValid() - { - Assert.IsTrue(c1 <= c2, "c1 <= c2"); // Same suit, different rank. - Assert.IsFalse(c2 <= c1, "c2 <= c1"); // Same suit, different rank. - - Assert.IsTrue(c3 <= c4, "c3 <= c4"); // Same suit, same rank. - - Assert.IsTrue(c1 <= c3, "c1 <= c3"); // Different suit, different rank. - Assert.IsFalse(c3 <= c1, "c3 <= c1"); // Different suit, different rank. - - Assert.IsTrue(c2 <= c4, "c2 <= c4"); // Different suit, same rank. - Assert.IsTrue(c4 <= c2, "c4 <= c2"); // Different suit, same rank. - } - - [TestMethod] - public void GreaterThanEquals_IsValid() - { - Assert.IsFalse(c1 >= c2, "c1 >= c2"); // Same suit, different rank. - Assert.IsTrue(c2 >= c1, "c2 >= c1"); // Same suit, different rank. - - Assert.IsTrue(c3 >= c4, "c3 >= c4"); // Same suit, same rank. - - Assert.IsFalse(c1 >= c3, "c1 >= c3"); // Different suit, different rank. - Assert.IsTrue(c3 >= c1, "c3 >= c1"); // Different suit, different rank. - - Assert.IsTrue(c2 >= c4, "c2 >= c4"); // Different suit, same rank. - Assert.IsTrue(c4 >= c2, "c4 >= c2"); // Different suit, same rank. - } - - [TestMethod] - public void ToString_IsValid() - { - string s1 = c1.ToString(); - string s2 = c3.ToString(); - string s3 = new Card(Suit.hearts, Rank.queen).ToString(); - string s4 = new Card(Suit.spades, Rank.ace).ToString(); - - Assert.IsTrue(s1 == "C-2", "s1 invalid"); - Assert.IsTrue(s2 == "D-10", "s2 invalid"); - Assert.IsTrue(s3 == "H-Q", "s3 invalid"); - Assert.IsTrue(s4 == "S-A", "s4 invalid"); - } - } -} diff --git a/94 War/csharp/War/WarTester/WarTester.csproj b/94 War/csharp/War/WarTester/WarTester.csproj deleted file mode 100644 index 84aa459a..00000000 --- a/94 War/csharp/War/WarTester/WarTester.csproj +++ /dev/null @@ -1,20 +0,0 @@ - - - - netcoreapp3.1 - - false - - - - - - - - - - - - - - From e9987a3269f8b98aee76002ea80356251109eec5 Mon Sep 17 00:00:00 2001 From: rbamforth <79797573+rbamforth@users.noreply.github.com> Date: Thu, 25 Mar 2021 23:38:35 +0000 Subject: [PATCH 5/8] Reinstated Tester project. The Visual Studio test runner still doesn't display any test results but this is common to all solutions. It's a VS issue, not an issue with this Program. First cut at a working program. --- 94 War/csharp/War/War.sln | 6 +++ 94 War/csharp/War/War/Program.cs | 90 +++++++++++++++++++++++++++++++- 2 files changed, 95 insertions(+), 1 deletion(-) diff --git a/94 War/csharp/War/War.sln b/94 War/csharp/War/War.sln index fcb2d2c2..af1e88a5 100644 --- a/94 War/csharp/War/War.sln +++ b/94 War/csharp/War/War.sln @@ -5,6 +5,8 @@ VisualStudioVersion = 16.0.31112.23 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "War", "War\War.csproj", "{C13BE0FA-D8F7-4CA7-A95D-DA03A9DE8950}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WarTester", "WarTester\WarTester.csproj", "{B539F618-EE83-486C-9A6D-404E998BED2D}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +17,10 @@ Global {C13BE0FA-D8F7-4CA7-A95D-DA03A9DE8950}.Debug|Any CPU.Build.0 = Debug|Any CPU {C13BE0FA-D8F7-4CA7-A95D-DA03A9DE8950}.Release|Any CPU.ActiveCfg = Release|Any CPU {C13BE0FA-D8F7-4CA7-A95D-DA03A9DE8950}.Release|Any CPU.Build.0 = Release|Any CPU + {B539F618-EE83-486C-9A6D-404E998BED2D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B539F618-EE83-486C-9A6D-404E998BED2D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B539F618-EE83-486C-9A6D-404E998BED2D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B539F618-EE83-486C-9A6D-404E998BED2D}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/94 War/csharp/War/War/Program.cs b/94 War/csharp/War/War/Program.cs index cf4b0edf..68b602f8 100644 --- a/94 War/csharp/War/War/Program.cs +++ b/94 War/csharp/War/War/Program.cs @@ -2,11 +2,99 @@ namespace War { + public class Intro + { + public void WriteIntro() + { + Console.WriteLine(" WAR"); + Console.WriteLine(" CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"); + Console.WriteLine(); + Console.WriteLine(); + Console.WriteLine(); + + Console.WriteLine("THIS IS THE CARD GAME OF WAR. EACH CARD IS GIVEN BY SUIT-#"); + Console.WriteLine("AS S-7 FOR SPADE 7. "); + + if (AskQuestion("DO YOU WANT DIRECTIONS? ")) + { + Console.WriteLine("THE COMPUTER GIVES YOU AND IT A 'CARD'. THE HIGHER CARD"); + Console.WriteLine("(NUMERICALLY) WINS. THE GAME ENDS WHEN YOU CHOOSE NOT TO"); + Console.WriteLine("CONTINUE OR WHEN YOU HAVE FINISHED THE PACK."); + } + + Console.WriteLine(); + Console.WriteLine(); + } + + public bool AskQuestion(string question) + { + while (true) + { + Console.Write(question); + string result = Console.ReadLine(); + + if (result.ToLower()[0] == 'y') + { + return true; + } + else /*if (result.ToLower() == "no")*/ + { + return false; + } + + Console.WriteLine("YES OR NO, PLEASE."); + } + } + } + class Program { static void Main(string[] args) { - Console.WriteLine("Hello World!"); + var intro = new Intro(); + intro.WriteIntro(); + + var deck = new Deck(); + deck.Shuffle(); + + int yourScore = 0; + int computersScore = 0; + bool usedAllCards = true; + + for (int i = 0; i < Deck.deckSize; i += 2) + { + var yourCard = deck.GetCard(i); + var computersCard = deck.GetCard(i + 1); + + Console.WriteLine($"YOU: {yourCard} COMPUTER: {computersCard}"); + if (yourCard < computersCard) + { + computersScore++; + Console.WriteLine($"THE COMPUTER WINS!!! YOU HAVE {yourScore} AND THE COMPUTER HAS {computersScore}"); + } + else if (yourCard > computersCard) + { + yourScore++; + Console.WriteLine($"YOU WIN. YOU HAVE {yourScore} AND THE COMPUTER HAS {computersScore}"); + } + else + { + Console.WriteLine("TIE. NO SCORE CHANGE"); + } + + if (!intro.AskQuestion("DO YOU WANT TO CONTINUE? ")) + { + usedAllCards = false; + break; + } + } + + if (usedAllCards) + { + Console.WriteLine("WE HAVE RUN OUT OF CARDS."); + } + Console.WriteLine($"FINAL SCORE: YOU: {yourScore} THE COMPUTER: {computersScore}"); + Console.WriteLine("THANKS FOR PLAYING. IT WAS FUN."); } } } From 0d35b103b06a5c650582c615a2fd142d1011badc Mon Sep 17 00:00:00 2001 From: rbamforth <79797573+rbamforth@users.noreply.github.com> Date: Fri, 26 Mar 2021 15:39:15 +0000 Subject: [PATCH 6/8] Added Tester files. --- 94 War/csharp/War/WarTester/Tests.cs | 126 +++++++++++++++++++ 94 War/csharp/War/WarTester/WarTester.csproj | 29 +++++ 2 files changed, 155 insertions(+) create mode 100644 94 War/csharp/War/WarTester/Tests.cs create mode 100644 94 War/csharp/War/WarTester/WarTester.csproj diff --git a/94 War/csharp/War/WarTester/Tests.cs b/94 War/csharp/War/WarTester/Tests.cs new file mode 100644 index 00000000..313d2a1e --- /dev/null +++ b/94 War/csharp/War/WarTester/Tests.cs @@ -0,0 +1,126 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Text; +using War; + + + +namespace WarTester +{ + [TestClass] + public class CardTest + { + private Card c1 = new Card(Suit.clubs, Rank.two); + private Card c2 = new Card(Suit.clubs, Rank.ten); + private Card c3 = new Card(Suit.diamonds, Rank.ten); + private Card c4 = new Card(Suit.diamonds, Rank.ten); + + [TestMethod] + public void LessThanIsValid() + { + Assert.IsTrue(c1 < c2, "c1 < c2"); // Same suit, different rank. + Assert.IsFalse(c2 < c1, "c2 < c1"); // Same suit, different rank. + + Assert.IsFalse(c3 < c4, "c3 < c4"); // Same suit, same rank. + + Assert.IsTrue(c1 < c3, "c1 < c3"); // Different suit, different rank. + Assert.IsFalse(c3 < c1, "c3 < c1"); // Different suit, different rank. + + Assert.IsFalse(c2 < c4, "c2 < c4"); // Different suit, same rank. + Assert.IsFalse(c4 < c2, "c4 < c2"); // Different suit, same rank. + } + + [TestMethod] + public void GreaterThanIsValid() + { + Assert.IsFalse(c1 > c2, "c1 > c2"); // Same suit, different rank. + Assert.IsTrue(c2 > c1, "c2 > c1"); // Same suit, different rank. + + Assert.IsFalse(c3 > c4, "c3 > c4"); // Same suit, same rank. + + Assert.IsFalse(c1 > c3, "c1 > c3"); // Different suit, different rank. + Assert.IsTrue(c3 > c1, "c3 > c1"); // Different suit, different rank. + + Assert.IsFalse(c2 > c4, "c2 > c4"); // Different suit, same rank. + Assert.IsFalse(c4 > c2, "c4 > c2"); // Different suit, same rank. + } + + [TestMethod] + public void LessThanEqualsIsValid() + { + Assert.IsTrue(c1 <= c2, "c1 <= c2"); // Same suit, different rank. + Assert.IsFalse(c2 <= c1, "c2 <= c1"); // Same suit, different rank. + + Assert.IsTrue(c3 <= c4, "c3 <= c4"); // Same suit, same rank. + + Assert.IsTrue(c1 <= c3, "c1 <= c3"); // Different suit, different rank. + Assert.IsFalse(c3 <= c1, "c3 <= c1"); // Different suit, different rank. + + Assert.IsTrue(c2 <= c4, "c2 <= c4"); // Different suit, same rank. + Assert.IsTrue(c4 <= c2, "c4 <= c2"); // Different suit, same rank. + } + + [TestMethod] + public void GreaterThanEqualsIsValid() + { + Assert.IsFalse(c1 >= c2, "c1 >= c2"); // Same suit, different rank. + Assert.IsTrue(c2 >= c1, "c2 >= c1"); // Same suit, different rank. + + Assert.IsTrue(c3 >= c4, "c3 >= c4"); // Same suit, same rank. + + Assert.IsFalse(c1 >= c3, "c1 >= c3"); // Different suit, different rank. + Assert.IsTrue(c3 >= c1, "c3 >= c1"); // Different suit, different rank. + + Assert.IsTrue(c2 >= c4, "c2 >= c4"); // Different suit, same rank. + Assert.IsTrue(c4 >= c2, "c4 >= c2"); // Different suit, same rank. + } + + [TestMethod] + public void ToStringIsValid() + { + string s1 = c1.ToString(); + string s2 = c3.ToString(); + string s3 = new Card(Suit.hearts, Rank.queen).ToString(); + string s4 = new Card(Suit.spades, Rank.ace).ToString(); + + Assert.IsTrue(s1 == "C-2", "s1 invalid"); + Assert.IsTrue(s2 == "D-10", "s2 invalid"); + Assert.IsTrue(s3 == "H-Q", "s3 invalid"); + Assert.IsTrue(s4 == "S-A", "s4 invalid"); + } + } + + [TestClass] + public class DeckTest + { + private string ConcatenateDeck(Deck d) + { + StringBuilder sb = new StringBuilder(); + + for (int i = 0; i < Deck.deckSize; i++) + { + sb.Append(d.GetCard(i)); + } + + return sb.ToString(); + } + + [TestMethod] + public void InitialDeckContainsCardsInOrder() + { + Deck d1 = new Deck(); + string allTheCards = ConcatenateDeck(d1); + + Assert.IsTrue(allTheCards == "C-2C-3C-4C-5C-6C-7C-8C-9C-10C-JC-QC-KC-AD-2D-3D-4D-5D-6D-7D-8D-9D-10D-JD-QD-KD-AH-2H-3H-4H-5H-6H-7H-8H-9H-10H-JH-QH-KH-AS-2S-3S-4S-5S-6S-7S-8S-9S-10S-JS-QS-KS-A"); + } + + [TestMethod] + public void ShufflingChangesDeck() + { + Deck d1 = new Deck(); + d1.Shuffle(); + string allTheCards = ConcatenateDeck(d1); + + Assert.IsTrue(allTheCards != "C-2C-3C-4C-5C-6C-7C-8C-9C-10C-JC-QC-KC-AD-2D-3D-4D-5D-6D-7D-8D-9D-10D-JD-QD-KD-AH-2H-3H-4H-5H-6H-7H-8H-9H-10H-JH-QH-KH-AS-2S-3S-4S-5S-6S-7S-8S-9S-10S-JS-QS-KS-A"); + } + } +} diff --git a/94 War/csharp/War/WarTester/WarTester.csproj b/94 War/csharp/War/WarTester/WarTester.csproj new file mode 100644 index 00000000..9d1808d6 --- /dev/null +++ b/94 War/csharp/War/WarTester/WarTester.csproj @@ -0,0 +1,29 @@ + + + + netcoreapp3.1 + + false + + AnyCPU + + + + true + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + From b4655d577745f9aa03e1e7ffc9fa5e3c5b9ca813 Mon Sep 17 00:00:00 2001 From: rbamforth <79797573+rbamforth@users.noreply.github.com> Date: Fri, 26 Mar 2021 22:51:28 +0000 Subject: [PATCH 7/8] Moved UI stuff into its own class. Minor changes, commenting and tidying up the other classes. --- 94 War/csharp/War/War/Cards.cs | 52 +++++++++++----- 94 War/csharp/War/War/Program.cs | 79 +++--------------------- 94 War/csharp/War/War/UserInterface.cs | 83 ++++++++++++++++++++++++++ 94 War/csharp/War/WarTester/Tests.cs | 30 ++++++---- 4 files changed, 145 insertions(+), 99 deletions(-) create mode 100644 94 War/csharp/War/War/UserInterface.cs diff --git a/94 War/csharp/War/War/Cards.cs b/94 War/csharp/War/War/Cards.cs index 7c31c3b2..99e03020 100644 --- a/94 War/csharp/War/War/Cards.cs +++ b/94 War/csharp/War/War/Cards.cs @@ -1,12 +1,13 @@ using System; using System.Collections.Generic; -using System.Text; + + namespace War { + // These enums define the card's suit and rank. public enum Suit { - none = 0, clubs, diamonds, hearts, @@ -15,7 +16,6 @@ namespace War public enum Rank { - none = 0, // Skip 1 because ace is high. two = 2, three, @@ -32,25 +32,25 @@ namespace War ace } - // TODO Testing - + // A class to represent a playing card. public class Card { + // A card is an immutable object (i.e. it can't be changed) so its suit + // and rank value are readonly; they can only be set in the constructor. private readonly Suit suit; private readonly Rank rank; - private static Dictionary suitNames = new Dictionary() + // These dictionaries are used to convert a suit or rank value into a string. + private readonly Dictionary suitNames = new Dictionary() { - { Suit.none, "N"}, { Suit.clubs, "C"}, { Suit.diamonds, "D"}, { Suit.hearts, "H"}, { Suit.spades, "S"}, }; - private static Dictionary rankNames = new Dictionary() + private readonly Dictionary rankNames = new Dictionary() { - { Rank.none, "0"}, { Rank.two, "2"}, { Rank.three, "3"}, { Rank.four, "4"}, @@ -66,18 +66,30 @@ namespace War { Rank.ace, "A"}, }; - public Card(Suit suit, Rank rank) // immutable + public Card(Suit suit, Rank rank) { this.suit = suit; this.rank = rank; } - // would normally consider suit and rank but in this case we only want to compare rank. + // Relational Operator Overloading. + // + // You would normally expect the relational operators to consider both the suit and the + // rank of a card, but in this program suit doesn't matter so we define the operators to just + // compare rank. + + // When adding relational operators we would normally include == and != but they are not + // relevant to this program so haven't been defined. Note that if they were defined we + // should also override the Equals() and GetHashCode() methods. See, for example: + // http://www.blackwasp.co.uk/CSharpRelationalOverload.aspx + + // If the == and != operators were defined they would look like this: + // //public static bool operator ==(Card lhs, Card rhs) //{ // return lhs.rank == rhs.rank; //} - + // //public static bool operator !=(Card lhs, Card rhs) //{ // return !(lhs == rhs); @@ -105,10 +117,12 @@ namespace War public override string ToString() { - return $"{suitNames[suit]}-{rankNames[rank]}"; // string interpolation + // N.B. We are using string interpolation to create the card name. + return $"{suitNames[suit]}-{rankNames[rank]}"; } } + // A class to represent a deck of cards. public class Deck { public const int deckSize = 52; @@ -117,6 +131,7 @@ namespace War public Deck() { + // Populate theDeck with all the cards in order. int i = 0; for (Suit suit = Suit.clubs; suit <= Suit.spades; suit++) { @@ -128,14 +143,21 @@ namespace War } } + // Return the card at a particular position in the deck. + // N.B. As this is such a short method, we make it an + // expression-body method. public Card GetCard(int i) => theDeck[i]; + // Shuffle the cards, this uses the modern version of the + // Fisher-Yates shuffle, see: + // https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm public void Shuffle() { - // https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle + var rand = new Random(); + + // Iterate backwards through the deck. for (int i = deckSize - 1; i >= 1; i--) { - var rand = new Random(); int j = rand.Next(0, i); // Swap the cards at i and j diff --git a/94 War/csharp/War/War/Program.cs b/94 War/csharp/War/War/Program.cs index 68b602f8..57a6c37e 100644 --- a/94 War/csharp/War/War/Program.cs +++ b/94 War/csharp/War/War/Program.cs @@ -1,58 +1,11 @@ -using System; - -namespace War +namespace War { - public class Intro - { - public void WriteIntro() - { - Console.WriteLine(" WAR"); - Console.WriteLine(" CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"); - Console.WriteLine(); - Console.WriteLine(); - Console.WriteLine(); - - Console.WriteLine("THIS IS THE CARD GAME OF WAR. EACH CARD IS GIVEN BY SUIT-#"); - Console.WriteLine("AS S-7 FOR SPADE 7. "); - - if (AskQuestion("DO YOU WANT DIRECTIONS? ")) - { - Console.WriteLine("THE COMPUTER GIVES YOU AND IT A 'CARD'. THE HIGHER CARD"); - Console.WriteLine("(NUMERICALLY) WINS. THE GAME ENDS WHEN YOU CHOOSE NOT TO"); - Console.WriteLine("CONTINUE OR WHEN YOU HAVE FINISHED THE PACK."); - } - - Console.WriteLine(); - Console.WriteLine(); - } - - public bool AskQuestion(string question) - { - while (true) - { - Console.Write(question); - string result = Console.ReadLine(); - - if (result.ToLower()[0] == 'y') - { - return true; - } - else /*if (result.ToLower() == "no")*/ - { - return false; - } - - Console.WriteLine("YES OR NO, PLEASE."); - } - } - } - class Program { static void Main(string[] args) { - var intro = new Intro(); - intro.WriteIntro(); + var ui = new UserInterface(); + ui.WriteIntro(); var deck = new Deck(); deck.Shuffle(); @@ -63,38 +16,20 @@ namespace War for (int i = 0; i < Deck.deckSize; i += 2) { + // Play the next hand. var yourCard = deck.GetCard(i); var computersCard = deck.GetCard(i + 1); - Console.WriteLine($"YOU: {yourCard} COMPUTER: {computersCard}"); - if (yourCard < computersCard) - { - computersScore++; - Console.WriteLine($"THE COMPUTER WINS!!! YOU HAVE {yourScore} AND THE COMPUTER HAS {computersScore}"); - } - else if (yourCard > computersCard) - { - yourScore++; - Console.WriteLine($"YOU WIN. YOU HAVE {yourScore} AND THE COMPUTER HAS {computersScore}"); - } - else - { - Console.WriteLine("TIE. NO SCORE CHANGE"); - } + ui.WriteAResult(yourCard, computersCard, ref computersScore, ref yourScore); - if (!intro.AskQuestion("DO YOU WANT TO CONTINUE? ")) + if (!ui.AskAQuestion("DO YOU WANT TO CONTINUE? ")) { usedAllCards = false; break; } } - if (usedAllCards) - { - Console.WriteLine("WE HAVE RUN OUT OF CARDS."); - } - Console.WriteLine($"FINAL SCORE: YOU: {yourScore} THE COMPUTER: {computersScore}"); - Console.WriteLine("THANKS FOR PLAYING. IT WAS FUN."); + ui.WriteClosingRemarks(usedAllCards, yourScore, computersScore); } } } diff --git a/94 War/csharp/War/War/UserInterface.cs b/94 War/csharp/War/War/UserInterface.cs new file mode 100644 index 00000000..ee93d9fa --- /dev/null +++ b/94 War/csharp/War/War/UserInterface.cs @@ -0,0 +1,83 @@ +using System; + + + +namespace War +{ + // This class displays all the text that the user sees when playing the game. + // It also handles asking the user a yes/no question and returning their answer. + public class UserInterface + { + public void WriteIntro() + { + Console.WriteLine(" WAR"); + Console.WriteLine(" CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"); + Console.WriteLine(); + + Console.WriteLine("THIS IS THE CARD GAME OF WAR. EACH CARD IS GIVEN BY SUIT-#"); + Console.Write("AS S-7 FOR SPADE 7. "); + + if (AskAQuestion("DO YOU WANT DIRECTIONS? ")) + { + Console.WriteLine("THE COMPUTER GIVES YOU AND IT A 'CARD'. THE HIGHER CARD"); + Console.WriteLine("(NUMERICALLY) WINS. THE GAME ENDS WHEN YOU CHOOSE NOT TO"); + Console.WriteLine("CONTINUE OR WHEN YOU HAVE FINISHED THE PACK."); + } + + Console.WriteLine(); + Console.WriteLine(); + } + + public void WriteAResult(Card yourCard, Card computersCard, ref int computersScore, ref int yourScore) + { + Console.WriteLine($"YOU: {yourCard} COMPUTER: {computersCard}"); + if (yourCard < computersCard) + { + computersScore++; + Console.WriteLine($"THE COMPUTER WINS!!! YOU HAVE {yourScore} AND THE COMPUTER HAS {computersScore}"); + } + else if (yourCard > computersCard) + { + yourScore++; + Console.WriteLine($"YOU WIN. YOU HAVE {yourScore} AND THE COMPUTER HAS {computersScore}"); + } + else + { + Console.WriteLine("TIE. NO SCORE CHANGE"); + } + } + + public bool AskAQuestion(string question) + { + // Repeat asking the question until the user answers "YES" or "NO". + while (true) + { + Console.Write(question); + string result = Console.ReadLine(); + + if (result.ToLower() == "yes") + { + Console.WriteLine(); + return true; + } + else if (result.ToLower() == "no") + { + Console.WriteLine(); + return false; + } + + Console.WriteLine("YES OR NO, PLEASE."); + } + } + + public void WriteClosingRemarks(bool usedAllCards, int yourScore, int computersScore) + { + if (usedAllCards) + { + Console.WriteLine("WE HAVE RUN OUT OF CARDS."); + } + Console.WriteLine($"FINAL SCORE: YOU: {yourScore} THE COMPUTER: {computersScore}"); + Console.WriteLine("THANKS FOR PLAYING. IT WAS FUN."); + } + } +} diff --git a/94 War/csharp/War/WarTester/Tests.cs b/94 War/csharp/War/WarTester/Tests.cs index 313d2a1e..387b4547 100644 --- a/94 War/csharp/War/WarTester/Tests.cs +++ b/94 War/csharp/War/WarTester/Tests.cs @@ -14,6 +14,8 @@ namespace WarTester private Card c3 = new Card(Suit.diamonds, Rank.ten); private Card c4 = new Card(Suit.diamonds, Rank.ten); + // Test the relational operators. + [TestMethod] public void LessThanIsValid() { @@ -77,10 +79,10 @@ namespace WarTester [TestMethod] public void ToStringIsValid() { - string s1 = c1.ToString(); - string s2 = c3.ToString(); - string s3 = new Card(Suit.hearts, Rank.queen).ToString(); - string s4 = new Card(Suit.spades, Rank.ace).ToString(); + var s1 = c1.ToString(); + var s2 = c3.ToString(); + var s3 = new Card(Suit.hearts, Rank.queen).ToString(); + var s4 = new Card(Suit.spades, Rank.ace).ToString(); Assert.IsTrue(s1 == "C-2", "s1 invalid"); Assert.IsTrue(s2 == "D-10", "s2 invalid"); @@ -92,7 +94,10 @@ namespace WarTester [TestClass] public class DeckTest { - private string ConcatenateDeck(Deck d) + private readonly string cardNamesInOrder = "C-2C-3C-4C-5C-6C-7C-8C-9C-10C-JC-QC-KC-AD-2D-3D-4D-5D-6D-7D-8D-9D-10D-JD-QD-KD-AH-2H-3H-4H-5H-6H-7H-8H-9H-10H-JH-QH-KH-AS-2S-3S-4S-5S-6S-7S-8S-9S-10S-JS-QS-KS-A"; + + //Helper method. Adds the names of all the cards together into a single string. + private string ConcatenateTheDeck(Deck d) { StringBuilder sb = new StringBuilder(); @@ -107,20 +112,21 @@ namespace WarTester [TestMethod] public void InitialDeckContainsCardsInOrder() { - Deck d1 = new Deck(); - string allTheCards = ConcatenateDeck(d1); + Deck d = new Deck(); + string allTheCards = ConcatenateTheDeck(d); - Assert.IsTrue(allTheCards == "C-2C-3C-4C-5C-6C-7C-8C-9C-10C-JC-QC-KC-AD-2D-3D-4D-5D-6D-7D-8D-9D-10D-JD-QD-KD-AH-2H-3H-4H-5H-6H-7H-8H-9H-10H-JH-QH-KH-AS-2S-3S-4S-5S-6S-7S-8S-9S-10S-JS-QS-KS-A"); + Assert.IsTrue(allTheCards == cardNamesInOrder); } [TestMethod] public void ShufflingChangesDeck() { - Deck d1 = new Deck(); - d1.Shuffle(); - string allTheCards = ConcatenateDeck(d1); + // I'm not sure how to test that shuffling has worked other than to check that the cards aren't in the initial order. + Deck d = new Deck(); + d.Shuffle(); + string allTheCards = ConcatenateTheDeck(d); - Assert.IsTrue(allTheCards != "C-2C-3C-4C-5C-6C-7C-8C-9C-10C-JC-QC-KC-AD-2D-3D-4D-5D-6D-7D-8D-9D-10D-JD-QD-KD-AH-2H-3H-4H-5H-6H-7H-8H-9H-10H-JH-QH-KH-AS-2S-3S-4S-5S-6S-7S-8S-9S-10S-JS-QS-KS-A"); + Assert.IsTrue(allTheCards != cardNamesInOrder); } } } From 191b77f54c302024bc8c63163ebafe3fe6f9746a Mon Sep 17 00:00:00 2001 From: rbamforth <79797573+rbamforth@users.noreply.github.com> Date: Fri, 26 Mar 2021 23:03:15 +0000 Subject: [PATCH 8/8] Fixed tabs, spaces mismatch. --- 94 War/csharp/War/War/Program.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/94 War/csharp/War/War/Program.cs b/94 War/csharp/War/War/Program.cs index 57a6c37e..7ab335e3 100644 --- a/94 War/csharp/War/War/Program.cs +++ b/94 War/csharp/War/War/Program.cs @@ -5,27 +5,27 @@ static void Main(string[] args) { var ui = new UserInterface(); - ui.WriteIntro(); + ui.WriteIntro(); - var deck = new Deck(); - deck.Shuffle(); + var deck = new Deck(); + deck.Shuffle(); - int yourScore = 0; + int yourScore = 0; int computersScore = 0; - bool usedAllCards = true; + bool usedAllCards = true; for (int i = 0; i < Deck.deckSize; i += 2) { // Play the next hand. - var yourCard = deck.GetCard(i); + var yourCard = deck.GetCard(i); var computersCard = deck.GetCard(i + 1); ui.WriteAResult(yourCard, computersCard, ref computersScore, ref yourScore); if (!ui.AskAQuestion("DO YOU WANT TO CONTINUE? ")) { - usedAllCards = false; - break; + usedAllCards = false; + break; } }