Since this is my fist post I figured I would introduce myself and show what I've been able to accomplish so far.
I'm at the end of my first year in college after taking over a decade break (I'm 31). I took a few computer programming classes in high school but that was a very long time ago. Since then, I've decided to go back to school(college) because one thing I did like from high school were those programming classes. Instead of asking a bunch of questions I figured I would learn c++ and see what I can figure out on my own. Since I won't be taking any programming classes for at least another year in college (currently taking calculus/physics classes), I wanted to get ahead and learn as much on my own as I can. I'm a computer science major and will be taking Calc II and Physics I next semester. Anyways, this is the code for my black jack game. It is you vs the computer at this point, with no other players. It is probably very simplistic to others designs, but it does work, which at this point is a win for me. It incorporates arrays, if statements, loops, random numbers, and a void array.
The next step I would like to take is incorporating up to six players and making the deck "real". By real, I mean once that card is used, it cannot be used again until all remaining cards are drawn.
If I can help anyone else out with the limited knowledge I have, I'd be glad to help. It's my plan to become regularly immersed in the information on this forum. Sorry in advance if my code doesn't paste properly. Criticism is welcome.
#include "pch.h"
#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include <ctime>
usingnamespace std;
void randomCard(); //Let program know I have a random card function below
int random_integer; //Global random integer variable because I dont know how to pass to functions yet
int main()
{
srand((unsigned)time(NULL)); //Seed random number from current time
char playAgain; //playAgain needs to be declared outside of program loop since do/while is dependent upon it
do {
char hitOrStay; //Declare/reset all variables
bool p1Bust = false; //Possibly don't need to recreate the arrays
bool houseBust = false;
int p1Counter = 0;
int houseCounter = 0;
int player1Total = 0;
int houseTotal = 0;
int houseHand[8]; //arrays to store players hands
int playerHand[8]; //Need to figure out how to create as many array "hands"
//as I need when adding more players
constint howManyCards = 52; //Create "deck" with an array
int deck[howManyCards];
for (int i = 0; i < howManyCards; i++) { //Fill the array "deck" with 52 cards
if (i == 0 || i == 1 || i == 2 || i == 3) { //Create each cards value and enter it into the array
deck[i] = 11; //These are aces, but I don't have 11 OR 1 setup yet...
}
elseif (i == 4 || i == 5 || i == 6 || i == 7) { //It also doesn't display clubs, spades...etc :(
deck[i] = 2; //just the value of the card...
}
elseif (i == 8 || i == 9 || i == 10 || i == 11) {
deck[i] = 3;
}
elseif (i == 12 || i == 13 || i == 14 || i == 15) {
deck[i] = 4;
}
elseif (i == 16 || i == 17 || i == 18 || i == 19) {
deck[i] = 5;
}
elseif (i == 20 || i == 21 || i == 22 || i == 23) {
deck[i] = 6;
}
elseif (i == 24 || i == 25 || i == 26 || i == 27) {
deck[i] = 7;
}
elseif (i == 28 || i == 29 || i == 30 || i == 31) {
deck[i] = 8;
}
elseif (i == 32 || i == 33 || i == 34 || i == 35) {
deck[i] = 9;
}
else {
deck[i] = 10; //Anything 10 or above in the array will be value of 10
}
}
cout << "Welcome to the Black Jack table." << endl; //First line displayed
cout << "Press enter to play!" << endl;
cin.ignore();
system("CLS");
cout << "Player 1 press hit enter to reveal your cards..." << endl;
cin.ignore();
randomCard(); //Player 1 and house get 1st card
houseHand[houseCounter] = deck[random_integer]; //House 1st card
houseTotal = houseTotal + houseHand[houseCounter]; //Add it to house total
randomCard();
playerHand[p1Counter] = deck[random_integer]; //Player 1 gets first card
p1Counter++; //Increase p1Counter so next in the array is 1 (2nd card)
do { //This do while loop is specifically for the player, to ask if they want to hit/stay again
system("CLS"); //Clear screen each loop to look better
player1Total = 0; //Resets total to zero after each loop for retotaling cards
//because I have the player 1's total calculated in a loop
randomCard();
playerHand[p1Counter] = deck[random_integer]; //Get each additional card after the first one
//p1Counter now is 1 (second card)
cout << "Your hand :";
for (int i = 0; i <= p1Counter; i++) { //Prints individual cards to screen and adds their running total
cout << " " << playerHand[i] << " "; // i points to position in array
player1Total = playerHand[i] + player1Total; // Total is equal to itself plus position pointed to
} //in array
cout << " " << endl;
cout << "House Hand : " << houseHand[houseCounter] << endl; //Display what the house's card is
//using houseCounter to point in array
if (player1Total > 21) //Figure out if the player has busted
{
p1Bust = true; //Set p1Bust bool value to true if player busts
}
elseif (player1Total == 21) //Automatically stay if players hand is 21
{
cout << "/nBlack Jack!" << endl;
hitOrStay = 's';
cin.ignore();
}
else
{
cout << "Would you like to hit (h) or stay (s) ? " << endl; //If other conditions fail
cin >> hitOrStay; //they have a choise to hit or stay
if (hitOrStay == 'h') { //If they want to hit 'h' increase the players counter by one so the next
p1Counter++; //card they draw will be in the next position in their hand array
}
}
} while (hitOrStay == 'h' && p1Bust == false);//If player wants to hit and they haven't busted loop again
system("CLS");
cout << "Player one results " << endl; //Display players results after they are done hitting or busting
if (hitOrStay == 's') { //Display if they stayed
cout << "You stayed \n" << endl;
}
cout << "Player 1 hand :"; //Display players individual cards
for (int i = 0; i <= p1Counter; i++) { //and adds total of cards
cout << " " << playerHand[i] << " "; // i is pointing to 0-p1Counter in playerHand array
}
cout << " Player 1 Total: " << player1Total << endl; //Display the calculated total for player
if (p1Bust == false) { //If player one busts skip straight to player one lost
do { //If the player has not busted then the house will draw it remaining cards
houseCounter++; //Use a counter to keep track of where we are at in the array
randomCard(); //Call random number function
houseHand[houseCounter] = deck[random_integer]; //House's hand array gets a random card in the deck array
houseTotal = houseTotal + houseHand[houseCounter]; //Keep track of the house's total hand count
} while (houseTotal < 17); //If the house total hand count is below 17 they hit again
cout << "\n\nHouse Hand: ";
for (int i = 0; i <= houseCounter; i++) { //Simple loop to print out the house's hand
cout << houseHand[i] << " "; //Use the i variable starting at zero in the house's array
}
cout << " House total :" << houseTotal; //Print the house's total from the previous do while loop
if (player1Total > houseTotal || houseTotal > 21){ //If player 1 has a higher total or the house busted...
cout << " \n\n\nCongrats you won" << endl; //Player1 wins
}
elseif (player1Total == houseTotal) { //Else if Player 1 and house tie...
cout << " \n\n\nTied, hand is a wash" << endl;
}
else {
cout << " \n\n\nYou lost, good try" << endl; //If Player 1 and House did not bust, yet house still
} //has a higher total...
}
else { //If player one bust is not false...
cout << "\n\n\n Bust, sorry you lost " << endl;
}
cout << "Would you like to play again? Yes(y) No(n)" << endl; //Ask if player would like to play again
cin >> playAgain; //Get input for character playAgain
if (playAgain == 'n') { //Exit statement
system("CLS");
cout << "\n\n\nThanks for playing.\n\n\n\n\n\n\n\n" << endl;
system("pause");
}
}while (playAgain != 'n'); //Continue the program loop while playAgain does not equal 'n'
return 0;
}
void randomCard() {
random_integer = rand() % 52; //Random integer function, used for getting a random card out of deck
} //Mainly because I didn't want to write this statement repeatedly
cout << "Would you like to play again? Yes(y) No(n)" << endl; //Ask if player would like to play again
How does that tell me anything that isn't utterly obvious? It's just clutter. Virtually all of your comments are the same way, simply translating to English what the code obviously and clearly already says. Do you see how useless (indeed, worse than useless) that is?
Also, beginners seem to have a bizarre fascination with endl. Just use '\n'.
Anyway, functions are not complicated and are very useful, so you should learn the basics of them right away. And a real deck is pretty easy too. Consider this little demo.
cstdlib, not stdlib.h … this can cause weird compiler errors every once in a while if you keep using C headers mixed with C++ headers. Its usually ok but awkward, the errors are a very specific set of circumstances that are kinda rare.
you will get a lecture on 'using namespace std' which is considered to be a problem; std:: in front of the stuff is the current best practice, or more limited using statements.
consider updating to use <random> instead of C's random tools.
consider using <vector> instead of arrays. You don't need them here but you will later and practice now will help, and you can do a couple of neat things with them that might be handy like sort, shuffle and so on.
ascii has symbols for all 4 standard card suites. you can use this for now if you want to be cute.
that gigantic if statement is horrible. Just make an array with hard coded values eg int diamonds[] = {2,3,4,5,6,7,8,9,10,10,10,10,1}; or whatever approach but don't fill it in with logic like that.
if you want to play, try adding a betting amount / cash score feature and multiple shoes of cards. Blackjack with 1 deck is a seriously flawed game.
much of what I am saying TPB did but without any explains his stuff is likely difficult to follow for a new coder. If you can try to follow it, though, he is showing you a lot of good stuff.
Don't take anything said too harshly. Its pretty good for an early program by a self taught programmer.