user3581970
user3581970

Reputation: 29

War Card Game for python

I'm trying to make a war card game, but I am having difficulties getting my code to connect. I keep getting the error that deck1 isn't defined. I cannot see why this is happening. I am trying to connect the deck1 and deck2 to the playerA=deck1.pop and so forth. Thanks for the help!

import random
total = {
   'winA':0,
   'winB':0
}

def shuffleDeck():
    suits = {'\u2660', '\u2661', '\u2662', '\u2663'}
    ranks = {'2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'}
    deck = []

for suit in suits:
    for rank in ranks:
        deck.append(rank+' '+suit)

random.shuffle(deck)
return deck

def dealDecks(deck):
    deck1 = deck[:26]
    deck2= deck[26:]
    hand = []
    return hand

def total(hand):
    values = {'2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, '1':10,
          'J':11, 'Q':12, 'K':13,'A':14}

def war(playerA, playerB):
    if playerA==playerB:
        print("Tie")
    elif playerA > playerB:
        print("Winner:Player A")
        return 1
    else:
        print("Winner:player B")
        return -1

def process_game(playerA,playerB):
    result = game(p1c,p2c)
    if result == -1:
        total['winB'] += 1
    else:
        total['winA'] += 1

deck = shuffleDeck()

dealDecks(deck);

gameplay = input("Ready to play a round: ")

while gameplay == 'y':

    playerA = deck1.pop(random.choice(deck1))
    playerB = deck2.pop(random.choice(deck2))
    print("Player A: {}. \nPlayer B: {}. \n".format(playerA,playerB))
    gameplay = input("Ready to play a round: ")

if total['winA'] > total['winB']:
    print("PlayerA won overall with a total of {} wins".format(total['winA']))
else:
    print("PlayerB won overall with a total of {} wins".format(total['winB']))

Upvotes: 1

Views: 2538

Answers (2)

jonrsharpe
jonrsharpe

Reputation: 122106

Currently, dealDecks doesn't really do what it says it does. Why does it create and return an empty list:

def dealDecks(deck):
    deck1 = deck[:26]
    deck2= deck[26:]
    hand = []
    return hand

that is then ignored:

dealDecks(deck);

Thus deck1 is inaccessible anywhere outside dealDecks. Instead, actually return and assign the two halves of the deck:

def split_deck(deck):
    middle = len(deck) // 2
    deck1 = deck[:middle]
    deck2 = deck[middle:]
    return deck1, deck2

deck1, deck2 = split_deck(deck)

Note that I've factored out the "magic number", renamed the function to describe what it does and adopted lowercase_with_underscores per the Python style guide (PEP-0008).

Upvotes: 1

BeetDemGuise
BeetDemGuise

Reputation: 974

The problem is Python creates variables on demand. Because of this, in your dealDecks function, instead of referencing the global variables deck1 and deck2 it instead creates two local variables of the same name.

Thus, when you try to pop off of the global deck1 it errors because it never was defined.

To fix this, you COULD use the global key word in dealDecks:

def dealDecks():
    global deck1
    global deck2

However, this is not good practice. You should only use global if its absolutely necessary. Typically good classes and program structure removes the need for global.

Upvotes: 0

Related Questions