Blackjack C Program Code

Blackjack C Program Code

BlackJack Program Description

The code for the program begins with a clear (clc) statement to clear anything currently within the command window and workspace. Once this has run, three different arrays (assigned to variables deck, deck_value, and card_names) are then created. These arrays will serve as the basis for the rest of the following code. Once the arrays have been created, a print statement is used to create the welcome screen for the player, and an input is written to let the player choose when to proceed. After the game has been initialized the choice1 variable is set equal to 1 and a while loop that includes the rest of our code is set to run while that variable remains equivalent to 1 and stop if the variable’s value switches to 0. At the end of our code the choice1 variable is set equal to the player’s input, which will determine if the code should rerun again based on the player’s choice.

At the start of the code within the loop two variables are created, d and dealer_ace_card, to create the basis of a while loop that will run twice in order to deal two cards to the dealer. Within the loop, the program chooses a random number out of the length of the deck array using the randi and length operators. Once this number is chosen, it then goes to that position in the deck array and assigns the number to the variable card. The script then looks for the value associated with that card variable by going to the same position in the deck_value array and assigns that number to card_value2. The same process is used to get the correct position within the card_names string array. This value is assigned the variable card_name. In order to correctly hide the dealer’s first card, two if statements are included within the loop. If the code recognizes that it is the first time the loop has run (d = 1), it will store the first card in the variable first_card to be later displayed. However, if it is the second time the loop has run (d = 2) the code will print a message showing the dealer’s second card. Another if statement is then included to register if any of the cards dealt are an ace. If so, the value of dealer_ace_card is set equal to 1. This allows the code to register that there is an ace in the dealer’s hand. At the end of each iteration of the loop the deck array is updated to take out the card drawn. After the loop has been completed, the sum of the card values found is added together and assigned to the variable dealer_sum. In rare cases, the dealer may have drawn two aces at this point, which would make the dealer_sum 22. In order to account for this, an if statement was made to check if the dealer_sum is above 21 at this point. If so, one of the ace values is changed to 1 by subtracting 10 from the current dealer_sum.

Aol Blackjack Codes

Here is the code so far: namespace Blackjack class Program static Player players = new Player5; static int pointer = 0; class PlayingCard public string Suit.

Almost the exact same process described above is then repeated for the player. Two variables, i and ace_card, are created and a while loop is made to run twice. The variable card is chosen, then card_value and card_name are found in reference to that variable. An if statement is created to recognize if an ace has been drawn. For each iteration of the loop, the card drawn is displayed, and the value associated with that card is taken out of the deck array. After the loop has completed, a similar if statement is made to check if the player_sum has exceeded 21 at this point. If so, the value of one of the aces is changed to 1 by subtracting 10 from the player_sum. Then a print statement displays to the player the current total they have. At this point, the initial steps of the game have been completed.

Before proceeding to the player’s choices, the code first checks if either the player or the dealer have achieved BlackJack (21 in your starting hand). Three if statements are created to check these conditions. The first checks if the player_sum is equivalent to 21 while dealer_sum is not. If these conditions are met, a message is printed saying that the player has achieved BlackJack. The second does the reverse, checking if dealer_sum is equivalent to 21 while player_sum is not. If these conditions are met, a message is printed saying that the dealer has achieved BlackJack. The last runs if neither player_sum nor dealer_sum are equivalent to 21. If so, instructions are printed using an fprintf statement telling the player to choose 1 for hit and 0 for stand, and the next loop begins.

  1. This program simulates a game of Blackjack, where the user is the player and the computer is the dealer. C Blackjack (Mini project) is a Games source code in C programming language. Visit us @ Source Codes World.com for Games projects, final year projects and source codes.
  2. The code is unfisished yet, but one of the main problems i have is how to print the cards, i wrote a function that is supossed to do this but i am having a touble using it in the code. Run example: Welcome to CS intro Blackjack! Start by entering a random seed: 5 You currently have 1000 fake dollars. How much do you want to bet?

The next loop is a while loop that is created to run while player_sum is less than 21 and dealer_sum is not equal to 21. At the start of the loop, variable choice is set equal to the input of the question “Would you like to hit or stand?:” From the printed instructions above, the player knows to input 1 for hit and 0 for stand. An if statement has been created in order to ensure that the input is equivalent to 1 or 0. If the input isn’t equivalent then a print statement will run showing there is an invalid input and the player will be prompted with the input again. If the input of the variable choice is equivalent to 1, then another card will be drawn from the deck using the same process outlined above. A random number is taken out of the deck array and assigned to card. Then the card variable is used in order to get the corresponding values from deck_value and card_names, and the card variable chosen will be taken out of the deck array. Lastly, an if statement will be used to check if the variable ace_card is equivalent to 1 and the player_sum is greater than 21. If both conditions are met, the program displays a message showing that the player will bust if the ace stays the value it is currently, player_sum will have 10 subtracted from its total to account for the ace’s change of 11 to 1, and the ace_card variable will be set equal to 0 once again in order to show it has accounted for the ace. At this point the player will be prompted again with the same input asking to hit or stand. If the player chooses 1 then another card is drawn in the same fashion as above. If the player chooses 0, the code knows to display the player’s final total stored in player_sum and to break out of the code. The loop will also end if player_sum exceeds 21 as values are added.

