views:

1214

answers:

7

I am in the process of writing a blackjack code for python, and i was hoping someone would be able to tell me how to make it:

  1. Recognize what someone has typed i.e. "Hit" or "Stand" and react accordingly.
  2. Calculate what the player's score is and whether it is an ace and a jack together, and automatically wins.

Ok, this is what i have gotten so far.

"This imports the random object into Python, it allows it to generate random numbers."
import random
print("Hello and welcome to Sam's Black Jack!")
input("Press <ENTER> to begin.")
card1name = 1
card2name = 1
card3name = 1
card4name = 1
card5name = 1

"This defines the values of the character cards."
Ace = 1
Jack = 10
Queen = 10
King = 10

decision = 0

"This generates the cards that are in your hand and the dealer's hand to begin with.
card1 = int(random.randrange(12) + 1)
card2 = int(random.randrange(12) + 1)
card3 = int(random.randrange(12) + 1)
card4 = int(random.randrange(12) + 1)
card5 = int(random.randrange(12) + 1)

total1 = card1 + card2

"This makes the value of the Ace equal 11 if the total of your cards is under 21"
if total1 <= 21:
    Ace = 11

"This defines what the cards are"
if card1 == 11:
    card1 = 10
    card1name = "Jack"
if card1 == 12:
    card1 = 10
    card1name = "Queen"
if card1 == 13:
    card1 = 10
    card1name = "King"
if card1 == 1:
    card1 = Ace
    card1name = "Ace"

elif card1:
    card1name = card1

if card2 == 11:
    card2 = 10
    card2name = "Jack"
if card2 == 12:
    card2 = 10
    card2name = "Queen"
if card2 == 13:
    card2 = 10
    card2name = "King"
if card2 == 1:
    card2 = Ace
    card2name = "Ace"

elif card2:
    card2name = card2

if card3 == 11:
    card3 = 10
    card3name = "Jack"
if card3 == 12:
    card3 = 10
    card3name = "Queen"
if card3 == 13:
    card3 = 10
    card3name= "King"
if card3 == 1:
    card3 = Ace
    card3name = "Ace"

elif card3:
    card3name = card3

if card4 == 11:
    card4 = 10
    card4name = "Jack"
if card4 == 12:
    card4 = 10
    card4name = "Queen"
if card4 == 13:
    card4 = 10
    card4name = "King"
if card4 == 1:
    card4 = Ace
    card4name = "Ace"

elif card4:
    card4name = card4

if card5 == 11:
    card5 = 10
    card5name = "Jack"
if card5 == 12:
    card5 = 10
    card5name = "Queen"
if card5 == 13:
    card5 = 10
    card5name = "King"
if card5 == 1:
    card5 = Ace
    card5name = "Ace"

elif card5:
    card5name = card5
"This creates the totals of your hand"
total2 = card1 + card2
total3 = card1 + card2 + card3

print("You hand is ", card1name," and", card2name)
print("The total of your hand is", total2)
decision = input("Do you want to HIT or STAND?").lower()

"This is the decision for Hit or Stand"
if 'hit' or 'HIT' or 'Hit' in decision:
    decision = 1
    print("You have selected HIT")
    print("Your hand is ", card1name,",",card2name," and", card3name)
    print("The total of your hand is", total3)

if 'STAND' or 'stand' or 'Stand' in decision:
    print("You have selected STAND")

"Dealer's Hand"
dealer = card4 + card5
print()
print("The dealer's hand is", card4name," and", card5name)

if decision == 1 and dealer < total3:
    print("Congratulations, you beat the dealer!")

if decision == 1 and dealer > total3:
    print("Too bad, the dealer beat you!")

Ok, nevermind, i fixed it :D

I just changed the Hit and Stand to Yes or No

if total2 < 21:
    decision = input("Do you want to hit? (Yes or No)")

    "This is the decision for Hit or Stand"
    if  decision == 'Yes':
        print("You have selected HIT")
        print("Your hand is ", card1name,",",card2name," and", card3name)
        print("The total of your hand is", total3)

    if decision == 'No':
            print("You have selected STAND")
+13  A: 

This can get you started:

http://docs.python.org/library/random.html

http://docs.python.org/library/strings.html

http://docs.python.org/library/stdtypes.html

http://docs.python.org/reference/index.html

I see you have added some code; that's good.

Think about the parts of your program that will need to exist. You will need some representation of "cards" -- cards have important features such as their value, their suit, etc. Given a card, you should be able to tell what its value is, whether it's a Jack or an Ace or a 2 of hearts. Read up on "classes" in Python to get started with this.

You will also have a hand of cards -- the cards your dealer is currently holding, and the cards your player is currently holding. A "hand" is a collection of cards, which you (the programmer) can add new cards to (when a card is dealt). You might want to do that using "lists" or "arrays" or "classes" that contain those arrays. A hand also has a value, which is usually the sum of card values, but as you know, Aces are special (they can be 1 or 11), so you'll need to treat that case correctly with some "if statements".

