2

I am trying to write a method for a class which will create a new instance of an already existing instance of a class. The problem is that I cannot access the new instance in the console when I try new_handname.

This is for creating a blackjack game in python. The idea of the code is when the hand is split a new instance will be created to create a new hand

import random


class Card(object):
    def __init__(self, value, suit,nvalue):
        self.value = value
        self.suit = suit
        self.nvalue = nvalue

suit = ['Hearts','Spades','Clubs','Diamonds']
value = ['2','3','4','5','6','7','8','9','10','J','Q','K','A']
nvalue = [2,3,4,5,6,7,8,9,10,10,10,10,11]


class Hand(object):
    def __init__(self,current_hand):
        self.current_hand = current_hand

    def hand_total(self):
        current_sum = 0
        for i in range(0,len(self.current_hand)):
            current_sum += self.current_hand[i].nvalue
        return current_sum

    def hand_type(self):
        if self.current_hand[0].value == self.current_hand[1].value:
            return('pair')
        elif self.current_hand[0].value == 'A' or self.current_hand[1].value == 'A':
            return('soft')
        else:
            return('hard')

    def append(self,current_hand,some_card):
        self.current_hand = self.current_hand + some_card

    def hit(self):
        self.current_hand.append(deck[0])
        deck.pop(0)

    def double(self,new_handname):  
        new_handname = Hand(self)


def deal_start_hand():
    player_hand.append(deck[0])
    deck.pop(0)
    dealer_hand.append(deck[0])
    deck.pop(0)
    player_hand.append(deck[0]) #### player gets two cards ### assuming europe no hole card rules
    deck.pop(0)

def gen_deck():
    for v,n in zip(value,nvalue):
        for s in suit:
            deck.append(Card(v,s,n))


### variable initiation ###

deck = []
player_hand = []
dealer_hand = []


##program start ##

gen_deck()
random.shuffle(deck)
deal_start_hand()

p1 = Hand(player_hand)
p1.double('p2')
p2   ### I expect p2 to return an instance but does not 

>>> p1 
<__main__.Hand object at 0x00000006A80F0898>
>>> p2
Traceback (most recent call last):
  File "<pyshell#182>", line 1, in <module>
    p2
NameError: name 'p2' is not defined

Note: current_hand is a list of the cards objects.

I expect p2 to return an instance of the class but instead the variable p2 is not defined

8
  • 3
    return new_handname if you want to return the value. But you do have other problems in your code as well... Commented Dec 31, 2018 at 0:12
  • 1
    You haven't created a single instance of either class Commented Dec 31, 2018 at 0:13
  • self.new_handname = Hand(self) I think might help. Commented Dec 31, 2018 at 0:15
  • where are you creating an instance? can you provide an example of how you're creating the instances to make your question more clear. Provide some sort of example please Commented Dec 31, 2018 at 0:15
  • Also, I'm guessing that this may be a port of something from another programming language (C++, Java, etc...) Commented Dec 31, 2018 at 0:17

1 Answer 1

1

Your split routine could look like the following, in which a new instance of the class is returned:

class Hand(object):
    def __init__(self, current_hand):
        self.current_hand = current_hand

    def split(self):
        return Hand(self.current_hand)

Simply create an instance, then split it later:

# You need to define "some_default" somewhere
myhand = Hand(some_default)
second_hand = myhand.split()

However, your split routine needs to take into account what cards have already been played, and what cards are still in the deck(s), none of which your code considers. I might advise mapping out the "states" of the game (think of it as a state machine), draw that out on paper, then consider how to code up each state and transition. A card game like this is more complicated to code than it might seem at first glance.

Sign up to request clarification or add additional context in comments.

1 Comment

I know the split function is not well defined but I am trying to create an instance within a class's method then incorporate the correct game logic of splitting in blackjack.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.