hand = Hand() for _ in range(num_cards): hand.draw_card(deck) print("Select cards to hold separated by a space") print('Hit return to discard all') print(hand) print('[1] [2] [3] [4] [5]') print('') hold_cards = input('> ') if len(hold_cards) == 0: hold_cards = [] else: hold_cards = hold_cards.split(" ") hold_cards = [int(val)-1 for val in hold_cards] for index in range(5): pos = 4 - index if pos not in hold_cards: hand.discard(pos) for _ in range(5 - len(hold_cards)): hand.draw_card(deck) print(hand) score = score_hand(hand) print(score) total_credits += pay_table[score] * bet * credit_value print('Total credits: ${0:0.2f}'.format(total_credits)) print("Press enter to play again, q to quit:") command = input('> ')
class Player(): def __init__(self, name): self.id = None self.name = name self.type = 'Human' self.hand = Hand() self.legalCards = [] self.wildCards = [] self.valueChangeCards = [] self.zeroCards = [] self.canSkip = False self.canReverse = False self.canDrawTwo = False self.canDrawFour = False self.canValueChange = False self.drew = False self.points = 0 self.forceDraw = 0 def addCard(self, card): self.drew = True if self.forceDraw > 0: self.forceDraw -= 1 self.drew = False self.hand.addCard(card) def beginTurn(self): self.drew = False def didDraw(self): return self.drew def getLegalCards(self, color, value, zeroChange=False): self.canSkip = False self.canReverse = False self.canDrawTwo = False self.canDrawFour = False self.canValueChange = False self.canZeroChange = False self.legalCards = [] self.wildCards = [] self.valueChangeCards = [] self.zeroCards = [] plusFours = [] for card in self.hand: if card.isWild(): if card.getValue() == '+4': plusFours.append(card) else: self.wildCards.append(card) elif zeroChange and card.isZero(): self.canZero = True self.zeroCards.append(card) elif card.getColor() == color or card.getValue() == value: if card.getColor() != color: self.canValueChange = True self.valueChangeCards.append(card) if card.getValue() == "+2": self.canDrawTwo = True elif card.getValue() == 'R': self.canReverse = True elif card.getValue() == 'X': self.canSkip = True self.legalCards.append(card) if len(self.legalCards) == 0 and len(plusFours) > 0: self.canDrawFour = True self.wildCards += plusFours def getValidCards(self): return self.legalCards def getAllValidCards(self): return self.legalCards + self.wildCards + self.zeroCards def hasLegalCard(self): return len(self.legalCards) > 0 def addPoints(self, amount): if (self.points + amount) <= 999999999999999999999: self.points += amount def removeCard(self, index): return self.hand.removeCard(index) def assignID(self, identity): self.id = identity def getName(self): return self.name def getID(self): return self.id def getPoints(self): return self.points def getType(self): return self.type def getCardNum(self): return len(self.hand) def getHand(self, scrollNum=0, hide=False): return self.hand.show(scrollNum, hide) def getForceDraws(self): return self.forceDraw def addForceDraw(self, num): self.forceDraw += num def decreaseForceDraw(self): self.forceDraw -= 1 def removeForceDraw(self): self.forceDraw = 0 def checkCard(self, index): return self.hand.getCard(int(index)) def discardHand(self): self.hand.discard() def __str__(self): return self.name def __repr__(self): return '({},{})'.format(self.name, self.points)
class Game(): def __init__(self, cards_list = None): self.mode = 0 self.win_hand = 0 # 0: round start, valid bet # place_bet() # 1: cards dealt for round 1 # round_start() # 2: cards dealt for round 2 # judge() # 3: round end, everything finalized # payout() # default variables self.total_credits = 20.00 self.bet = 5 self.credit_value = 0.25 self.hold_cards = [False, False, False, False, False] self.deck = Deck() self.hand = Hand() def place_bet(self): if (self.bet <= self.total_credits): self.total_credits -= self.bet return True else: return False def set_bet(self, new_bet): if (new_bet <= self.total_credits): self.bet = self.bet def round_start(self): self.deck = Deck() self.hand = Hand() for _ in range(5): self.hand.draw_card(self.deck) self.hold_cards = [False, False, False, False, False] def hold(self, num): num = int(num) if (num >= 1 & num <= 5): self.hold_cards[num-1] = not self.hold_cards[num-1] def judge(self): #print(self.hold_cards) for index in range(4, -1, -1): #print(index, self.hold_cards[index]) if self.hold_cards[index] == False: #print("Attempting to discard #", str(index)) #print(self.hand) self.hand.discard(index) #print("Success:\t", self.hand) #else: #print("Not attempting to discard #", str(index)) print("Length:\t", (5 - self.hand.length())) for _ in range(5 - self.hand.length()): self.hand.draw_card(self.deck) # update hand GUI score = score_hand(self.hand) # to do: convert change score_hand into an array output in the format [score_name,score]) return score def test_class(self): #Def os.system('clear') print(''' Testing Video Poker Terminal Version! ============================== Please enter a command: q - quit n - new game ''') command = input('> ') if command.lower() == 'q': sys.exit() else: if (self.set_bet(5)): #nothing i = True else: self.total_credits = 20 self.set_bet(5) self.credit_value = 0.25 while (command.lower() != 'q') and (self.total_credits > 0): os.system('clear') self.round_start() self.total_credits -= self.bet * self.credit_value print('Total credits: ${0:0.2f}'.format(self.total_credits)) print('') self.round_start() print("Select cards to hold separated by a space") print('Hit return to discard all') print(self.hand) print('[1] [2] [3] [4] [5]') print('') # Using original implementation of hold command for this test temp_cards = input('> ') self.hold_cards = [False, False, False, False, False] #Debug if len(temp_cards) == 0: self.hold_cards = [False, False, False, False, False] else: temp_cards = temp_cards.split(" ") print(temp_cards, len(temp_cards)) for i in range(5,0,-1): print("Test:\t", temp_cards, "\t", i) if temp_cards.count(str(i)) > 0: self.hold_cards[i-1] = True #End original implementation result = self.judge() print("Cards:", self.hand) pay_table = { "Royal Flush": 800, "Straight Flush": 50, "4 of a Kind": 25, "Full House": 9, "Flush": 6, "Straight": 4, "3 of a Kind": 3, "2 Pair": 2, "Jacks or Better": 1, "Not a Winning Hand": 0, } self.total_credits += pay_table[result] * self.bet * self.credit_value print('Total credits: ${0:0.2f}'.format(self.total_credits)) print("Press enter to play again, q to quit:") command = input('> ')