Skip to content

mpoterek/Blackjack

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Blackjack

Single player blackjack game in C++ CSE 20312

From the user’s perspective, this blackjack game is relatively simple. When the executable is entered, the user is immediately presented with both his/her hand and the dealer’s hand, and the option to hit or stand. If she chooses to stand, the total value of the player’s hand is compared to the total value of the dealer’s (after the cards necessary to reach 17 have been added). Whoever has the higher score (that remains less than or equal to 21) is declared the winner. The current score between the dealer and the player is then displayed and the player is asked if she would like to play another game, and the process begins again or the program is exited. If the player chooses to hit, a card is added to her hand (and the hand displayed). The player can continue to choose hit until she busts, at which time a message is displayed declaring the dealer the winner and displaying the score. Alternatively, if the player achieves 21 with her original hand (the first two cards), “blackjack” is displayed and she is declared the winner. The player is also declared the winner if the dealer’s draw to 17 results in a bust. Internally, the blackjack program used the class CardDeck as a basic structure for the program. In CardDeck.cpp, this class creates an array of integers, with memory allocated dynamically and 52 integers making up the 52 cards found in a deck. In the non-default constructor, the array is initialized to the values 0-52, which are then converted to their modulo value if divided by 13 plus 2, so each number value is 2-14. Numbers greater than 11 were then converted simply to 10, giving a deck filled with values 2-11, which are the only relevant values in Blackjack. There are also access functions to the deck’s size and the deck itself (its values). The main gameplay functionality, called in main, runs through the play() function. This function creates two vectors—one for the player and one for the dealer—and initializes them to hold the first four elements of the shuffled deck. Depending on player choices, functions to add cards to a hand were used to add to that vector or find the sum of a vector’s contents were used, calling the vector by reference to facilitate the function’s ability to modify the actual vector, rather than just its values locally. Each time either vector was created or modified, both were checked for blackjack or bust conditions, and dealt with appropriately. The cardnum variable was used throughout (and passed by reference) to ensure that 15 or more cards remain in the deck, and if not the shuffle function is called to create a new deck and shuffle it. In Cardmain.cpp, the gameplay is started and continued until the player chooses to quit. Additionally, the main driver keeps track of the number of wins the player and dealer have accumulated within the game and uses srand to seed the shuffle function in the implementation.

To verify the program and eliminate the possibility of user error, error checks are in place at each point the user might enter a value—when being prompted to hit or stand, or when asked to continue. If the user enters an invalid number, i.e. a number besides 1 or 2, when in game play, he/she is asked to try again. When an invalid number is entered when the user is asked if he/she would like to play another game, the player is notified of the invalid entry and the program is exited. To verify the rest of the program’s functions, many cout statements were used to verify that the deck was being shuffled correctly, the modulo operator was functioning as intended, and that win statements and the addition of each hand was occurring correctly.

About

Single player blackjack game in C++

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published