mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-22 23:26:40 -08:00
Added Tester project and tests for the Card class.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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<Suit, string> suitNames = new Dictionary<Suit, string>()
|
||||
{
|
||||
{ Suit.none, "N"},
|
||||
{ Suit.clubs, "C"},
|
||||
{ Suit.diamonds, "D"},
|
||||
{ Suit.hearts, "H"},
|
||||
{ Suit.spades, "S"},
|
||||
};
|
||||
|
||||
private static Dictionary<Rank, string> rankNames = new Dictionary<Rank, string>()
|
||||
{
|
||||
{ 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<Suit, string>();
|
||||
|
||||
suitNames[Suit.none] = "N";
|
||||
suitNames[Suit.clubs] = "C";
|
||||
suitNames[Suit.diamonds] = "D";
|
||||
suitNames[Suit.hearts] = "H";
|
||||
suitNames[Suit.spades] = "S";
|
||||
|
||||
var rankNames = new Dictionary<Rank, string>();
|
||||
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()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
90
94 War/csharp/War/WarTester/Tests.cs
Normal file
90
94 War/csharp/War/WarTester/Tests.cs
Normal file
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
20
94 War/csharp/War/WarTester/WarTester.csproj
Normal file
20
94 War/csharp/War/WarTester/WarTester.csproj
Normal file
@@ -0,0 +1,20 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
|
||||
<PackageReference Include="MSTest.TestAdapter" Version="2.1.0" />
|
||||
<PackageReference Include="MSTest.TestFramework" Version="2.1.0" />
|
||||
<PackageReference Include="coverlet.collector" Version="1.2.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\War\War.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user