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
{
Spades,
Clubs,
Diamonds,
Hearts
}
public enum Suit { Spades, Clubs, Diamonds, Hearts }
public enum PictureCard
{
Jack = 1,
Queen,
King,
Ace
}
public enum PictureCard { Jack = 1, Queen, King, Ace }
public class Card
{
public string Value;
public Suit Suit;
public string NamedValue
{
get => Value + " of " + Suit;
}
public Boolean IsPicture
{
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 string NamedValue => Value + " of " + Suit;
public bool IsPicture => Enum.IsDefined(typeof(PictureCard), Value);
public bool IsNumber => !Enum.IsDefined(typeof(PictureCard), Value);
public bool IsBlack => (Enum.GetName(Suit) == "Spades" || Enum.GetName(Suit) == "Clubs");
public bool IsRed => (Enum.GetName(Suit) == "Diamonds" || Enum.GetName(Suit) == "Hearts");
}
public class Deck
@@ -68,8 +43,8 @@
}
}
}
private static Random rng = new();
private static Random rng = new();
public static void Shuffle()
{
Cards = Cards.OrderBy(a => rng.Next()).ToList();
@@ -78,7 +53,7 @@
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)
{
@@ -86,7 +61,9 @@
}
if (card is null)
card = From.FirstOrDefault();
{
card = From.FirstOrDefault(); // FIFO
}
if (From.Contains(card) && !To.Contains(card))
{
@@ -94,7 +71,7 @@
From.Remove(card);
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
{
static class Constants
internal static class Constants
{
public const int PlayerCount = 2;
}
@@ -19,38 +19,35 @@ namespace StripJackNaked
class Program
{
public static int NextPlayer(int CurrentPlayer)
{
return ++CurrentPlayer % Constants.PlayerCount;
}
public static int NextPlayer(int CurrentPlayer) { return ++CurrentPlayer % Constants.PlayerCount; }
public static Boolean PlayCard(List<Card> From)
{
return Actions.MoveCard(From, Kitty.Pile);
}
public static bool PlayCard(List<Card> From) { return Actions.MoveCard(From, Kitty.Pile); }
public static void TakePile(List<Card> Winner)
{
while (Kitty.Pile.Count() > 0)
{
Actions.MoveCard(Kitty.Pile, Winner);
_ = Actions.MoveCard(Kitty.Pile, Winner);
}
}
public static void ShowList(List<Card> Cards)
{
foreach (Card card in Cards)
{
Console.Write(card.NamedValue + ((card == Cards.LastOrDefault()) ? "" : ", "));
}
Console.WriteLine();
}
static void Main()
private static void Main()
{
// Init players
List<Player> Players = new();
for (int i = 0; i < Constants.PlayerCount; i++)
{
Players.Add(new Player());
}
// Init deck
Deck.Enumerate();
@@ -61,10 +58,9 @@ namespace StripJackNaked
{
Card card = Deck.Cards.Last();
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
int CardsToDraw = 1;
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
ActivePlayer = NextPlayer(ActivePlayer);
}
else
if (ActivePictureCard is not null)
else if (ActivePictureCard is not null)
{
// Active player didn't play a picture card when needed to
CardsToDraw--;
}
if (CardsToDraw < 1)
{
@@ -98,17 +95,18 @@ namespace StripJackNaked
}
if (ActivePictureCard is null)
{
ActivePlayer = NextPlayer(ActivePlayer);
}
// Show some output
Console.WriteLine("Player 0:");
Console.Write("Player 0: ");
ShowList(Players[0].Hand);
Console.WriteLine("Player 1:");
Console.Write("Player 1: ");
ShowList(Players[1].Hand);
Console.WriteLine("Kitty:");
Console.Write("Kitty: ");
ShowList(Kitty.Pile);
Console.WriteLine("---------------------");