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 + + + + + + + + + + + + + +