Once the loop has ended, an if statement is set to to check if player_sum has exceeded 21, and if so to display that the player has busted. An elseif is included to run if the starting_player_sum doesn’t equal 21, the player_sum is less than or equal to 21, and the dealer_sum doesn’t equal 21. If these logicals are found to all be true the dealer’s first card and starting total is printed and the next loop begins. The next loop is a while loop set to run while dealer_sum is less than player_sum or dealer_sum is less than 17. The loop displays that the dealer has hit and then includes an input in order to slow down the outputs shown in the command window. The exact same process for the previous loop is used except the values are stored in card_value2 and added to the dealer_sum. If statements are also included to check for an ace and change the value if needed. An if statement within the loop checks if dealer_sum is greater than 21. If so the program will print that the dealer has busted and break out of the code. At the end of each iteration of the loop another input is included in order to let the player choose when to proceed. After the loop, an if statement is included to display the dealer has stood if he has gotten a value higher than player_sum or tied with player_sum at a value of 17 or above. Then multiple if and elseif statements are included to compare the values of player_sum and dealer_sum. If dealer_sum is greater than player_sum without going over 21, the dealer wins, if player_sum is greater than dealer_sum without going over 21, the player wins, if both variables are equivalent it’s a tie, and if both variables go above 21 the dealer wins. All of these are associated with the proper print statement. At this point the player is asked if they would like to play again. If so, the code restarts again at the start of paragraph 2.

Variables

  1. deck – An array of numbers 1 through 52. This will serve as all the cards within our deck
  2. deck_value – An array in equal size to the deck array. Includes all the possible values that a card could be and puts the value in the same position as that of the deck array.
  3. card_names – A string array of the same size as deck and deck_value. Includes the name of all cards within the deck put within a correlated position to display the correct card name with the associated value.
  4. choice1 – A variable initially set equal to 1 that determines whether the rest of the code within the while loop will run based on the players input after the first run.
  5. d – Allows the dealer to draw their inital hand while d is not greater than 2.
  6. dealer_ace_card – Determines if the dealer has an ace within their hand. Whenever the dealer draws an ace, the value of this variable changes
  7. card – Chooses a random number out of the length of deck and then goes to that position within the deck array.
  8. card_value2 – Reads what number the card variable landed on and goes to that position within the deck_value array.
  9. card_name – Reads the value of card and goes that position within the card_names string array.
  10. first_card – Stores what the first card drawn by the dealer was, but doesn’t display it.
  11. shown_sumStores the known second card value.
  12. dealer_sum – The total that the dealer has from the cards that have been drawn.
  13. i – Allows the player to draw their initial hand while i is not greater than 2.
  14. ace_card – Determines if the player has an ace within their hand. Whenever the players draws an ace, the value of this variable changes.
  15. card_value – Similar to card_value2, it records the number that the card variable landed on and goes to that position within the deck_value array, but for the player
  16. player_sum – The total that the player has from the cards that have been drawn.
  17. starting_player_sum – Keeps the total of the players first two cards to let the code recognize if the player got BlackJack.
  18. choice – Set equal to the input the player chooses. Lets the code know what to run based off if the player chooses hit or stand.

Over/Under 7 Program Description

Blackjack

The game starts off with saying clear and clc which removes all previous stored values for variables and empty’s the command window. The variable play is put in place for a while loop that contains the whole game to keep the game running without having to restart. The coins variable is also not included in the while loop so that it does not reset while the player loops back to the top of the game multiple times, it will remain constant through every run until they end the game. The while play1 encompasses the actual gameplay to create the loop of continuous play if desired. The next step is to create the visual displays by using the my_scene and dice_sprites variables. The code uses fprintf statements to tell the player their initial coin value and the options for choosing their bet of over or under. Then an input command is given to give the choice variable a value. A while loop is set up after that reads the input choice and checks if the input was not one of the given options, if not it says invalid bet and prompts the input statement for a correct input. When the correct input for choice is set, the same steps occur with the bet amount. An input statement calls for the player to insert the amount of coins they wish to bet and a while loop checks that bet to make sure it is not more coins than they currently have. When a valid bet is set the next step is to randomize two dice values by using the randi(6,1,2) function. This randomly generates a number 1 through 6 twice. Then using the sum(dice_roll) command, the two dice values are summed up and printed out using the fprintf command. The draw_scene variable is then used to match both of the dice rolls to the proper visual dice display. The next step is for a series of if elseif statements to compare the choice of the bet against the sum of the dice rolled. It first checks if the variable choice was given a value of 2 and the bet was greater than 7. If it was, an fprintf command says the player won and it changes the value of coins to initial coins plus the bet of coins. The elseif statement checks if the value of choice was not equal to 2 and the sum being greater than 7. This results in a loss signaling the fprintf command to say the player loses and changes the value of coins to be the initial coins minus the lost bet. This process is repeated with two more identical if elseif statements that checks based off of the other choice options. After the choice is checked and the correct win loss function is executed the next step of the code is to check the current total of the coins. Using an if statement, the value of coins is checked to see if it equals 0, if so the game is over because the player lost all their coins. The game will then exit the game loop and the player will have to rerun the code to start back over with a fresh set of coins. The player then is prompted with an input statement that asks if they would like to play again. Once again, a while loop is used to ensure the player has to input a valid answer to playing again and setting the play variable with a correct value. A final if statement checks if the value of play is 1 then it goes back to the top just like the first while loop indicates and plays the game again. If the value of play is 0 the game thanks the player for playing using an fprintf statement, then exits the game loop thus ending the code.

Blackjack c program codes

Variables

  1. play=1 This variable is created so it can be used at the end of the code, an if statement checks to see the new value of play from the input, defending on the input the game will stay in the loop or not
  2. coins=1000 This is the starting amount for betting and is edited later throughout the game
  3. bet= input this will be the amount of coins placed into the pot for the dice roll
  4. my_scene = simpleGameEngine(‘retro_simple_dice.png’,16,16,8) This creates the image display for the dice
  5. dice_sprites = 1:6 This sets up the variables for different sprites
  6. choice = input This is where the player puts in their bet which is then checked in an if else statement against the sum of the dice
  7. dice_roll = randi(6,1,2); this creates a random roll of a dice
  8. dice_sum = sum(dice_roll) this then sums of up the value of the two dice

Project Summary:

Blackjack is a popular casino card game. The game is played between a player and a dealer, where the object of the game is to get the value of the cards to reach as close to 21 as possible. Using MATLAB code Group F planned and created a Blackjack game where the user plays against a computer dealer. The code included MATLAB functions when taken together created a functioning code allowing for actual game play. The purpose of this was to explore and implement logic and code in order to present a fully functional game.

The beginning of this project started out with a basic algorithm of the game which outlined a step by step process of how the game is played from beginning to end. Using the algorithm as a guideline, the code was put into place. The beginning code took advantage of built in functions, such as “showim()” and “randperm()”, and basic concepts such as number vectors, which assigned values to cards to create the foundations of the game. For example, two vectors were established: one which documented the the order in which the cards appeared in the image file, another which assigned numerical values (card values) to the first vector. Once card value vectors were established, a system for dealing cards from the deck was created. Some functions were created specifically for the game such as “addcard()” and “sumcard()”, which added a card to a player’s hand and summed the value of the cards in the player’s hand, respectively.

With these functions in place it was possible to create a loop for the player’s turn. The player immediately would enter a loop and the player’s turn would begin by being dealt the first and third card in the array (while the second and fourth card were reserved for the dealer). These cards would appear on the screen. The player would have two cards from the shuffled deck and could see one of the dealer’s cards. The player would have the option to “Stay” (end turn) or “Hit” (simply by typing “hit” or “stay” and was achieved by checking the string for the exact word) which would add a card to the player’s hand from the next card in the vector, the program would calculate the total value of the cards in the player’s hand. So long as the player’s card values had not reached 21 the player could choose to again Hit or Stay. Once the player chose to stay, the dealers turn would begin.

The dealer was programmed to operate independently of the player. The dealer was programmed using simple if then statements: if the dealer card’s were at any point under a score of 17 the program would request another card be added to the dealer’s hand. The dealer would, thus, stop once it’s total was 17 or above. If the dealer went over 21 the player would automatically win.

House of blackjack codesProgram

Finally, a full game loop was implemented in which tied the game together. The player was first prompted to ask if he or she would like to start a game of Blackjack. Included in this full game loop was a betting system, which allowed the player to bet money. The player would start out with $200 and could place a bet between $25 and $100. If the player at any point in the game had a score of 21 or over, the money would be subtracted from the player’s total. Additionally, if after the dealer’s play was over, if the dealer’s score was higher than the player’s the money would be subtracted from the player’s total. Otherwise, (that is, the dealer busted or the player had a higher score than the dealer) the player’s bet would be added to the player’s total. The game continued until the player ran out of money or decided to quit.

Blackjack C++ Program

Improvements and additions could be made to future versions of this program, such as adding more complex casino rules to the game such as Insurance, Splitting, Double, and Surrender. Additionally, the program could incorporate multiple players. In future versions the graphical interface could be improved to show cards on a virtual Blackjack table as well as animations of the cards being moved from the deck to the players and dealer.


Table Of Contents:
1. Project Management

  • Team Working Agreement
  • Individual Responsibility Agreement
  • Project Schedule
  • Meeting Notes

2. Business Plan

  • User Identification and Interviews
  • Electronic/Print Advertisement
  • Pitch Video with Demonstration
  • Introduction
  • User Manual
  • Program Description for Developers
  • Final Algorithm, Flowchart, or Pseudocode
  • Final Program with Comments
  • Discussion
  • Conclusions and Recommendations
  • References