below is the tutorials for developing the program i had a hard time with it.
Part 1: Blackjack Card Game
Blackjack is a card game played in casinos. The game is also called Twenty-One. Blackjack came originally from France, where it is called Vingt-Et-Un (21).
The goal for both the player and the dealer is to have a point count of less than or equal to 21 that is higher than the opponent. In the traditional casino version of the game, the player makes a bet on his hand. In your simpler version of the game, there is no betting.
The traditional game has multiple players playing against the house, or dealer. In your version of the game, for your Python program, there will be only a single player playing against the dealer (the computer).
In the traditional game, if a player is dealt a hand containing the ace of spades or any black jack, she immediately collects 10-1 on her bet. You won’t do that in your version of the game.
Cards in a hand have this point value:
• Ace = 1 or 11
• 2–10 = the value of the card (that is, between 2 and 10)
• Jack, queen, king = 10
In the beginning of the game, both the dealer and the player are dealt two cards, and the dealer’s top card is turned up for the player to see.
After the deal, it is the player’s turn. The player can elect to have more cards dealt to her, or she can “stand” (that is, keep the cards she was dealt). If the value of the player’s hand exceeds 21, she is “busted,” and the dealer wins.
If the player stops taking cards with a hand value of 21 or less, it is the dealer’s turn. The dealer takes more cards until his hand value is 17 or more. If the value of the dealer’s hand exceeds 21, the dealer is “busted,” and the player wins.
If neither the player nor the dealer is busted, the point value of the two hands is used to determine who wins the hand. The one with the higher point value wins.
The following figure shows the Python IDLE window in the middle of a blackjack game:
Part 2: Blackjack Python Program
Almost any Python program can be written in multiple ways, and this one is no exception. The descriptions below give you an idea of one way you can write your blackjack program. Feel free to substitute techniques of your own invention and choosing.
This Blackjack program uses simplified rules (for example, if a new deck is needed in the middle of a game, the cards currently in play are not removed).
Environment Setup Section
In this section you need to import the random number routines.
Function Definition Section
The program has several defined functions, some of which can be reused from the Shuffle Cards program you did in Lesson 8.
• define_cards(n): This function assigns string values to a list of integer variables representing the cards (52 in all). n is the card to be defined.
The list items range in value from 0 to 51. The value strings include both card rank (for example, seven or queen) and suit (for example, clubs or hearts).
Examples: List item 0 represents the string "ace of clubs". List item 2 represents "three of clubs". List item 51 represents "king of spades".
• create_deck(deck): This function creates a card deck.
The card deck is created as a list of numbers: [0,1,2, …,51].
• shuffle_deck(deck): This function shuffles the card deck.
The function calls the random.shuffle() function to do the shuffle.
The function shuffles its list parameter (argument) in place.
After the call to this function, the deck list is in a random order rather than in strict sequence. For example: [2,45,3,5, …,0].
• deal_cards(deck): This function “deals” a card (that is, turns it over to reveal its face) by returning and removing the first card in the deck list.
• card_display(n). This function returns the content of a card as a string including suit and rank-and-value. (A card is an integer from 0–51.)
This function calls define_cards() and card_value() to create the string it returns to the caller. This makes it possible to reuse define_cards(), which you already have from Shuffle Cards in Lesson 8.
• card_value(n). Function to return the point value of a card.
An ace is 11, and 10, jack, queen, and king are all 10.
• hand_value(hand). This function returns the value of a hand.
You start by calling card_value() for each card in the hand and adding them all up. You also count how many aces are in the hand, since they can be valued as either 1 or 11.
If the hand has a value that exceeds 21, you recalculate aces as 1 instead of 11 until you (if possible) get to a hand value of less than 21.
Tip on how to write this function:
# initialize the total hand value and the number of aces
# add up the value of each of the cards for card in hand:
# call card_value() to get the value of this card
# count the number of aces (value 11) that are in the hand
# add to the total hand value
# if the total hand value is greater than 21, try counting
# any aces as 1 to reduce the value to be below 21
# return the value
• show_cards(hand). This is a function to print out a list of cards by displaying the value and suit for each card.
You do this by calling card_display() for each card in the hand and then displaying the result with three cards in each line of output.
• deal_from_deck(deck). This is a function to deal a card and, if necessary, create and shuffle a new deck.
This function acts as a “wrapper” for several other functions you already have. In addition to just returning the first card in the deck, it also handles the case when you have run out of cards by creating a new deck, shuffling it, and then returning the top card. If you play blackjack long enough, the deck can run out of cards while you are playing the game.
Tip: This function calls create_deck() and shuffle_deck() if it needs to (when the deck is empty) and then returns a card by calling deal_cards().
• print_game_rules(). This function prints out the rules of the game. It is similar in concept to the function of the same name in Hangman from Lesson 9.
Processing Initialization Section
The following variables need to be initialized in the Processing Initialization section of the program:
• A count of the number of drawn hands set to 0
• A count of player wins set to 0
• A count of dealer wins set to 0
The following information applies to the Processing section of the program.
The main code in the Processing section is a while-loop that contains all the code to play one hand of the game.
This section starts by creating a list called deck that is initialized to be empty (that is, ). Two other empty lists, player and dealer, will contain the cards for the player and dealer.
After that, the logic in the while-loop is as follows:
1. Deal two cards each to the player and the dealer. Display the dealer’s top card. Use deal_from_deck() to deal the cards and card_display() to show the dealer’s “up” card.
2. Deal cards to the player until she stops drawing or goes over 21. Use calls to hand_value() and deal_from_deck() to do that. All of this code is inside a nested while-loop.
3. Deal cards to the dealer until his hand value reaches 17 or he goes over 21. Again call hand_value() and deal_from_deck(). This code is also inside a nested while-loop.
4. Compare the value of the hands to see who has won, the player or the dealer. Use if/elif/else for this. (The sample program fortune_cookie.py shows how to use elif, which has not been used before in this course.)
5. Ask the player whether she wants to play another hand.
You deal cards by calling deal_from_deck() and adding the card to the appropriate hand using list.append().
Cleanup, Termination, and Exit Section
The following information applies to the Cleanup, Termination, and Exit section of the program.
At the end of the program, tell the player the game is over, and display the statistics for the hand just played (for example, player total, dealer total, and who won).[/B]
for reference here is my shuffle card program
from random import* rank_suit[U][U]=["ace","two","three","four","five","six","seven","eight","nine","ten","jack","queen","king"][/U][/U] suit_rank=["clubs","spades","heatrts","diamonds"] cards= def define_cards(): for suit in range(4): for rank in range(13): card_string=rank_suit[rank]+ " of "+ suit_rank[suit] cards.append(card_string) return def shuffle_cards(): shuffle(cards) return def deal_cards(): for i in range (0,10): print cards[i] return define_cards() shuffle_cards() deal_cards()
Edited 6 Years Ago by Nick Evan: Added codetags. Moved from C++ to Python