continued trouble with classes in python 3.2 -
in below code, working @ first when want tell me card number dealer has returning sort encrypted stuff don't understand.
[<__main__.card object @ 0x025a4e50>]
how return corresponding card number?
also said may need use __eq__
when comparing things opposed way explain why, , how use __eq__
?
below code program trying work, blackjack game.
from random import* class card(object): def __init__(self,suit,number): self.suit=suit self.number=number class deckofcards(object): def __init__(self,deck): self.deck=deck self.shuffledeck=self.shuffle() #print(self.shuffledeck) def shuffle(self): #print('this shuffle function') b=[] count=0 while count<len(self.deck): a=randrange(0,len(self.deck)) if not in b: b.append(self.deck[a]) count+=1 return(b) def deal(self): if len(self.shuffledeck)>0: return(self.shuffledeck.pop(0)) else: shuffle(self) return(self.shuffledeck.pop(0)) class player(object): def __init__(self,name,hand,inout,money,score,bid): self.name=name self.hand=hand self.inout=inout self.money=money self.score=score self.bid=bid def __str__(self): x = self.name + ":\t" x += "card(s):" y in range(len(self.hand)): x +=self.hand[y].face + self.hand[y].suit + " " if (self.name != "dealer"): x += "\t money: $" + str(self.money) return(x) class game(object): def __init__(self,deck, player): self.player=player(player,[],true,100,0,0) self.dealer=player("dealer",[],true,100,0,0) self.deck=deckofcards(deck) self.blackjack= false #self.blackjacksearch() def blackjacksearch(self):#this says there error moved because said needed in class getot function if self.player.hand.gettot()==21: return true else: return false def firstround(self): self.player.inout=true self.player.hand=[] self.dealer.hand=[] self.dealer.hand.append(deckofcards.deal(self.deck)) print('the dealer has '+str(self.dealer.hand)) playerbid=int(input('how bet?')) self.player.bid=playerbid def playturn(self): while self.player.blackjack!=true or hit=='yes': print(self.player.hand) a=self.player.hand.append(deal()) print('the card drew ' + str(a)) print(gettot()) hit=input('would hit? ') if hit=='yes': return(self.player.hand.append(deal())) else: return() #might need change if self.player.blackjack==true: print(self.player.name + " has blackjack ") if hit=='no': print (self.player.hand.gettot()) def playdealer(self): while self.dealer.hand<17: self.dealer.hand.append(deal()) dealerhand=self.dealer.hand.gettot() #confused print(dealerhand) if self.dealer.hand==21: self.dealer.blackhjack=true dealerhand1=self.dealer.hand.gettot() print(dealerhand1) def gettot(self,hand): total=0 x in self.hand: if x==card('h','a'): b=total+x if b>21: total+=1 else: total+=11 if x==card('d','a'): b=total+x if b>21: total+=1 else: total+=11 if x==card('s','a'): b=total+x if b>21: total+=1 else: total+=11 if x==card('c','a'): if b>21: total+=1 else: total+=11 else: total+=x return(total) def playgame(self): play = "yes" while (play.lower() == "yes"): self.firstround() self.playturn() if self.player.blackjack == true: print(self.player.name + " got blackjack! ") self.player.money += self.player.bid * 1.5 print (self.player.name + " has " + str(self.player.money)) print("\n") self.player.inout = false if self.player.score > 21: print(self.player.name + " lost tot of " + str(self.player.score)) self.player.money -= self.player.bid print (self.player.name + " has " + str(self.player.money)) print ("\n\n") self.player.inout = false self.playdealer() if self.dealer.blackjack == true: print("dealer got blackjack, dealer wins\n") self.player.money -= self.player.bid print("round\n") print("\t",self.dealer) print("\t",self.player) print("\t dealer has " + str(self.dealer.score) + ", " + self.player.name + " has " + str(self.player.score)) elif self.player.inout == true: print("round\n") print("\t",self.dealer) print("\t",self.player) print("\n\t dealer has " + str(self.dealer.score) + ", " + self.player.name + " has " + str(self.player.score)) if self.dealer.score > 21: print("\t dealer lost total of " + str(self.dealer.score)) self.player.money += self.player.bid print(self.player.name + " has " + str(self.player.money)) elif self.player.score > self.dealer.score: print("\t" +self.player.name + " won total of " + str(self.player.score)) self.player.money += self.player.bid print("\t"+self.player.name + " has " + str(self.player.money)) else: print("\t dealer won total of " + str(self.dealer.score)) self.player.money -= self.player.bid print("\t"+self.player.name + " has " + str(self.player.money)) else: print("round") print("\t",self.dealer) print("\t",self.player) if self.player.blackjack == false: print("\t "+ self.player.name + " lost" ) else: print("\t "+self.player.name + " won!") if self.player.money <= 0: print(self.player.name + " out of money - out of game ") play = "no" else: play = input("\nanother round? ") print("\n\n") print("\ngame over. ") print(self.player.name + " ended " + str(self.player.money) + " dollars.\n") print("thanks playing. come soon!") ls= [card('h','a'),card('h','2'),card('h','3'),card('h','4'),card('h','5'),card('h','6'),card('h','7'),card('h','8'),card('h','9'),card('h','10'), card('h','j'),card('h','q'),card('h','k'), card('s','a'),card('s','2'),card('s','3'),card('s','4'),card('s','5'), card('s','6'),card('s','7'),card('s','8'),card('s','9'),card('s','10'), card('s','j'),card('s','q'),card('s','k'), card('c','a'),card('c','2'),card('c','3'),card('c','4'),card('c','5'), card('c','6'),card('c','7'),card('c','8'),card('c','9'),card('c','10'), card('c','j'),card('c','q'),card('c','k'), card('d','a'),card('d','2'),card('d','3'),card('d','4'),card('d','5'), card('d','6'),card('d','7'),card('d','8'),card('d','9'),card('d','10'), card('d','j'),card('d','q'),card('d','k')] '''tom=card('heart','queen') print(tom.suit) print(deckofcards(ls)) print(ls.suit)''' def main(): x = input("player's name? ") blackjack = game(ls,x) blackjack.playgame() main()
print card instead of list of card objects , give card decent __str__
method display human-readable. instead of
print(self.player.hand)
try this:
for card in self.player.hand: print(card)
and in class card
insert this:
def __str__(self): return '%s%s' % (self.number, self.suit)
concerning question __eq__
: objects __eq__()
method gets called whenever gets compared equality using ==
operator. without such method, 2 objects equal if same object. in cards might want have sth this:
def __eq__(self, other): return self.number == other.number , self.suit == other.suit
Comments
Post a Comment