well, I know it looks I have nothing to do, but I start new home
project. This time I try to make own poker card game.
I would like to show you my recent code of the library used by the
future game...
using System;
using System.Collections.Generic;
using System.Text;
namespace PokerCore
{
public class Hand
{
public Hand(Deck deck, GameTypes gameType)
{
this.deck = deck;
this.gameType = gameType;
this.cards = new List<Card>(5);
}
public void ChangeGameType(GameTypes gameType)
{
this.gameType = gameType;
}
public Card[] showCards(bool showAll)
{
if (showAll == true)
{
foreach (Card c in this.cards)
{
c.ShowCard(true);
}
}
return this.cards.ToArray();
}
public void PickNewCard(bool showCard)
{
this.cards.Add(this.deck.pickCard(showCard));
}
public bool CanChangeCards
{
get
{
if (this.gameType == GameTypes.Draw)
return true;
else
return false;
}
}
public void ChangeCards(params int[] indexes)
{
if (this.CanChangeCards && indexes.Length < 5)
{
foreach (int i in indexes)
{
this.cards.RemoveAt(i);
this.cards.Add(this.deck.pickCard(false));
}
}
else
throw (new Exception("Can change just 1 - 4 cards"));
}
Deck deck;
GameTypes gameType;
List<Card> cards;
}
public class Deck
{
public Deck()
{
this.cards = new List<Card>(52);
this.Shuffle();
this.r = new Random();
}
public void Shuffle()
{
this.cards.Clear();
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 13; j++)
{
this.cards.Add(new Card((Ranks)j, (Suits)i,
false));
}
}
}
public Card pickCard(bool showCard)
{
int selectCard = r.Next(0, this.cards.Count - 1);
Card pickedCard = this.cards[selectCard];
this.cards.RemoveAt(selectCard);
pickedCard.ShowCard(showCard);
return pickedCard;
}
List<Card> cards;
Random r;
}
public class Card
{
public Card(Ranks rank, Suits suit, bool revealed)
{
this.suit = suit;
this.rank = rank;
this.revealed = revealed;
}
public void ShowCard(bool show)
{
this.revealed = show;
}
public string Rank
{
get
{
if (this.revealed == true)
return this.rank.ToString();
else
return "HIDDEN";
}
}
public string Suit
{
get
{
if (this.revealed == true)
return this.suit.ToString();
else
return "HIDDEN";
}
}
public string CardName
{
get
{
if (this.revealed == true)
return string.Format("{0} of {1}",
this.rank.ToString(), this.suit.ToString());
else
return "HIDDEN";
}
}
Suits suit;
Ranks rank;
bool revealed;
}
public enum Ranks
{
Two,
Three,
Four,
Five,
Six,
Seven,
Eight,
Nine,
Ten,
Jack,
Queen,
King,
Ace
}
public enum Suits
{
Spades, Hearts, Diamonds, Clubs
}
public enum GameTypes
{
Stud, Draw
}
}