Dion Moult In honour of the late Dion Moult, 1992 - 2012In honour of the late Dion Moult, 1992 - 2012

A Little 21 Fun with C++.

Given the recent love I’ve shown to a great deal of languages, including but not limited to making a text editor with Python (Ssss!), challenging your manliness with LaTeX (what real men useā„¢), and most recently a tribute to a great deal of languages that all mesh together in the most wonderful way in “How to Make a Website part 1 – The Environment“, I’ve decided it’s time to give C++ a chance to show itself on this blog.

When I decided to learn application programming (from web programming), I chose the languages Python and C++. Python for the ultimate rapid application development it can be used for, as well as the ease of syntax that should introduce the application programming concepts a lot quicker to me, and C++ for, well, it’s pure power. C++ can be described as the swiss-army-knife of a programming language … a decently low level language, strictly typed, nicely object orientated, efficient, and it’s actually compiled, unlike what I’m used to (oh my, what a spoiled generation I live in). Another reason being that C++ is the main language used in KDE development, of which I would like to contribute to someday.

Today, being a Friday, was the be-all and end-all of the week, where I enter my weekend early, and take a break from the ongoings in my life. Therefore, I decided to poke again into C++. Armed with nothing but with whatever little I remembered from the first peek at the language more than 2-3 months ago, the man pages, and general knowledge of language syntax (well, the syntax highlighter gave some clues), I decided to make myself a BlackJack game. Suffice to say that the process was pain-free, and within 15 minutes I was playing BlackJack!

It is, of course, a CLI app, but I don’t see why it shouldn’t be cross platform. However, since I don’t have a Windows computer I can compile it on, you Windows folks will have to do it yourselves. For the technophobes, I give you a screenshot (hahaha):

2009-01-16-234418_1280x800_scrot

…and for the more logic inclined, well … a combination of basic common sense and mathematics should be enough for you to get the gist of what the code is doing. (It’s even commented for the hard of thinking) Take a look:

#include <iostream>
/*
* Generate random number that represents a card (1-13)
*/
int generate_card()
{
return rand() % 13 + 1;
}

/*
* The AI will always have a card between 15-21. This will generate their card.
*/
int generate_ai_card()
{
return rand() % 7 + 15;
}

/*
* Let's play the game!
*/
int main()
{
// This creates a random seed for rand() based on the time.

srand(time(NULL));

bool play;
int ai_card, my_card;
char x;

std::cout << "Welcome to a very simple BlackJack game to kill time.\n";

play = true;

// Get the AI's card.
ai_card = generate_ai_card();

// Get your first card.
my_card = generate_card();

std::cout << "You have " << my_card << " - (h)it or (s)tay?: ";

// Ask for a hit or stay.
do
{
std::cin >> x;

// If they have hit...
if ( x == 'h' )
{
// Generate a card, and add it on.
my_card = my_card + generate_card();

// Have they bust?
if ( my_card > 21 )
{
std::cout << "You got bust (" << my_card << ").\n";
play = false;
}
else
{
std::cout << "You now have " << my_card << " - (h)it or (s)tay?: ";
}
}
// If they stayed...
else if ( x == 's' )
{

// ... See who is bigger.
if ( my_card > ai_card )
{
// You WIN!
std::cout << "You won! (" << my_card << " vs " << ai_card << ")\n";
play = false;
}
else
{
// You have FAILED.
std::cout << "You lost! (" << my_card << " vs " << ai_card << ")\n";
play = false;
}
}
else
{
std::cout << "Please either type \"h\" to hit or \"s\" to stay: ";
}
}
while ( play == true );
// Some credits and stuff.
std::cout << "Thank you for playing Dion Moult's C++ BlackJack game.\n";
return 0;
}

Again, it amazes me how I can turn a quick 15 minutes of nothing into an entire blog post. Perhaps I should finish off one of my drafts and give you something interesting for once.

No related posts.


3 Comments

thinkMoult - The Road to KDE Devland (Moult Edition) #0 « says: (26 July 2009)

[...] do know PHP and do web-development. I have coded a black jack CLI game in C++ (well it was a start). I do graphics design and am proficient in The GIMP. I once touched Python [...]

krunal says: (19 January 2010)

Hi how come when i press s the program just cancels out u also forgot to declare time.h

Dion Moult says: (23 January 2010)

This was compiled on Linux, I guess it might be a different on Windows – this doesn’t require any time.h file so I’m not quite sure what you’re talking about.

Leave a Comment