You will also have a deck; a deck is a special collection -- it has exactly 52 cards when it starts, and none of the cards are repeated (you could, of course, be using several decks to play, but that's a complication you can solve later). How do you populate a deck like that? Your program will want to "deal" from the deck -- so you'll need a way to keep track of which cards have been dealt to players.

That's a lot of stuff. Try writing down all the logic of what your program needs to do in simple sentences, without worrying about Python. This is called "pseudo-code". It's not a real program, it's just a plan for what exactly you are going to do -- it's useful the way a map is useful. If you are going to a place you've been to a 100 times, you don't need a map, but if you are driving to some town you've never been to, you want to plan out your route first, before getting behind the wheel..

Update your question with your pseudocode, and any attempts you have made (or will have made) to translate the pseudocode to Python.

SquareCog
Wow, someone actually downvoted that answer. I happen to think basic language references are extremely relevant to the question; if the OP can't understand them, he's welcome to post questions about the specifics.
SquareCog
Thanks for the answer, but i am not looking to use suits, as this is just my first program for the class, i am just looking to use the cards with only 4 of each.Thanks :D
Sam Bristow
cool. Don't include suits in your pseudocode. You still need pseudocode in order to organize the way you are thinking about the problem.
SquareCog
And you said _I_ was patient and nurturing. If only I could upvote more than once...
Blair Conrad
Hey, I pretty much told the OP to drop his class, so I don't know how you can call me nurturing after that! Unless it's "tough love"..
SquareCog
Cog: sometimes that's the right suggestion... If someone (not necessarily indicating the OP here) just can't get it, despite wanting to badly enough, sometimes its best to cut ones losses and find a different path.
Erik Forbes
+4  A: 

I agreed with SquareCog's comment - some additional information about what you've tried and what's not working and what you're confused about would be helpful.

Some info that might be helpful, though:

Regarding the generation of numbers between 1-10, ace, king, queen, and jack: it might be helpful to assign each card a numeric index. 2-10 are obvious, and you can make your own values for jack, queen, king, and ace. One thing to especially note is that there's no 1, if you're also generating ace. Once you've assigned numbers, the random module can help.

There are standard comparison methods that can be used to recognize the difference between "Hit" or "Stand". Notably the == operator.

Since you're generating hands, it should be easy to check whether they are certain combinations. As far as calculating scores, a good starting point would be to use addition.

Edit: based on your subsequent comments, it seems like you might benefit from some of the "Python for non-programmer" guides.

Blair Conrad
Hi, thanks for your answer, but im really having trouble with the "Hit" and "Stand" stuff, i just can't seem to find anything that would help, can you please explain some more, or link me to somewhere?Thanks.
Sam Bristow
Many types in python use the '==' operator to test for equality, and you could perform different actions via the if statement: http://docs.python.org/tutorial/controlflow.html#if-statements
Blair Conrad
you are a patient and nurturing person, sir. Upvoted :-).
SquareCog
Please, you'll make me blush.
Blair Conrad
Lol, thanks guys, i will try this out and post my code for you <3
Sam Bristow
A: 

do your own computer science homework :-)

http://stackoverflow.com/questions/230510/homework-on-stackoverflow
John Fouhy
+8  A: 

I've covered the entire thing as a (long) series of exercises.

http://homepage.mac.com/s_lott/books/oodesign.html

Just read and do the exercises in the book. You'll implement blackjack (and Craps and Roulette, too.)

S.Lott
very impressive!
Learning
+1 - Neat stuff. :)
Paolo Bergantino
+3  A: 

Hi Sam,

In your code, you wrote:

Ace = 1 or 11

I'm afraid this doesn't do what you think. Start the python interpreter, and then type 1 or 11 into it. Here's what I get:

>>> 1 or 11
1

What this means is that when you type: Ace = 1 or 11, python first evaluates the 1 or 11 bit, and then it sets Ace to be that. In other words, your code is equivalent to:

Ace = 1

I suggest you forget about the two possible values for an Ace; just leave it as 1 only. Simplify the rules, get something working, and then you can look at making a full blackjack game.

John Fouhy
A: 

Here's a tip to get started: Instead of drawing a number between 1 and 10 and doing something different for face cards, draw a number between 1 and 13, and define a function (or even a class) that interprets these numbers as cards. For example, 1 would map to Ace, 11 to Jack, 2 to a deuce, etc.

ruds
A: 

Check all of your inputs. As a young lad, I wrote my blackjack program in Fortran. One of my users pointed out that he could:

  1. Enter a negative number as a bet.
  2. Deliberately lose.

The program would then do exactly what it was programmed to do:

  1. Subtract the negative bet from the total won/lost.
  2. Which is (of course) equivalent to adding a positive number to the total.

Sometimes, you can win by losing....

mkClark