Advertisement
Daemonion

Assignment 3, Coursera BGP C#

Oct 4th, 2013
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.56 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using ConsoleCards;
  6.  
  7. namespace ProgrammingAssignment3
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             // HELLO, EVALUTATOR!
  14.             // I added a few things to the code that were not part of the assignment
  15.             // in order to challenge myself. I hope that is ok, and I tried to
  16.             // use proper commenting to make it easier to read.
  17.             //
  18.             // You can also see a PasteBin version of this code (easier to read) at:
  19.             // http://pastebin.com/n8bAcmei
  20.             //
  21.             // - Desmond                      
  22.            
  23.             // Print a “welcome” message to the user telling them that the program will play a single hand of Blackjack
  24.             // I added a ReadLine to collect the player's name
  25.             Console.WriteLine("**********************************\n");
  26.             Console.WriteLine("WELCOME!\n\nLet's play a round of blackjack.\nYour hard-earned money is forfeit!\n");
  27.             Console.WriteLine("**********************************\n");
  28.             Console.WriteLine("Type your name and press ENTER to deal...");
  29.             string confirmation = Console.ReadLine();
  30.  
  31.             // declare variables for and create a deck of cards and blackjack hands for the dealer and player
  32.             // name one BlackjackHand after the player's input
  33.             Deck bicycleRed = new Deck();
  34.             BlackjackHand playerHand = new BlackjackHand(confirmation);
  35.             BlackjackHand dealerHand = new BlackjackHand("Dealer");
  36.             Console.WriteLine();
  37.  
  38.             // Shuffle the deck of cards
  39.             bicycleRed.Shuffle();
  40.  
  41.             // Deal 2 cards to the player and dealer
  42.             // Remove the top card, store it into a new card variable, and alternate which deck they go into
  43.             Card card0 = bicycleRed.TakeTopCard();
  44.             dealerHand.AddCard(card0);
  45.            
  46.             Card card1 = bicycleRed.TakeTopCard();
  47.             playerHand.AddCard(card1);
  48.  
  49.             Card card2 = bicycleRed.TakeTopCard();
  50.             dealerHand.AddCard(card2);
  51.  
  52.             Card card3 = bicycleRed.TakeTopCard();
  53.             playerHand.AddCard(card3);
  54.  
  55.             // Make all the player’s cards face up
  56.             playerHand.ShowAllCards();
  57.  
  58.             // Make the dealer’s first card face up
  59.             dealerHand.ShowFirstCard();
  60.  
  61.             // Print both the player’s hand and the dealer’s hand
  62.             // I also printed the scores (hid the dealer's score) to make it easier to decide if to hit or not
  63.             playerHand.Print();
  64.             Console.WriteLine(confirmation + "'s current score: " + playerHand.Score + "\n");
  65.             dealerHand.Print();
  66.             Console.WriteLine("Dealer's score: ?\n");
  67.            
  68.             // Let the player hit if they want to
  69.             playerHand.HitOrNot(bicycleRed);
  70.  
  71.             // Make all the dealer’s cards face up
  72.             dealerHand.ShowAllCards();
  73.  
  74.             // Print both the player’s hand and the dealer’s hand
  75.             // I added some formatting here
  76.             playerHand.Print();
  77.             Console.WriteLine(confirmation + "'s final score: " + playerHand.Score + "\n\n");
  78.             dealerHand.Print();
  79.             Console.WriteLine("Dealer's final score: " + dealerHand.Score + "\n\n______________\n");
  80.  
  81.             // Print the scores for both hands
  82.             // I also added conditions so that, based on the score, the appropriate message is printed
  83.             // If blackjack is obtained, the player gets a special message with the unicode character for spade
  84.             if (playerHand.Score > 21)
  85.             {
  86.                 Console.WriteLine(confirmation + " LOSES!\n");
  87.             }
  88.            
  89.             else if (playerHand.Score == 21 && dealerHand.Score != 21)
  90.             {
  91.                 Console.WriteLine("\u2660\u2660\u2660f " + "BLACKJACK, you win!" + " \u2660\u2660\u2660\n");
  92.             }
  93.            
  94.             else if (playerHand.Score > dealerHand.Score && playerHand.Score <= 21)
  95.             {
  96.                 Console.WriteLine(confirmation + " WINS!\n");
  97.             }
  98.            
  99.             else if (playerHand.Score < dealerHand.Score && dealerHand.Score <= 21)
  100.             {
  101.                 Console.WriteLine(confirmation + " LOSES!\n");
  102.             }
  103.            
  104.             else
  105.             {
  106.                 Console.WriteLine("Wow ... you both tied!\n");
  107.             }
  108.         }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement