python - Updating cards in deck loop issue -
after playing round, cards removed, if play again, (if click yes), goes 52 cards again. didn't remove cards has been removed in previous game.i think loop issue because calling mainfuction in end , goes first stage , ignoring cards been deleted before want ask if of can guide me or me in updating cards in deck if yes click thank alot
below code:
import random, sys # define global variables cards suits = ('clubs', 'spades', 'hearts', 'diamonds') pip = ('ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'jack', 'queen', 'king') pipvalues = {'ace':11, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, '10':10, 'jack':10, 'queen':10, 'king':10} class card: #when create instance of card, pass suit ("c", "d", "h", or "s") , "pip" (2, 3, 4, 5, 6, 7, 8, 9, 10 j, q, k, a). #these values should stored instance variables new card object. #i recommend have instance variable "value" stores point value of card. #it should have __str__ method lets print individual card (such "3s" 3 of spades). #note __str__ method decks should take advantage of __str__ method cards. def __init__(self, suit, pip): self.suit = suit self.pip = pip self.value = pipvalues.get(pip) def __str__(self): thiscard = "" thiscard += str(self.pip) thiscard += str(self.suit) return (thiscard) def __repr__(self): thiscard = "" thiscard += str(self.pip) thiscard += str(self.suit) return (thiscard) class player: #when create instance of player, should have instance variable "hand" that's set empty list, #and instance variable "handtotal" that's set zero. these instance variables modified #by deck's "dealone()" method. #it should have __str__ method lets print out player's hand. def __init__(self): self.hand = [] self.handtotal = 0 def __str__(self): printhand = "" in self.hand: printhand += str(i) + " " return (printhand) def __repr__(self): printhand = "" in self.hand: printhand += str(i) + " " return (printhand) class deck: #when create instance of deck, should add 52 card objects instance variable "cardlist". #it should have shuffle() method rearranges cards in cardlist. can importing #"random" package python, , using random.shuffle() method. random.shuffle(mylist) rearranges #elements of list "mylist" random order. #it should have dealone() method removes first card deck's cardlist, , appends #hand of specified player. #it should have __str__ method lets print out entire deck debugging purposes. def __init__(self): self.cardlist = [] #for each suit take every card in range(len(suits)): j in range(len(pip)): self.cardlist.append(card(suits[i], pip[j])) def shuffle(self): random. shuffle (self.cardlist) def dealone(self, player): (player.hand).append(self.cardlist[0]) player.handtotal += self.cardlist[0].value del self.cardlist[0] print self.cardlist return self.cardlist def __str__(self): printstring = "" in range(len(self.cardlist)): if % 13 == 0: printstring += "\n \t" printstring += str(self.cardlist[i]) + " " else: printstring += str(self.cardlist[i]) + " " printstring += "\n" return printstring def showhands(player, opponent): print ('dealer shows ' + str(opponent.hand[0]) + ' faceup') print ('you show ' + str(player.hand[0]) +str(player.hand[0] )) def playerturn(deck, player, other): #first, check scores see if either player has blackjack: if player.handtotal == 21 or other.handtotal == 21: if other.handtotal == 21: print ('you hold ' + str(player) + 'for total of ' + str(player.handtotal)) print ("dealer has " + str(other) + "for total of 21") print ("dealer has blackjack! dealer wins!") print ("thanks playing. come again soon! ") message() else: print ("you hold " + str(player) + "for total of 21") print ("you have blackjack! win!") print ("thanks playing. come again soon! ") message() hitorstand = 0 aces = false #if 2 aces if player.hand[0].pip == "a" , player.hand[1].pip == "a": player.hand[0].pipvalue = 1 if player.handtotal == 21: print ('you hold ' + str(player) + 'for total of ' + str(player.handtotal)) print ("blackjack! win!") print ("thanks playing. come soon!") print() message() while hitorstand != 2: #check aces in player.hand: if i.pip == "a" , i.value == 11: aces = true print ('you hold ' + str(player) + 'for total of ' + str(player.handtotal)) print() hitorstand = input('do hit or stand? enter "1" hit , "2" stand: ') while hitorstand != 1 , hitorstand != 2: try: hitorstand = int(hitorstand) break except valueerror: print ("enter valid integer \n") hitorstand = input('do hit hit or stand? enter "1" hit , "2" stand: ') print() if hitorstand == 1: print('card dealt: ' + str(deck.cardlist[0])) print() deck.dealone(player) #check if ace drawn in player.hand: if i.pip == "a" , i.value == 11: aces = true if player.handtotal == 21: print ('you hold ' + str(player) + 'for total of ' + str(player.handtotal)) print ("blackjack!! win!") print() print ("thanks playing. come soon!") message() if player.handtotal > 21: #check aces if aces: print ('you hold ' + str(player) + 'for total of ' + str(player.handtotal)) print ("over 21. value of ace changed 1") #chanlge value of ace , hand player.handtotal = player.handtotal - 10 in player.hand: if i.pip == "a" , i.value == 11: i.value = 1 #check other standard aces aces = false in player.hand: if i.pip == "a" , i.value == 11: aces = true else: print ('you hold ' + str(player) + 'for total of ' + str(player.handtotal)) print ("you bust! dealer wins!") #exit, since you're loser print ("thanks playing! come soon!") print() raise systemexit if hitorstand == 2: print ('you stand at: ' + str(player.handtotal)) print() print ("now dealer's turn") print () def message(): again = raw_input("do want play again? (y/n) : ") if(again == "y" or again == "y"): main() else: print "\n\n-------thank playing!--------\n\n" exit() def opponentturn(deck, player, other): if other.handtotal == 21: raise systemexit aces = false hitorstand = 0 #if 2 aces if player.hand[0].pip == "a" , player.hand[1].pip == "a": player.hand[0].pipvalue = 1 while hitorstand != 2: #check aces in player.hand: if i.pip == "a" , i.value == 11: aces = true print ('dealer holds ' + str(player) + 'for total of ' + str(player.handtotal)) print() #if blackjack if player.handtotal == 21: print ("dealer has blackjack! dealer wins!") break if player.handtotal <21 , other.handtotal == 21: print ("dealer's hand " + str(player.handtotal) + ". have blackjack! congratulations! win! ") break if player.handtotal < other.handtotal: hitorstand = 1 if player.handtotal >= other.handtotal: hitorstand = 2 if hitorstand == 1: print("dealer hits. " + 'card dealt: ' + str(deck.cardlist[0])) deck.dealone(player) #check if ace drawn in player.hand: if i.pip == "a" , i.value == 11: aces = true if player.handtotal > 21: #check aces if aces: print ('dealer holds ' + str(player) + 'for total of ' + str(player.handtotal)) print ("over 21. value of ace changed 1") #chanlge value of ace , hand player.handtotal = player.handtotal - 10 in player.hand: if i.pip == "a" , i.value == 11: i.value = 1 #check other standard aces aces = false in player.hand: if i.pip == "a" , i.value == 11: aces = true else: print ('dealer holds ' + str(player) + 'for total of ' + str(player.handtotal)) print ("dealer busts! win!") message() if hitorstand == 2: print ("dealer stands @ " + str(player.handtotal)) print ("your score " + str(other.handtotal)) print ("dealer wins!") message() #who won def main(): carddeck = deck() print ('initial deck: ') print(carddeck) carddeck.shuffle() print ('shuffled deck: ') print(carddeck) player = player() opponent = player() #give each player 2 cards, alternating carddeck.dealone(player) carddeck.dealone(opponent) carddeck.dealone(player) carddeck.dealone(opponent) print ('deck after giving 2 cards each') print (carddeck) #show 1 faceup card each player showhands(player,opponent) #start playing playerturn(carddeck,player, opponent) opponentturn(carddeck, opponent, player) main()
Comments
Post a Comment