Author Topic: For you guys: Blackjack in Console!  (Read 1090 times)

0 Members and 1 Guest are viewing this topic.

Offline S3THST4

  • Contributor
  • Jr. Member
  • *
  • Posts: 81
  • LOL, Women's Rights!
  • Field: Software Development
  • Real Name: Seth Jacoby
  • Favorite OS: Linux
  • Programming Language: C#
For you guys: Blackjack in Console!
« on: October 19, 2009, 03:52:04 AM »
Today, I got bored. I decided to try and write a blackjack game in Console. I've decided to share the code with you guys. As far as I know, it works great. Tomorrow, I'll write a tutorial on the whole thing and submit it.

There is no splitting, and ace = 11. The dealer is just a random number between 15 and 21. It's not very complex.

Source code is licensed under the Eclipse Public License v1.0.

Code: C# [Select]
  1. using System;
  2.  
  3. namespace Blackjack
  4. {
  5.     class Blackjack
  6.     {
  7.         static string[] playerCards = new string[11];
  8.         static string hitOrStay = "";
  9.         static int total = 0, count = 1, dealerTotal = 0;
  10.         static Random cardRandomizer = new Random();
  11.  
  12.         static void Main(string[] args)
  13.         {
  14.             Console.Title = "Seth's Blackjack!";
  15.             Start();
  16.         }
  17.  
  18.         static void Start()
  19.         {
  20.             dealerTotal = cardRandomizer.Next(15, 22);
  21.             playerCards[0] = Deal();
  22.             playerCards[1] = Deal();
  23.             do
  24.             {
  25.                 Console.WriteLine("Welcome to Blackjack! You were dealed " + playerCards[0] + " and " + playerCards[1] + ". \nYour total is " + total + ".\nWould you like to hit or stay?");
  26.                 hitOrStay = Console.ReadLine().ToLower();
  27.             } while (!hitOrStay.Equals("hit") && !hitOrStay.Equals("stay"));
  28.             Game();
  29.         }
  30.  
  31.         static void Game()
  32.         {
  33.             if (hitOrStay.Equals("hit"))
  34.             {
  35.                 Hit();
  36.             }
  37.             else if (hitOrStay.Equals("stay"))
  38.             {
  39.                 if (total > dealerTotal && total <= 21)
  40.                 {
  41.                     Console.WriteLine("\nCongrats! You won the game! The dealer's total was " + dealerTotal + ".\nWould you like to play again? y/n");
  42.                     PlayAgain();
  43.                 }
  44.                 else if (total < dealerTotal)
  45.                 {
  46.                     Console.WriteLine("\nSorry, you lost! The dealer's total was " + dealerTotal + ".\nWould you like to play again? y/n");
  47.                     PlayAgain();
  48.                 }
  49.  
  50.             }
  51.             Console.ReadLine();
  52.         }
  53.  
  54.         static string Deal()
  55.         {
  56.             string Card = "";
  57.             int cards = cardRandomizer.Next(1, 14);
  58.             switch (cards)
  59.             {
  60.                 case 1: Card = "Two"; total += 2;
  61.                     break;
  62.                 case 2: Card = "Three"; total += 3;
  63.                     break;
  64.                 case 3: Card = "Four"; total += 4;
  65.                     break;
  66.                 case 4: Card = "Five"; total += 5;
  67.                     break;
  68.                 case 5: Card = "Six"; total += 6;
  69.                     break;
  70.                 case 6: Card = "Seven"; total += 7;
  71.                     break;
  72.                 case 7: Card = "Eight"; total += 8;
  73.                     break;
  74.                 case 8: Card = "Nine"; total += 9;
  75.                     break;
  76.                 case 9: Card = "Ten"; total += 10;
  77.                     break;
  78.                 case 10: Card = "Jack"; total += 10;
  79.                     break;
  80.                 case 11: Card = "Queen"; total += 10;
  81.                     break;
  82.                 case 12: Card = "King"; total += 10;
  83.                     break;
  84.                 case 13: Card = "Ace"; total += 11;
  85.                     break;
  86.                 default: Card = "2"; total += 2;
  87.                     break;
  88.             }
  89.             return Card;
  90.         }
  91.  
  92.         static void Hit()
  93.         {
  94.             count += 1;
  95.             playerCards[count] = Deal();
  96.             Console.WriteLine("\nYou were dealed a(n) " + playerCards[count] + ".\nYour new total is " + total + ".");
  97.             if (total.Equals(21))
  98.             {
  99.                 Console.WriteLine("\nYou got Blackjack! The dealer's total was " + dealerTotal + ".\nWould you like to play again?");
  100.                 PlayAgain();
  101.             }
  102.             else if (total > 21)
  103.             {
  104.                 Console.WriteLine("\nYou busted, therefore you lost. Sorry. The dealer's total was " + dealerTotal + ".\nWould you like to play again? y/n");
  105.                 PlayAgain();
  106.             }
  107.             else if (total < 21)
  108.             {
  109.                 do
  110.                 {
  111.                     Console.WriteLine("\nWould you like to hit or stay?");
  112.                     hitOrStay = Console.ReadLine().ToLower();
  113.                 } while (!hitOrStay.Equals("hit") && !hitOrStay.Equals("stay"));
  114.                 Game();
  115.             }
  116.         }
  117.  
  118.         static void PlayAgain()
  119.         {
  120.             string playAgain = "";
  121.             do
  122.             {
  123.                 playAgain = Console.ReadLine().ToLower();
  124.             } while (!playAgain.Equals("y") && !playAgain.Equals("n"));
  125.             if (playAgain.Equals("y"))
  126.             {
  127.                 Console.WriteLine("\nPress enter to restart the game!");
  128.                 Console.ReadLine();
  129.                 Console.Clear();
  130.                 dealerTotal = 0;
  131.                 count = 1;
  132.                 total = 0;
  133.                 Start();
  134.             }
  135.             else if (playAgain.Equals("n"))
  136.             {
  137.                 Console.WriteLine("\nPress enter to close Blackjack.");
  138.                 Console.ReadLine();
  139.                 Environment.Exit(0);
  140.             }
  141.        
  142.         }
  143.     }
  144. }
  145.  

