ALL CODE IN THIS TUTORIAL IS LICENSED UNDER THE EPL (ECLIPSE PUBLIC LICENSE) 1.0!
If you don't wanna read the tutorial, just skip to the bottom for the whole code.
Okay, so first, open up a new project, click Console Application, and name it Blackjack.
There is a total of five methods we'll create, and six globals.
Now, let's declare our globals. Above static void Main, copy and paste the following.
static string[] playerCards = new string[12]; // create a new string array named playerCards, which we will store our cards in. The maximum index is 12 (so the most you can hold is 11 cards.) static string hitOrStay = ""; // create a string variable named hitOrStay to ask to hit or stay. static int total = 0, count = 1, dealerTotal = 0; // create three integer variables named total, which is for our total, count, which is our variable to get an index in our array (we increase to hold multiple cards), and dealerTotal, which is, obviously, the dealer's total. static Random cardRandomizer = new Random(); // create a new instance of Random called cardRandomizer so we can randomize our cards and the dealer's total.
Alright, the code is commented, so I don't really need to explain.
Let's start with the method that will deal out a card, named Deal().
static string Deal()
{
string Card = "";
int cards = cardRandomizer.Next(1, 14);
switch (cards)
{
case 1: Card = "Two"; total += 2;
break;
case 2: Card = "Three"; total += 3;
break;
case 3: Card = "Four"; total += 4;
break;
case 4: Card = "Five"; total += 5;
break;
case 5: Card = "Six"; total += 6;
break;
case 6: Card = "Seven"; total += 7;
break;
case 7: Card = "Eight"; total += 8;
break;
case 8: Card = "Nine"; total += 9;
break;
case 9: Card = "Ten"; total += 10;
break;
case 10: Card = "Jack"; total += 10;
break;
case 11: Card = "Queen"; total += 10;
break;
case 12: Card = "King"; total += 10;
break;
case 13: Card = "Ace"; total += 11;
break;
default: Card = "2"; total += 2;
break;
}
return Card;
}
This method is fairly simple. First, we make a string variable named Card to return; we will later assign this to playerCards[]. Then, we create a random number between 1 and 14. Then, we switch that integer, and depending on its result, assign a value to Card, and add a respective amount to 'total'. Afterwards, we return the string Card. Simple, huh?
Next, let's code the method to start the game. Its name is Start().
static void Start()
{
dealerTotal = cardRandomizer.Next(15, 22); // A random number between 15 and 21 for the dealer's number
playerCards[0] = Deal(); // assign the index 0 (our first index) of playerCards[] to the returned value of Deal(); this is our first card
playerCards[1] = Deal(); // assign the index 1 (our second index) of playerCards[] to the returned value of Deal(); this is our second card
do
{
Console.WriteLine("Welcome to Blackjack! You were dealed " + playerCards[0] + " and " + playerCards[1] + ". \nYour total is " + total + ".\nWould you like to hit or stay?");
hitOrStay = Console.ReadLine().ToLower();
} while (!hitOrStay.Equals("hit") && !hitOrStay.Equals("stay")); // tell the user what numbers they were dealed, and then ask them to hit or stay. Do this until they enter a correct value.
Game(); // Call the game Method
}
I heavily commented it, but, in a nutshell, it gives the dealer a total, gives us two cards, and asks us to hit or stay.
Now, let's work on the Game() method, which is where the game takes place!
static void Game()
{
if (hitOrStay.Equals("hit")) // we asked the user to hit or stay, and if they chose to hit..
{
Hit(); // call the method hit
}
else if (hitOrStay.Equals("stay")) // if they chose to stay..
{
if (total > dealerTotal && total <= 21) // if our total is greater than the dealer's total and less than 21..
{
Console.WriteLine("\nCongrats! You won the game! The dealer's total was " + dealerTotal + ".\nWould you like to play again? y/n"); // tell the user the won, tell them the dealer's total, and ask them to play again
PlayAgain(); // Call the method 'PlayAgain()'.
}
else if (total < dealerTotal) // if our total is less than the dealer's..
{
Console.WriteLine("\nSorry, you lost! The dealer's total was " + dealerTotal + ".\nWould you like to play again? y/n"); // tell the user they lost, tell them the dealer's total, and ask them to play again
PlayAgain(); // Call the method 'PlayAgain()'
}
}
}
Again, it is quite heavily commented, but, all we're doing is seeing if they chose to hit or stay, and if they stayed, see if they won and ask if they want to play again.
And now, the method we've all been waiting for: Hit()
static void Hit()
{
count += 1; // add one to count (count is where we get the number to index playerCards)
playerCards[count] = Deal(); // Deal a card to our next avaliable index in playerCards
Console.WriteLine("\nYou were dealed a(n) " + playerCards[count] + ".\nYour new total is " + total + "."); // tell the user what they were dealed
if (total.Equals(21)) // if their total is 21.. (Blackjack)
{
Console.WriteLine("\nYou got Blackjack! The dealer's total was " + dealerTotal + ".\nWould you like to play again?"); // Tell the user they got blackjack, tell them the dealer's total, and ask them to play again
PlayAgain(); // Call the method PlayAgain()
}
else if (total > 21) // if their total is greater than 21.. (bust)
{
Console.WriteLine("\nYou busted, therefore you lost. Sorry. The dealer's total was " + dealerTotal + ".\nWould you like to play again? y/n"); // Tell the user they busted, tell them the dealer's total, and ask them to play again
PlayAgain(); // Call the method PlayAgain()
}
else // otherwise.. (less than 21)
{
do
{
Console.WriteLine("\nWould you like to hit or stay?");
hitOrStay = Console.ReadLine().ToLower();
} while (!hitOrStay.Equals("hit") && !hitOrStay.Equals("stay")); // ask the user to hit or stay, and loop it until they give an acceptable answer
Game(); // call the Game() method
}
}
The Hit() method is quite simple. We add another card to our hand, and see if we got Blackjack, busted, or are still less than 21.
And finally, the PlayAgain() method. It's /very/ simple.
static void PlayAgain()
{
string playAgain = ""; // create a string to ask to play again
do
{
playAgain = Console.ReadLine().ToLower();
} while (!playAgain.Equals("y") && !playAgain.Equals("n")); // in the other methods, we asked if they wanted to play again, now we're just getting input and looping it until they give an acceptable answer
if (playAgain.Equals("y")) // if they want to play again..
{
Console.WriteLine("\nPress enter to restart the game!"); // tell them to press enter to play again
Console.ReadLine(); // get input (Enter)
Console.Clear(); // Clear the Console
dealerTotal = 0; // reset the dealer total
count = 1; // reset the count
total = 0; // reset our total
Start(); // call the Start() method to restart the game
}
else if (playAgain.Equals("n")) // if they don't wanna play again..
{
Console.WriteLine("\nPress enter to close Blackjack."); // tell them to press enter to close the game
Console.ReadLine(); // get input (Enter)
Environment.Exit(0); // exit the Console with exit code 0
}
}
And all this does is ask them to play again, and if they want to, reset all the values, and call Start(), otherwise, close the Console.
How do we put all this together? In the main method, type in
Start();
and thus the game begins!
Here is the full code (uncommented):
using System;
namespace Blackjack
{
class Blackjack
{
static string[] playerCards = new string[11];
static string hitOrStay = "";
static int total = 0, count = 1, dealerTotal = 0;
static Random cardRandomizer = new Random();
static void Main(string[] args)
{
Console.Title = "Seth's Blackjack!";
Start();
}
static void Start()
{
dealerTotal = cardRandomizer.Next(15, 22);
playerCards[0] = Deal();
playerCards[1] = Deal();
do
{
Console.WriteLine("Welcome to Blackjack! You were dealed " + playerCards[0] + " and " + playerCards[1] + ". \nYour total is " + total + ".\nWould you like to hit or stay?");
hitOrStay = Console.ReadLine().ToLower();
} while (!hitOrStay.Equals("hit") && !hitOrStay.Equals("stay"));
Game();
}
static void Game()
{
if (hitOrStay.Equals("hit"))
{
Hit();
}
else if (hitOrStay.Equals("stay"))
{
if (total > dealerTotal && total <= 21)
{
Console.WriteLine("\nCongrats! You won the game! The dealer's total was " + dealerTotal + ".\nWould you like to play again? y/n");
PlayAgain();
}
else if (total < dealerTotal)
{
Console.WriteLine("\nSorry, you lost! The dealer's total was " + dealerTotal + ".\nWould you like to play again? y/n");
PlayAgain();
}
}
Console.ReadLine();
}
static string Deal()
{
string Card = "";
int cards = cardRandomizer.Next(1, 14);
switch (cards)
{
case 1: Card = "Two"; total += 2;
break;
case 2: Card = "Three"; total += 3;
break;
case 3: Card = "Four"; total += 4;
break;
case 4: Card = "Five"; total += 5;
break;
case 5: Card = "Six"; total += 6;
break;
case 6: Card = "Seven"; total += 7;
break;
case 7: Card = "Eight"; total += 8;
break;
case 8: Card = "Nine"; total += 9;
break;
case 9: Card = "Ten"; total += 10;
break;
case 10: Card = "Jack"; total += 10;
break;
case 11: Card = "Queen"; total += 10;
break;
case 12: Card = "King"; total += 10;
break;
case 13: Card = "Ace"; total += 11;
break;
default: Card = "2"; total += 2;
break;
}
return Card;
}
static void Hit()
{
count += 1;
playerCards[count] = Deal();
Console.WriteLine("\nYou were dealed a(n) " + playerCards[count] + ".\nYour new total is " + total + ".");
if (total.Equals(21))
{
Console.WriteLine("\nYou got Blackjack! The dealer's total was " + dealerTotal + ".\nWould you like to play again?");
PlayAgain();
}
else if (total > 21)
{
Console.WriteLine("\nYou busted, therefore you lost. Sorry. The dealer's total was " + dealerTotal + ".\nWould you like to play again? y/n");
PlayAgain();
}
else if (total < 21)
{
do
{
Console.WriteLine("\nWould you like to hit or stay?");
hitOrStay = Console.ReadLine().ToLower();
} while (!hitOrStay.Equals("hit") && !hitOrStay.Equals("stay"));
Game();
}
}
static void PlayAgain()
{
string playAgain = "";
do
{
playAgain = Console.ReadLine().ToLower();
} while (!playAgain.Equals("y") && !playAgain.Equals("n"));
if (playAgain.Equals("y"))
{
Console.WriteLine("\nPress enter to restart the game!");
Console.ReadLine();
Console.Clear();
dealerTotal = 0;
count = 1;
total = 0;
Start();
}
else if (playAgain.Equals("n"))
{
Console.WriteLine("\nPress enter to close Blackjack.");
Console.ReadLine();
Environment.Exit(0);
}
}
}
}




MultiQuote







|