Python_Learner
Python_Learner

Reputation: 29

How to create a new instance using a class method

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

Upvotes: 2

Views: 264

Answers (1)

Jonah Bishop
Jonah Bishop

Reputation: 12571

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.

Upvotes: 1

Related Questions