Enjoy!
« Last Edit: October 20, 2009, 03:02:22 AM by S3THST4 »
Eric Clapton is God.


Offline S3THST4

  • Contributor
  • Jr. Member
  • *
  • Posts: 81
  • LOL, Women's Rights!
  • Field: Software Development
  • Real Name: Seth Jacoby
  • Favorite OS: Linux
  • Programming Language: C#
Re: For you guys: Blackjack in Console!
« Reply #1 on: October 19, 2009, 10:39:20 PM »
Edited the code a little to make it less repetitive (the play again is now in a seperate method.

(PS. AJ, if there's a better place for this, please move it there)
Eric Clapton is God.


Offline gbertoli3

  • Expert
  • Sr. Member
  • *
  • Posts: 1,169
  • My Code Is In The Cube
    • Bertoli Family
  • Field: Software & Web Development
  • Real Name: Guido Bertoli
  • Favorite OS: Windows
  • Programming Language: C#
Re: For you guys: Blackjack in Console!
« Reply #2 on: October 20, 2009, 01:13:01 AM »
Sorry Seth, but there is a minor error in your code. This line:
Code: C# [Select]
  1. static string[] playerCards = new string[5];

Lets imagine a user is delt the number 2, then they hit 3 times more; they get the number 2 all three times(highly unlikely, but still possible). Now their total is 8, so they hit again. This is now the 5th time they have hit, say they get a 3. Now they have 11, but what if they want to hit again? They can't because you said they can only have 4 hits(5th hit is an IndexOutOfBounds Exception). So you might want to change your code to:
Code: C# [Select]
  1. static string[] playerCards = new string[11];

You need it to be 11 because, you need to allow the user to take 10 hits; if the code was = new string[10] it would throw an exception on the 10th hit, but it should be on the 11th hit; because it is not possible to have 11 hits. Here are the possible lowest hits: 2,2,2,2,3,3,3,3,4,4

Also when writing your tutorial you might want to include my explanation, that way the person reading the tutorial will know exactly what that is for.

Hope this helps.
« Last Edit: October 20, 2009, 01:14:54 AM by gbertoli3 »

Offline S3THST4

  • Contributor
  • Jr. Member
  • *
  • Posts: 81
  • LOL, Women's Rights!
  • Field: Software Development
  • Real Name: Seth Jacoby
  • Favorite OS: Linux
  • Programming Language: C#
Re: For you guys: Blackjack in Console!
« Reply #3 on: October 20, 2009, 03:01:29 AM »
I realized that I put the max index at 5, but I didn't think about it that deep. Thanks, man. I think when I first did it, I was just getting it functioning, then forgot about it. :P
Eric Clapton is God.


Offline gbertoli3

  • Expert
  • Sr. Member
  • *
  • Posts: 1,169
  • My Code Is In The Cube
    • Bertoli Family
  • Field: Software & Web Development
  • Real Name: Guido Bertoli
  • Favorite OS: Windows
  • Programming Language: C#
Re: For you guys: Blackjack in Console!
« Reply #4 on: October 20, 2009, 08:28:38 PM »
I like to start out making my code function as close to 100% as possible when I start, that way I don't forget about those little error(s).

Also, when it asks the user if they would like to start a new game and they say, yes; then it asks them again. It says press enter to restart the game. It is a little redundant, but if that is what you intended then it is fine. Otherwise everything else seems to be coded error free(that I can see). Good Job :^:

Offline S3THST4

  • Contributor
  • Jr. Member
  • *
  • Posts: 81
  • LOL, Women's Rights!
  • Field: Software Development
  • Real Name: Seth Jacoby
  • Favorite OS: Linux
  • Programming Language: C#
Re: For you guys: Blackjack in Console!
« Reply #5 on: October 20, 2009, 11:29:32 PM »
Yeah, it is a little stupid, but I meant for it.

I should probably start doing that; do the little things as well so I don't forget :P
Eric Clapton is God.


Offline gbertoli3

  • Expert
  • Sr. Member
  • *
  • Posts: 1,169
  • My Code Is In The Cube
    • Bertoli Family
  • Field: Software & Web Development
  • Real Name: Guido Bertoli
  • Favorite OS: Windows
  • Programming Language: C#
Re: For you guys: Blackjack in Console!
« Reply #6 on: October 21, 2009, 05:27:58 AM »
Yes, a good programmer tests then codes. By that I mean, they logically test(see if it will do what it should), then they code it so it does what they want it too.