Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Challenge216
- {
- class Program
- {
- static void Main(string[] args)
- {
- Game game = new Game();
- game.play();
- }
- }
- class Card
- {
- public static string[] validDescriptions = { "UNKNOWN_CARD", "Ace", "Two", "Three", "Four", "Five",
- "Six", "Seven", "Eight", "Nine", "Ten",
- "Jack", "Queen", "King"
- };
- public static string[] possibleSuits = { "Clubs", "Diamonds", "Hearts", "Spades" };
- private int value;
- private int suit;
- private String description;
- public Card(int value, int suit)
- {
- if (value < 0 || value > 13)
- {
- value = 0;
- }
- this.value = value;
- this.suit = suit;
- }
- public String getSuit()
- {
- switch (suit)
- {
- case 0:
- return "Clubs";
- case 1:
- return "Diamonds";
- case 2:
- return "Hearts";
- case 3:
- return "Spades";
- default:
- return "UNKNOWN_SUIT";
- }
- }
- public String getDescription()
- {
- description = validDescriptions[value];
- description += " of " + getSuit();
- return description;
- }
- public int getValue()
- {
- return value;
- }
- }
- class Deck
- {
- private int shuffleTimes = 6;
- private Random random = new Random();
- private List<Card> deck = new List<Card>();
- public Deck()
- {
- initializeDeck();
- }
- private void addCard(Card card)
- {
- deck.Add(card);
- }
- public Card drawCard()
- {
- if (deck.Count() > 0)
- {
- Card topCard = deck.ElementAt(0);
- deck.RemoveAt(0);
- return topCard;
- }
- else
- {
- Console.WriteLine("No more cards in the deck!");
- return null;
- }
- }
- public void initializeDeck()
- {
- deck.Clear();
- for (int i = 0; i < 4; i++)
- { /* suits */
- for (int j = 1; j <= 13; j++)
- { /* values */
- Card card;
- card = new Card(j, i);
- addCard(card);
- }
- }
- }
- public void show()
- {
- Console.WriteLine("Cards in deck:");
- for (int i = 0; i < deck.Count(); i++)
- {
- Console.WriteLine(" " + deck.ElementAt(i).getDescription());
- }
- Console.ReadLine();
- }
- public void shuffle()
- {
- for (int i = 0; i < deck.Count() * shuffleTimes; i++)
- {
- int cardIndex1 = random.Next(deck.Count());
- int cardIndex2 = random.Next(deck.Count());
- Card card1 = deck.ElementAt(cardIndex1);
- Card card2 = deck.ElementAt(cardIndex2);
- deck.RemoveAt(cardIndex1);
- deck.Insert(cardIndex1, card2);
- deck.RemoveAt(cardIndex2);
- deck.Insert(cardIndex2, card1);
- }
- }
- }
- class Game
- {
- private Deck deck = new Deck();
- private List<Card> hand = new List<Card>();
- private List<Card> flop = new List<Card>();
- private List<Card>[] computerPlayerHands;
- private void newGame()
- {
- int temp = 1;
- deck.initializeDeck();
- deck.shuffle();
- hand.Clear();
- dealToComputers();
- dealCard();
- dealCard();
- foreach (var item in computerPlayerHands)
- {
- Console.WriteLine("\nComputer Player " + temp + " cards");
- for (int i = 0; i <= 1; i++)
- {
- Console.WriteLine(item.ElementAt(i).getDescription());
- }
- temp++;
- }
- dealFlop();
- dealTurn();
- dealRiver();
- Console.ReadLine();
- }
- private void printWelcome()
- {
- String welcomeMsg = "Welcome to Texas Hold 'Em Poker \n"
- + "You will start with two cards.\n"
- + "Press enter to continue";
- Console.WriteLine(welcomeMsg);
- Console.ReadLine();
- }
- public void play()
- {
- Boolean isPlaying = true;
- printWelcome();
- while (isPlaying)
- {
- newGame();
- }
- }
- private void showHand()
- {
- if (hand.Count() > 0)
- {
- for (int i = 0; i < hand.Count(); i++)
- {
- Console.WriteLine(" " + hand.ElementAt(i).getDescription());
- }
- }
- else
- {
- Console.WriteLine("Hand is empty.");
- }
- Console.ReadLine();
- }
- private void dealCard()
- {
- Card newCard = deck.drawCard();
- if (newCard != null)
- {
- Console.WriteLine("\nYou drew the " + newCard.getDescription() + ".");
- hand.Add(newCard);
- }
- }
- private void dealFlop()
- {
- for (int i = 0; i < 3; i++)
- {
- Card newCard = deck.drawCard();
- if (newCard != null)
- {
- flop.Add(newCard);
- }
- }
- Console.WriteLine("\nFlop:");
- foreach (var card in flop)
- {
- Console.WriteLine(card.getDescription());
- }
- }
- private void dealTurn()
- {
- Card newCard = deck.drawCard();
- if (newCard != null)
- {
- flop.Add(newCard);
- }
- Console.WriteLine("\nTurn: " + newCard.getDescription());
- }
- private void dealRiver()
- {
- Card newCard = deck.drawCard();
- if (newCard != null)
- {
- flop.Add(newCard);
- }
- Console.WriteLine("\nRiver: " + newCard.getDescription());
- }
- private void dealToComputers()
- {
- int numberOfPlayers = 0;
- Console.WriteLine("How many players (2-8)?");
- try
- {
- numberOfPlayers = Convert.ToInt16(Console.ReadLine());
- }
- catch
- {
- Console.WriteLine("Error: Please enter in a number. Ex: 6");
- numberOfPlayers = Convert.ToInt16(Console.ReadLine());
- }
- computerPlayerHands = new List<Card>[numberOfPlayers];
- for (int i = 0; i < numberOfPlayers; i++)
- {
- Card newCard1 = deck.drawCard();
- Card newCard2 = deck.drawCard();
- computerPlayerHands[i] = new List<Card>();
- computerPlayerHands[i].Add(newCard1);
- computerPlayerHands[i].Add(newCard2);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment