This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
notes:discrete:fall2023:projects:bjm0 [2023/10/23 16:35] – [GAMEPLAY] wgates1 | notes:discrete:fall2023:projects:bjm0 [2023/11/02 01:53] (current) – [Ace Logic] cfoster8 | ||
---|---|---|---|
Line 19: | Line 19: | ||
=====GAMEPLAY===== | =====GAMEPLAY===== | ||
- | ===Ace Logic=== | + | ===Shuffle Cards and Values=== |
+ | In past documentation pages we implemented a way to shuffle cards but that way made it hard/ | ||
+ | |||
+ | The following 2 arrays are needed: | ||
+ | <code C> | ||
+ | // Values to be assigned to each card | ||
+ | int[52] cardValues = {2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, | ||
+ | 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, | ||
+ | 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, | ||
+ | 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11}; | ||
+ | |||
+ | |||
+ | // All cards before they are randomized | ||
+ | int[52] cardOrder = {1, | ||
+ | | ||
+ | | ||
+ | | ||
+ | </ | ||
+ | |||
+ | Once you have created your arrays you will want to make the following for loop: | ||
+ | <code C> | ||
+ | // Fisher-Yates shuffle algorithm | ||
+ | for (int i = 51; i > 0; i--) | ||
+ | { | ||
+ | // Generate a random index between 0 and i | ||
+ | int j = rand() % (i + 1); | ||
+ | |||
+ | // Swaps cardOrder[i] and cardOrder[j], | ||
+ | int temp = cardOrder[i]; | ||
+ | int temp2 = cardValues[i]; | ||
+ | cardOrder[i] = cardOrder[j]; | ||
+ | cardValues[i] = cardValues[j]; | ||
+ | cardOrder[j] = temp; | ||
+ | cardValues[j] = temp2; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | In this version of the shuffle, we also shuffle the card values making it so they still line up and are assigned correctly. This version does not include suits but you can also do the same thing. Create an array of numbers 0-3. the numbers represent a suit so heart could be 0, diamond as 1, spade as 2, and clubs as 3. | ||
+ | ====Ace Logic==== | ||
One of the unique aspects of Black Jack is that an Ace can either be 11 or 1 depending on what the player decides. Here is an example of a hand involving an Ace. | One of the unique aspects of Black Jack is that an Ace can either be 11 or 1 depending on what the player decides. Here is an example of a hand involving an Ace. | ||
Line 27: | Line 65: | ||
The challenging aspect of this is how would you implement this so the game automatically handles this for the player. | The challenging aspect of this is how would you implement this so the game automatically handles this for the player. | ||
- | To start you need to create two int variables, one to keep track of the current score of the player and one to keep track of how many aces are dealt to the player. | + | To start you need to create two int variables, one to keep track of the current score of the player and one to keep track of how many aces are dealt to the player. Note: Ace is originally set to 11 because starting with the highest value the ace can be is easier and requires less testing. |
<code C> | <code C> | ||
Line 34: | Line 72: | ||
</ | </ | ||
- | Next, inside your button logic for asking for another card from the dealer use the following if statements | + | Next, inside your button logic for asking for another card from the dealer use the following if statements: |
<code C> | <code C> | ||
Line 56: | Line 94: | ||
+ | ====Dealer Strategy==== | ||
+ | The dealer in blackjack always hits until hitting at least 17, and goes after the player does. A simple way of doing this is to out a check for if the player stands or busts towards the end of the loop, and set a flag if they do. | ||
+ | if(stand == true || playerbust == true){} | ||
+ | |||
+ | Next you need a check to see if the dealer' | ||
=====CHANCES===== | =====CHANCES===== | ||
+ | In a standard 52 card deck, there are | ||
+ | *4 aces | ||
+ | *16 cards with the value of 10 | ||
+ | *32 cards with values from 2-9. | ||
+ | |||
+ | The probability of getting Blackjack for the first 2 cards dealt accounts for all the possible combinations of 2 cards and the cases which the 2 cards add to 21. | ||
+ | |||
+ | ===Calculating Bust Probabilities: | ||
+ | |||
+ | To calculate probabilities for a bust, between both the player and the house, you'll need a way to compare against all possible outcomes. You'll need a way to store these potential outcomes that isn't the deck itself. Either an array or list should work, but for this example we'll use an array. | ||
+ | |||
+ | Each slot in the array will represent a drawn card's potential point value. Since Aces can be either 1 or 11, but we're calculating the probability of a bust, we only need to count Aces as 1s. So in total we'll have 10 array slots, and the number stored in each slot is the number of cards with that point value theoretically stored in the deck. | ||
+ | < | ||
+ | |||
+ | When dealing cards, make sure to decrement its point value from the probabilities array. (i.e. you draw the 5 of Spades, decrement '' | ||
+ | |||
+ | To calculate the player' | ||
+ | < | ||
+ | int playerScore; | ||
+ | int bust; // Counting number for the number of bust outcomes. | ||
+ | int total; | ||
+ | </ | ||
+ | |||
+ | You'll need to loop through for each possible value stored in the '' | ||