mercredi 29 juillet 2020

dealing cards to players and updating dictionary with player:hand

I am trying to create a deck of cards, randomly shuffle it, and deal 3 cards to 3 players. I created a function buildDeck() that returns the 48 needed cards in deck as a list. I then tried to make a dictionary playerHands with key, value {playername: [3 random cards from deck]}. I then def pullCards() which shuffles the deck, checks that there are atleast 9 cards in the deck, and then draws 3 random cards from the deck and removing them using pop(). I would now like to assign the 3 randomly drawn cards as a value for each player key in the dict playerHand.

I am new to programming and don't really know if this was a good setup for this program nor do I know if this use of list in lists and dictionaries are appropriate for this.

I am not supposed to use classes for this program and I also don't even know how! Really would appreciate some help! Thanks

import random

def printMenu():
    #prints menu of options
    menu = "s: start new game\np: Pull cards for all players\no: output deck\nh: output players’ hand\ne: exchange one card\nd: declare winner\nq: quit\nSelect an option:\n"
    print(menu)
    return
printMenu()

def buildDeck():
    suits = ['Clubs', 'Golds', 'Cups', 'Swords']
    values = list(range(1, 13))
    deck = []
    #iterates through elements in suits and elements in values appending each respective element as a new element into deck
    for i in suits:
        for x in values:
            deck.append(list([i, x]))
    return deck


def startNewGame():
    #prompts user for player names
    player1 = input("Enter player 1's name:\n")
    player2 = input("Enter player 2's name:\n")
    player3 = input("Enter player 3's name:\n")
    
    #calls 48 card deck using buildDeck() function 
    buildDeck()
    
    #creates dictionary of each player's empty hand with values of None
    playerHand = {player: None for player in [player1, player2, player3]}
    return playerHand

    
def pullCards():
    playerHand = startNewGame()
    deck = buildDeck()
    #randomly shuffles deck
    random.shuffle(deck)
    #iterates through cards in the deck and checks if at least 9 cards
    for cards in deck:
        if deck.count(cards) >= 9:
            #draws 3 random cards and pop() removes cards from the deck
            for i in range(3):
                randomCard = deck.pop(random.randint(0, len(deck) - 1))
                for keys in playerHand.keys():
                    playerHand[keys] = randomCard



Aucun commentaire:

Enregistrer un commentaire