using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public enum Combination
{
None,
Pair,
TwoPair,
ThreeofaKind,
Straight,
Flush,
FullHouse,
FourofaKind,
StraightFlush,
RoyalFlush,
}
public enum Suit
{
Spades,
Hearts,
Clubs,
Diamonds,
}
public enum Number : int
{
Ace = 1,
Two = 2,
Three = 3,
Four = 4,
Five = 5,
Six = 6,
Seven = 7,
Eight = 8,
Nine = 9,
Ten = 10,
Jack = 11,
Queen = 12,
King = 13,
}
public class Card
{
public Suit suit;
public Number number;
public Card(Suit s, Number n)
{
suit = s;
number = n;
}
}
public class Player
{
Deck PlayerDeck = new Deck();
public List<Card> hand = new List<Card>();
public void GenerateHand()
{
for (int i = 0; i < 5; i++)
{
Card c = PlayerDeck.RandomCard();
hand.Add(c);
PlayerDeck.deck.Remove(c);
}
}
public Combination CardCombo(Combination cardcombo)
{
cardcombo = Combination.None;
return cardcombo;
}
public Card HighCard() // If combination = none, highest card is calculated
{
return null;
}
}
class Deck
{
private Random rand = new Random();
public List<Card> deck = new List<Card>();
public Deck()
{
Card AofS = new Card(Suit.Spades, Number.Ace);
deck.Add(AofS);
Card TwoofS = new Card(Suit.Spades, Number.Two);
deck.Add(TwoofS);
Card ThreeofS = new Card(Suit.Spades, Number.Three);
deck.Add(ThreeofS);
Card FourofS = new Card(Suit.Spades, Number.Four);
deck.Add(FourofS);
Card FiveofS = new Card(Suit.Spades, Number.Five);
deck.Add(FiveofS);
Card SixofS = new Card(Suit.Spades, Number.Six);
deck.Add(SixofS);
Card SevenofS = new Card(Suit.Spades, Number.Seven);
deck.Add(SevenofS);
Card EightofS = new Card(Suit.Spades, Number.Eight);
deck.Add(EightofS);
Card NineofS = new Card(Suit.Spades, Number.Nine);
deck.Add(NineofS);
Card TenofS = new Card(Suit.Spades, Number.Ten);
deck.Add(TenofS);
Card JackofS = new Card(Suit.Spades, Number.Jack);
deck.Add(JackofS);
Card QueenofS = new Card(Suit.Spades, Number.Queen);
deck.Add(QueenofS);
Card KingofS = new Card(Suit.Spades, Number.King);
deck.Add(KingofS);
Card AofH = new Card(Suit.Hearts, Number.Ace);
deck.Add(AofH);
Card TwoofH = new Card(Suit.Hearts, Number.Two);
deck.Add(TwoofH);
Card ThreeofH = new Card(Suit.Hearts, Number.Three);
deck.Add(ThreeofH);
Card FourofH = new Card(Suit.Hearts, Number.Four);
deck.Add(FourofH);
Card FiveofH = new Card(Suit.Hearts, Number.Five);
deck.Add(FiveofH);
Card SixofH = new Card(Suit.Hearts, Number.Six);
deck.Add(SixofH);
Card SevenofH = new Card(Suit.Hearts, Number.Seven);
deck.Add(SevenofH);
Card EightofH = new Card(Suit.Hearts, Number.Eight);
deck.Add(EightofH);
Card NineofH = new Card(Suit.Hearts, Number.Nine);
deck.Add(NineofH);
Card TenofH = new Card(Suit.Hearts, Number.Ten);
deck.Add(TenofH);
Card JackofH = new Card(Suit.Hearts, Number.Jack);
deck.Add(JackofH);
Card QueenofH = new Card(Suit.Hearts, Number.Queen);
deck.Add(QueenofH);
Card KingofH = new Card(Suit.Hearts, Number.King);
deck.Add(KingofH);
Card AofC = new Card(Suit.Clubs, Number.Ace);
deck.Add(AofC);
Card TwoofC = new Card(Suit.Clubs, Number.Two);
deck.Add(TwoofC);
Card ThreeofC = new Card(Suit.Clubs, Number.Three);
deck.Add(ThreeofC);
Card FourofC = new Card(Suit.Clubs, Number.Four);
deck.Add(FourofC);
Card FiveofC = new Card(Suit.Clubs, Number.Five);
deck.Add(FiveofC);
Card SixofC = new Card(Suit.Clubs, Number.Six);
deck.Add(SixofC);
Card SevenofC = new Card(Suit.Clubs, Number.Seven);
deck.Add(SevenofC);
Card EightofC = new Card(Suit.Clubs, Number.Eight);
deck.Add(EightofC);
Card NineofC = new Card(Suit.Clubs, Number.Nine);
deck.Add(NineofC);
Card TenofC = new Card(Suit.Clubs, Number.Ten);
deck.Add(TenofC);
Card JackofC = new Card(Suit.Clubs, Number.Jack);
deck.Add(JackofC);
Card QueenofC = new Card(Suit.Clubs, Number.Queen);
deck.Add(QueenofC);
Card KingofC = new Card(Suit.Clubs, Number.King);
deck.Add(KingofC);
Card AofD = new Card(Suit.Diamonds, Number.Ace);
deck.Add(AofD);
Card TwoofD = new Card(Suit.Diamonds, Number.Two);
deck.Add(TwoofD);
Card ThreeofD = new Card(Suit.Diamonds, Number.Three);
deck.Add(ThreeofD);
Card FourofD = new Card(Suit.Diamonds, Number.Four);
deck.Add(FourofD);
Card FiveofD = new Card(Suit.Diamonds, Number.Five);
deck.Add(FiveofD);
Card SixofD = new Card(Suit.Diamonds, Number.Six);
deck.Add(SixofD);
Card SevenofD = new Card(Suit.Diamonds, Number.Seven);
deck.Add(SevenofD);
Card EightofD = new Card(Suit.Diamonds, Number.Eight);
deck.Add(EightofD);
Card NineofD = new Card(Suit.Diamonds, Number.Nine);
deck.Add(NineofD);
Card TenofD = new Card(Suit.Diamonds, Number.Ten);
deck.Add(TenofD);
Card JackofD = new Card(Suit.Diamonds, Number.Jack);
deck.Add(JackofD);
Card QueenofD = new Card(Suit.Diamonds, Number.Queen);
deck.Add(QueenofD);
Card KingofD = new Card(Suit.Diamonds, Number.King);
deck.Add(KingofD);
}
private void Shuffle()
{
Random rand = new Random();
int y = 0;
foreach (Card c in deck)
{
y++;
}
for (int i = 0; i < 100; i++)
{
int x = rand.Next(0, y);
Card c = deck[x];
deck.Remove(c);
deck.Add(c);
}
}
public Card RandomCard()
{
Shuffle();
Random rand = new Random();
int y = 0;
foreach (Card c in deck)
{
y++;
}
int x = rand.Next(0, y);
return deck[x];
}
public Player DetermineWinner(Player One, Player Two, Player Three, Player Four)
{
return null;
}
}
class Program
{
static void Main(string[] args)
{
Console.Title = "Poker";
Player PlayerOne = new Player();
Player PlayerTwo = new Player();
Player PlayerThree = new Player();
Player PlayerFour = new Player();
PlayerOne.GenerateHand();
PlayerTwo.GenerateHand();
PlayerThree.GenerateHand();
PlayerFour.GenerateHand();
bool quit = false;
Console.WriteLine("Welcome to Poker!");
Console.WriteLine("Want to play?");
string userinput = Console.ReadLine();
if (userinput == "no" || userinput == "n") quit = true;
while (!quit)
{
Console.WriteLine("Your cards are:\n");
for (int a = 0; a < 5; a++)
{
Console.Write(PlayerOne.hand[a].number.ToString());
Console.Write(" of ");
Console.Write(PlayerTwo.hand[a].suit.ToString());
Console.Write("\n");
}
for (int b = 0; b < 5; b++)
{
Console.Write(PlayerTwo.hand.number.ToString());
Console.Write(" of ");
Console.Write(PlayerTwo.hand.suit.ToString());
Console.Write("\n");
}
for (int c = 0; c < 5; c++)
{
Console.Write(PlayerThree.hand[c].number.ToString());
Console.Write(" of ");
Console.Write(PlayerThree.hand[c].suit.ToString());
Console.Write("\n");
}
for (int d = 0; d < 5; d++)
{
Console.Write(PlayerFour.hand[d].number.ToString());
Console.Write(" of ");
Console.Write(PlayerFour.hand[d].suit.ToString());
Console.Write("\n");
}
quit = true;
}
}
}
}
C# poker game problem
Hello all. I've recently taken an interest in programming, and I heard that Visual C# was a good language to start with. So I got a book and read some tutorials, and spent about 4 months learning the basics of the language. I decided to try and program a poker game of sorts, to see if I could use what I learned to make a simple game. I thought it was going pretty well, and then I decided to test to see if the players all had different hands. But every time, each player always has the same hand. I've spent a week and a half trying to figure out what I was doing wrong, and I played around with different things, such as giving each player their own unique list and hand-generating method in the Player class. But no matter what I do, the hands are always the same. Eventually I got so frustrated that I decided to post it here and see if anybody could help. Thanks for any help, and I apologize in advance if the solution to this is common knowledge :x
Actually it wasn't intended; I didn't really think about how each player would end up having their own deck when I did it. I just took out the deck from the Player class and declared it in Main, and all the players have different cards now when I run it. Thanks a lot! However, would it be better if I made the Deck class static? Or is simply declaring an instance of it in Main fine?
In my Blackjack game I simply initialized one deck object when the game starts and I used that same deck object for all players. So it seems to be working now?
Making the deck static could work, however I see no reason why all the players would share a deck. Make a dealer class, or use your deck as a dealer class, and use that for deck management.
I would also recommend a base class of CardCollection. From that you could subclass Hand and Deck.
And, for populating the deck this could save you some lines of code:
And, for populating the deck this could save you some lines of code:
public enum Suit { Spades, Hearts, Clubs, Diamonds, } public enum Number { Ace = 1, Two = 2, Three = 3, Four = 4, Five = 5, Six = 6, Seven = 7, Eight = 8, Nine = 9, Ten = 10, Jack = 11, Queen = 12, King = 13, } public class Card { private Suit suit; private Number number; public Card(Suit suit, Number number) { this.suit = suit; this.number = number; } } public abstract class CardCollection { protected List<Card> cards = new List<Card>(); public void AddCard(Card card) { this.cards.Add(card); } public void RemoveCard(Card card) { if (this.cards.Contains(card) == true) { this.cards.Remove(card); } else { throw new InvalidOperationException("The card is not in the deck"); } } } public class Hand : CardCollection { } public class Deck : CardCollection { public Deck() { foreach (Suit currentSuit in Enum.GetValues(typeof(Suit))) { foreach (Number currentNumber in Enum.GetValues(typeof(Number))) { AddCard(new Card(currentSuit, currentNumber)); } } } }
Yeah, having only one deck object fixed it. I didn't actually want a deck for each player, it was something that I put in without thinking. And I'm going to try to work that code for adding the cards to the deck into mine, because it makes a lot more sense to me to do it that way. Thanks for all the help.
I'm not sure if the same is true in c# as c++ but the rand function will always produce the same results as it's not actually random this can be impoved on by using a seeded random function this takes a parameter that it uses in a attempt to randomize the number generated a bit more.
Quote:Original post by ramearess
I'm not sure if the same is true in c# as c++ but the rand function will always produce the same results as it's not actually random this can be impoved on by using a seeded random function this takes a parameter that it uses in a attempt to randomize the number generated a bit more.
this!
and also try to seed the rand after initialisation and other stuff, as it will always be a different time into the program, as opposed to doing it at the start where it will always be the same
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement