mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-22 23:26:40 -08:00
Removed Tester project which wasn't working properly.
Added shuffling the deck.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
<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