1. Start with the problem statement
" Design a Blackjack Simulator "
2. Express the project in abstract, but solvable, pieces
3. Take each of the pieces and write out the logic necessary to code that pieceFirst, shuffle the cards Second, deal 2 cards to the player and 2 cards to the dealer Third, deal the player's hand until he busts or stays Fourth, if the player didn't lose, deal the dealer's hand until the dealer wins or lose
I assert that this algorithm, if coded correctly,
- Shuffling the Cards
- If we choose a random number between 1 and 11, it will simulate shuffled decks
- Dealing the First Hand
- Pick a random card for the player
- Pick a random card for the dealer
- Pick a random card for the player
- Pick a random card for the dealer
- Total the dealer's hand
- Total the player's hand and tell him what he has
- Dealing the Player's Hand
- Ask the Player if he wants a card
- If the player says yes
- Give the player a card
- Total the player's hand
- If the total is greater than 21, the player LOSES
- Else, go back to "Ask the Player if he wants a card"
- If the player says no, you're finished with this step
- Dealing the Dealer's Hand
- If the Dealer's total is greater than the Player's total, the player LOSES
- Otherwise, Give the Dealer a Card
- Total the Dealer's Hand
- If the Dealer's total is greater than 21, the player WINS
- Else, go back to "If the dealer's total is greater than the player's total..."
4. Take this algorithm and write pseudocode for whatever language
you are coding for
5. Finally, Sit down at the computer with your design,
- Shuffling the Cards
- Card = 1+(rand() %11) /* Will give a number between 1 and 11 inclusive */
- Dealing the First Hand
- for( i= 1 to 2)
- { Dealer_Card = 1+(rand() %11)
- Dealer_total += Dealer_Card
- Player_ Card = 1+(rand() % 11)
- Player_total += Player_Card
- }
- etc. etc. etc.
And release it to the world
(To see a working version of the BlackJack code based on this design,
click Here)