Guest User

Untitled

a guest
May 25th, 2015
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.71 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Challenge216
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Game game = new Game();
  12.  
  13.             game.play();
  14.  
  15.         }
  16.     }
  17.  
  18.     class Card
  19.     {
  20.         public static string[] validDescriptions = { "UNKNOWN_CARD", "Ace", "Two", "Three", "Four", "Five",
  21.         "Six", "Seven", "Eight", "Nine", "Ten",
  22.         "Jack", "Queen", "King"
  23.         };
  24.  
  25.         public static string[] possibleSuits = { "Clubs", "Diamonds", "Hearts", "Spades" };
  26.  
  27.         private int value;
  28.         private int suit;
  29.         private String description;
  30.  
  31.         public Card(int value, int suit)
  32.         {
  33.             if (value < 0 || value > 13)
  34.             {
  35.                 value = 0;
  36.             }
  37.             this.value = value;
  38.             this.suit = suit;
  39.         }
  40.  
  41.         public String getSuit()
  42.         {
  43.             switch (suit)
  44.             {
  45.                 case 0:
  46.                     return "Clubs";
  47.                 case 1:
  48.                     return "Diamonds";
  49.                 case 2:
  50.                     return "Hearts";
  51.                 case 3:
  52.                     return "Spades";
  53.                 default:
  54.                     return "UNKNOWN_SUIT";
  55.             }
  56.         }
  57.  
  58.         public String getDescription()
  59.         {
  60.             description = validDescriptions[value];
  61.             description += " of " + getSuit();
  62.             return description;
  63.         }
  64.  
  65.         public int getValue()
  66.         {
  67.             return value;
  68.         }
  69.  
  70.  
  71.  
  72.     }
  73.     class Deck
  74.     {
  75.         private int shuffleTimes = 6;
  76.         private Random random = new Random();
  77.         private List<Card> deck = new List<Card>();
  78.  
  79.         public Deck()
  80.         {
  81.             initializeDeck();
  82.         }
  83.  
  84.         private void addCard(Card card)
  85.         {
  86.             deck.Add(card);
  87.         }
  88.  
  89.         public Card drawCard()
  90.         {
  91.             if (deck.Count() > 0)
  92.             {
  93.                 Card topCard = deck.ElementAt(0);
  94.                 deck.RemoveAt(0);
  95.                 return topCard;
  96.             }
  97.             else
  98.             {
  99.                 Console.WriteLine("No more cards in the deck!");
  100.                 return null;
  101.  
  102.             }
  103.  
  104.  
  105.         }
  106.  
  107.         public void initializeDeck()
  108.         {
  109.             deck.Clear();
  110.             for (int i = 0; i < 4; i++)
  111.             {     /* suits */
  112.                 for (int j = 1; j <= 13; j++)
  113.                 { /* values */
  114.                     Card card;
  115.                     card = new Card(j, i);
  116.                     addCard(card);
  117.                 }
  118.             }
  119.         }
  120.  
  121.         public void show()
  122.         {
  123.             Console.WriteLine("Cards in deck:");
  124.             for (int i = 0; i < deck.Count(); i++)
  125.             {
  126.                 Console.WriteLine("  " + deck.ElementAt(i).getDescription());
  127.             }
  128.  
  129.             Console.ReadLine();
  130.         }
  131.  
  132.         public void shuffle()
  133.         {
  134.  
  135.             for (int i = 0; i < deck.Count() * shuffleTimes; i++)
  136.             {
  137.                 int cardIndex1 = random.Next(deck.Count());
  138.                 int cardIndex2 = random.Next(deck.Count());
  139.  
  140.                 Card card1 = deck.ElementAt(cardIndex1);
  141.                 Card card2 = deck.ElementAt(cardIndex2);
  142.  
  143.                 deck.RemoveAt(cardIndex1);
  144.                 deck.Insert(cardIndex1, card2);
  145.  
  146.                 deck.RemoveAt(cardIndex2);
  147.                 deck.Insert(cardIndex2, card1);
  148.             }
  149.         }
  150.  
  151.     }
  152.  
  153.     class Game
  154.     {
  155.         private Deck deck = new Deck();
  156.         private List<Card> hand = new List<Card>();
  157.         private List<Card> flop = new List<Card>();
  158.         private List<Card>[] computerPlayerHands;
  159.        
  160.         private void newGame()
  161.         {
  162.             int temp = 1;
  163.  
  164.             deck.initializeDeck();
  165.             deck.shuffle();
  166.             hand.Clear();
  167.  
  168.  
  169.             dealToComputers();
  170.  
  171.             dealCard();
  172.             dealCard();
  173.  
  174.  
  175.  
  176.             foreach (var item in computerPlayerHands)
  177.             {
  178.                
  179.  
  180.                 Console.WriteLine("\nComputer Player " + temp + " cards");
  181.                 for (int i = 0; i <= 1; i++)
  182.                 {
  183.                    
  184.                     Console.WriteLine(item.ElementAt(i).getDescription());
  185.                 }
  186.  
  187.                 temp++;
  188.  
  189.             }
  190.  
  191.             dealFlop();
  192.             dealTurn();
  193.             dealRiver();
  194.  
  195.             Console.ReadLine();
  196.         }
  197.  
  198.  
  199.         private void printWelcome()
  200.         {
  201.             String welcomeMsg = "Welcome to Texas Hold 'Em Poker \n"
  202.               + "You will start with two cards.\n"
  203.               + "Press enter to continue";
  204.             Console.WriteLine(welcomeMsg);
  205.             Console.ReadLine();
  206.         }
  207.  
  208.  
  209.         public void play()
  210.         {
  211.             Boolean isPlaying = true;
  212.  
  213.             printWelcome();
  214.  
  215.             while (isPlaying)
  216.             {
  217.                 newGame();
  218.  
  219.  
  220.  
  221.  
  222.             }
  223.         }
  224.  
  225.         private void showHand()
  226.         {
  227.             if (hand.Count() > 0)
  228.             {
  229.  
  230.                 for (int i = 0; i < hand.Count(); i++)
  231.                 {
  232.                     Console.WriteLine("  " + hand.ElementAt(i).getDescription());
  233.                 }
  234.             }
  235.             else
  236.             {
  237.                 Console.WriteLine("Hand is empty.");
  238.             }
  239.  
  240.             Console.ReadLine();
  241.         }
  242.  
  243.  
  244.         private void dealCard()
  245.         {
  246.             Card newCard = deck.drawCard();
  247.             if (newCard != null)
  248.             {
  249.                 Console.WriteLine("\nYou drew the " + newCard.getDescription() + ".");
  250.                 hand.Add(newCard);
  251.             }
  252.         }
  253.  
  254.         private void dealFlop()
  255.         {
  256.  
  257.             for (int i = 0; i < 3; i++)
  258.             {
  259.                 Card newCard = deck.drawCard();
  260.                 if (newCard != null)
  261.                 {
  262.                     flop.Add(newCard);
  263.                 }
  264.             }
  265.  
  266.             Console.WriteLine("\nFlop:");
  267.  
  268.             foreach (var card in flop)
  269.             {
  270.                 Console.WriteLine(card.getDescription());
  271.             }
  272.  
  273.         }
  274.  
  275.         private void dealTurn()
  276.         {
  277.             Card newCard = deck.drawCard();
  278.             if (newCard != null)
  279.             {
  280.                 flop.Add(newCard);
  281.             }
  282.  
  283.             Console.WriteLine("\nTurn: " + newCard.getDescription());
  284.         }
  285.  
  286.         private void dealRiver()
  287.         {
  288.             Card newCard = deck.drawCard();
  289.             if (newCard != null)
  290.             {
  291.                 flop.Add(newCard);
  292.             }
  293.  
  294.             Console.WriteLine("\nRiver: " + newCard.getDescription());
  295.  
  296.         }
  297.  
  298.         private void dealToComputers()
  299.         {
  300.             int numberOfPlayers = 0;
  301.  
  302.  
  303.             Console.WriteLine("How many players (2-8)?");
  304.  
  305.            
  306.             try
  307.             {
  308.                 numberOfPlayers = Convert.ToInt16(Console.ReadLine());
  309.             }
  310.             catch
  311.             {
  312.                 Console.WriteLine("Error: Please enter in a number. Ex: 6");
  313.                 numberOfPlayers = Convert.ToInt16(Console.ReadLine());
  314.                
  315.             }
  316.  
  317.             computerPlayerHands = new List<Card>[numberOfPlayers];
  318.  
  319.             for (int i = 0; i < numberOfPlayers; i++)
  320.             {
  321.                 Card newCard1 = deck.drawCard();
  322.                 Card newCard2 = deck.drawCard();
  323.  
  324.                 computerPlayerHands[i] = new List<Card>();
  325.                 computerPlayerHands[i].Add(newCard1);
  326.                 computerPlayerHands[i].Add(newCard2);
  327.             }
  328.  
  329.         }
  330.     }
  331.  
  332. }
Advertisement
Add Comment
Please, Sign In to add comment