This commit is contained in:
2022-07-06 10:52:16 +10:00
parent 4f2c4ec90a
commit c01a1b3228
2 changed files with 37 additions and 62 deletions

View File

@@ -1,43 +1,18 @@
namespace Cards namespace PlayingCards
{ {
public enum Suit public enum Suit { Spades, Clubs, Diamonds, Hearts }
{
Spades,
Clubs,
Diamonds,
Hearts
}
public enum PictureCard public enum PictureCard { Jack = 1, Queen, King, Ace }
{
Jack = 1,
Queen,
King,
Ace
}
public class Card public class Card
{ {
public string Value; public string Value;
public Suit Suit; public Suit Suit;
public string NamedValue public string NamedValue => Value + " of " + Suit;
{ public bool IsPicture => Enum.IsDefined(typeof(PictureCard), Value);
get => Value + " of " + Suit; public bool IsNumber => !Enum.IsDefined(typeof(PictureCard), Value);
} public bool IsBlack => (Enum.GetName(Suit) == "Spades" || Enum.GetName(Suit) == "Clubs");
public Boolean IsPicture public bool IsRed => (Enum.GetName(Suit) == "Diamonds" || Enum.GetName(Suit) == "Hearts");
{
get => Enum.IsDefined(typeof(PictureCard), Value);
}
public Boolean IsBlack
{
get => (Enum.GetName(Suit) == "Spades" || Enum.GetName(Suit) == "Clubs");
}
public Boolean IsRed
{
get => (Enum.GetName(Suit) == "Diamonds" || Enum.GetName(Suit) == "Hearts");
}
} }
public class Deck public class Deck
@@ -68,8 +43,8 @@
} }
} }
} }
private static Random rng = new();
private static Random rng = new();
public static void Shuffle() public static void Shuffle()
{ {
Cards = Cards.OrderBy(a => rng.Next()).ToList(); Cards = Cards.OrderBy(a => rng.Next()).ToList();
@@ -78,7 +53,7 @@
public class Actions public class Actions
{ {
public static Boolean MoveCard(List<Card> From, List<Card> To, Card card = null) public static bool MoveCard(List<Card> From, List<Card> To, Card card = null)
{ {
if (From.Count() == 0) if (From.Count() == 0)
{ {
@@ -86,7 +61,9 @@
} }
if (card is null) if (card is null)
card = From.FirstOrDefault(); {
card = From.FirstOrDefault(); // FIFO
}
if (From.Contains(card) && !To.Contains(card)) if (From.Contains(card) && !To.Contains(card))
{ {
@@ -94,7 +71,7 @@
From.Remove(card); From.Remove(card);
return true; return true;
} }
return false; return false; // If we're here, something went bang
} }
} }
} }

View File

@@ -1,8 +1,8 @@
using Cards; using PlayingCards;
namespace StripJackNaked namespace StripJackNaked
{ {
static class Constants internal static class Constants
{ {
public const int PlayerCount = 2; public const int PlayerCount = 2;
} }
@@ -19,38 +19,35 @@ namespace StripJackNaked
class Program class Program
{ {
public static int NextPlayer(int CurrentPlayer) public static int NextPlayer(int CurrentPlayer) { return ++CurrentPlayer % Constants.PlayerCount; }
{
return ++CurrentPlayer % Constants.PlayerCount;
}
public static Boolean PlayCard(List<Card> From) public static bool PlayCard(List<Card> From) { return Actions.MoveCard(From, Kitty.Pile); }
{
return Actions.MoveCard(From, Kitty.Pile);
}
public static void TakePile(List<Card>Winner) public static void TakePile(List<Card> Winner)
{ {
while (Kitty.Pile.Count() > 0) while (Kitty.Pile.Count() > 0)
{ {
Actions.MoveCard(Kitty.Pile, Winner); _ = Actions.MoveCard(Kitty.Pile, Winner);
} }
} }
public static void ShowList(List<Card>Cards) public static void ShowList(List<Card> Cards)
{ {
foreach (Card card in Cards) foreach (Card card in Cards)
{
Console.Write(card.NamedValue + ((card == Cards.LastOrDefault()) ? "" : ", ")); Console.Write(card.NamedValue + ((card == Cards.LastOrDefault()) ? "" : ", "));
}
Console.WriteLine(); Console.WriteLine();
} }
static void Main() private static void Main()
{ {
// Init players // Init players
List<Player> Players = new(); List<Player> Players = new();
for (int i = 0; i < Constants.PlayerCount; i++) for (int i = 0; i < Constants.PlayerCount; i++)
{
Players.Add(new Player()); Players.Add(new Player());
}
// Init deck // Init deck
Deck.Enumerate(); Deck.Enumerate();
@@ -61,10 +58,9 @@ namespace StripJackNaked
{ {
Card card = Deck.Cards.Last(); Card card = Deck.Cards.Last();
int player = Deck.Cards.Count() % Constants.PlayerCount; // Alternate between players 0 and 1 int player = Deck.Cards.Count() % Constants.PlayerCount; // Alternate between players 0 and 1
Actions.MoveCard(Deck.Cards, Players[player].Hand); _ = Actions.MoveCard(Deck.Cards, Players[player].Hand, card);
} }
// Init first round // Init first round
int CardsToDraw = 1; int CardsToDraw = 1;
Card ActivePictureCard = null; Card ActivePictureCard = null;
@@ -82,10 +78,11 @@ namespace StripJackNaked
CardsToDraw = (int)Enum.Parse(typeof(PictureCard), ActivePictureCard.Value); // Cards to draw correlates to PictureCard enum value CardsToDraw = (int)Enum.Parse(typeof(PictureCard), ActivePictureCard.Value); // Cards to draw correlates to PictureCard enum value
ActivePlayer = NextPlayer(ActivePlayer); ActivePlayer = NextPlayer(ActivePlayer);
} }
else else if (ActivePictureCard is not null)
if (ActivePictureCard is not null) {
// Active player didn't play a picture card when needed to // Active player didn't play a picture card when needed to
CardsToDraw--; CardsToDraw--;
}
if (CardsToDraw < 1) if (CardsToDraw < 1)
{ {
@@ -98,17 +95,18 @@ namespace StripJackNaked
} }
if (ActivePictureCard is null) if (ActivePictureCard is null)
{
ActivePlayer = NextPlayer(ActivePlayer); ActivePlayer = NextPlayer(ActivePlayer);
}
// Show some output // Show some output
Console.WriteLine("Player 0:"); Console.Write("Player 0: ");
ShowList(Players[0].Hand); ShowList(Players[0].Hand);
Console.WriteLine("Player 1:"); Console.Write("Player 1: ");
ShowList(Players[1].Hand); ShowList(Players[1].Hand);
Console.WriteLine("Kitty:"); Console.Write("Kitty: ");
ShowList(Kitty.Pile); ShowList(Kitty.Pile);
Console.WriteLine("---------------------"); Console.WriteLine("---------------------");