A Little History
The reason I’m revisiting blackjack is because I have a C++ version and I want to port it to PHP, for fun. If you want to have a simple project for learning a language, other than “Hello World,” then Blackjack is a good project.
The reasoning is that Blackjack is pretty simple, has few rules, deals with pesky randomization (which was a bitch back before STL days (God I love STL), damn compiler bastards, except for Microsoft and GCC). Anyway, Blackjack teaches and allows practice for:
- Randomization
- Handling input and output
- Looping
- Conditional Statements
- Procedural Programming (totally don’t want everything in Main)
Disclaimer: This code is from my friends project. I did Blackjack using OpenGL toolkit, it was crap, but was a great learning experience (even though I forgot most of what it was that I learned).
Guidelines
Procedural Programming
Okay, it is generally a bad idea to have everything in the main(), gets cluttered and just amateurish. For a project of this size, it is a good idea to break things down into reusable functions to help with debugging and development. It is also good practice.
Keeping it Simple
The friend’s project broke it down to the principle that the card was not important, but the value was. The only problem with this, was that you couldn’t say, deal an Ace and have it either equal 1 or 10. The nice thing was that it kept from building a deck, keeping track of the cards, and keeping track of the players hand. Good stuff and I would had spent a great deal of time building it to where there was a deck. The idea is quick and simple, so it is good stuff, no matter how much I disagreed with the implementation.
1. Intro
The intro states the games intention and gets the game started.
cout < < "Welcome to BlackJack!" <<endl <<endl; cout << "Press Any Key to Start a Game." <<endl <<endl; getch();
The interesting part worth noting is getch(), which gets a character for a variable. Since there is no variable, it will pause the console until one is pressed and then continue the execution. Nice trick!
echo "Welcome to Blackjack!\n\n"; echo "Press Any Key to Start a Game.\n\n"; fgetc(STDIN); // same effect as getch().
Very similar.
2. Holding Stats
The original version held the stats in the main in separate variables and updated the variables by passing by reference. I thought it would be useful to use a global struct instead.
struct bjStats
{
int wins;
int loses;
int draws;
} Stats = { 0, 0, 0 };
class Stats
{
static public $wins = 0, $loses = 0, $draws = 0;
}
The usage would be the similar for both languages. For C++, the access would use an period instead of the "->" symbol, in PHP.
3. Randomization
The nice thing about PHP is that you don't need to seed the random number using srand(). I usually seed in main() and leave rand() in another function.
return(rand() % 10 + 1);
return mt_rand(1, 11);
I like php better, because you don't have to worry about the tricky math related to the modulus symbol.
Possibly Related Posts:
- Game Engine Development and Open Source
- Plans for Base CMS
- Bullet: E-Book Library Management and Content Server
- Using ZendFramework 2 beta1 For Directory Project
- Project Plans
Hey if you like blackjack and want to look at subtle different version also written in PHP you should swing by mysite. I wrote a few, one that is ajax using php and javascript, one with a login system and one just for fun. All Blackjack. All PHP. Come by and tell me what you think.
hi, nice content added you to my rss reader thanks
im actually looking for a blackjack cardcounting and basic strategy script
please contact me
This was an attempt to write a tutorial on how to write a CLI PHP script for playing Blackjack, it however wasn’t ever completed and I don’t have any motivation to complete it.
Thanks for adding me to your feed reader, I should warn you that not everything is development and I do post some more personal ideas and thoughts.