Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a multiplayer blackjack game

I'm very new to python and have been trying to make a multiplayer blackjack game on python for a while now. I've been running into lots and lots of problems and was wondering if you guys could help me with them.

import random

def total(hand):
    aces = hand.count(11)
    t = sum(hand)
    if t > 21 and aces > 0:
        while  aces > 0 and t > 21:
            t -= 10
            aces -= 1
    return t

Cards = ["2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "10C", "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "10S", "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "10D", "AH", "JH", "QH", "KH", "AC", "JC", "QC", "KC", "AS", "JS", "QS", "KS", "AD", "JD", "QD", "KD"]

Cards[35] = 11
Cards[36] = 10
Cards[37] = 10
Cards[38] = 10
Cards[39] = 11
Cards[40] = 10
Cards[41] = 10
Cards[42] = 10
Cards[43] = 11
Cards[44] = 10
Cards[45] = 10
Cards[46] = 10
Cards[47] = 11
Cards[48] = 10
Cards[49] = 10
Cards[50] = 10

Players = raw_input("How many players are there?")
for i in range Players:
    Player i = []
    Player i.append(choice(Cards))
    Player i.append(choice(Cards))
    tp = total(player)
    print "Player" + i + "Cards: " + Player i + "," + "total: " + tp
    hitorstand = raw_input("hit (h) or stand (s)?")
    if hitorstand == "h":
        Player i.append(choice(cards))
        print ("hit (h) or stand (s)?")
    elif hitorstand == "s":
        break
    else print "Please enter h or s"

dealer = []
While True:
    dealer.append(choice(cards))
    dealer.append(choice(cards))
    td = total(dealer)
    while td > 17:
        dealer.append(choice(cards))
    else:
        break
if td < tp < 21:
    "Player i wins"
else print "dealer wins"

This is what I have so far. I understand there are lots of gibberish and code that won't work. I was wondering if you guys can let me know what is wrong with the code and maybe suggest some options on how to fix it.

My main concerns right now:

  1. I am making a "multiplayer" blackjack game. I have no idea how I'm supposed to make a loop for a multiplayer blackjack game. In my code, I asked how many people are playing. How do I make a loop for the game without knowing what the number will be?

    Also, how do I create a function to find out the winner without knowing how many players are playing?

  2. After I type in

    Players = raw_input("How many players are there?") 
    for i in range Players:
    

    The Players in the for loop gives me a syntax error. What is wrong?

As an update, I've thought about what you said about making a list and I still do not really understand how I should go about making a code to find out the winner.

for example

even if I make a list, if i do not know how many players are actually playing, I wouldn't be able to compare the elements in the list. If I knew how many people were playing,

playerlist = [1,2,3]

I can say

if playerlist[0] > playerlist[1], playerlist[2] and playerlist[0] < 21:
    then print "player 1 wins!"

But since I won't know how many people are playing until the user actually types in the input, I am lost as to how I am supposed to write the code for the winner.

I do not know if there is a way of saying "if this is bigger than the rest". I only know how to say "if this is bigger than that".

Is there a way of saying "if this is bigger than the rest" in python? If not, can you give me some suggestions to making the code to find out the winner?

like image 834
user1561751 Avatar asked Nov 04 '22 19:11

user1561751


1 Answers

  1. You're on the right track with your "Players" variable. If you store an int representing the number of players there, then any time you want to do something that should require knowing the number of players, just use the Players variable instead. For instance, you can write a for loop that iterates that number of times (like you've already almost got). In order to iterate over the players, you'll need to place them in some sort of data structure (probably a list).

    Once you know the number of players, you can set up the list like this:

    playerList = []
    for i in range(Players):
        playerList[i] = [whateverDataYouWantToStoreAboutEachPlayer]
    

    After that, you can access each player in a for loop by referring to playerList[i]. That way, you can do things for each player without knowing the number of players ahead of time.

  2. When you use raw_input, the input is stored as a string. To use it as an int (which is the way you want to use it), you need to convert it first. You can do this with

    Players = int(Players)
    

    although it would be safer to first make sure that the number the user entered is actually a number, as in

    while not Players.isdigit():
       Players = raw_input("Please enter an integer: ")
    Players = int(Players)
    

    Also, as minitech said (and as shown above), you need to iterate over range(Players), rather than just Players, as that will create a list of numbers from 0 to Players-1, and you can only iterate over data that is in some sort of a sequence format.

EDIT (to answer follow-up question): You can find the largest value in the list by iterating over the whole thing and keeping track of the largest value you see:

    highestIndex = 0
    for i in range(Players):
        if PlayerList[i] > playerList[highestIndex]:
            highestIndex = i

At the end, highestIndex will hold the index in the list of the player with the highest score.

Python also has a built-in function, max(), which will give you the maximum value in a list. It won't give you the location of that value, though, so it would be more cumbersome to use in this case.

like image 109
seaotternerd Avatar answered Nov 13 '22 14:11

seaotternerd