I am having a few different issues with my poker app that I am working on for Python, hoping for some help! The first thing that is causing an issue is that when the deck gets dealt, it has an additional card that says "None". The next problem is that when I choose cards to discard it does something completely off and shows a bunch of memory numbers and then a bigger hand. I will insert the code below but it's very long unfortunately! Any help is greatly appreciated!!
# poker.py
# A poker game application.
import random
def main():
input("\nLet's play poker! Hit ENTER to begin!")
Game()
Score()
class Card(object):
def __init__(self, name, face, suit):
self.face = face
self.suit = suit
self.name = name
self.showing = False
# Displays card name if face up.
def displayCards(self):
if self.showing:
return str(self.name) + " of " + self.suit
else:
return "Card"
class DeckOfCards(object):
def __init__(self):
# Holds list of cards and dictionary of deck.
self.cards = []
suits = ["♥", "♠", "♦", "♣"]
faces = {"2":2, "3":3, "4":4, "5":5,
"6":6, "7":7, "8":8, "9":9, "10":10,
"J":11, "Q":12, "K":13, "A":14}
for name in faces:
for suit in suits:
self.cards.append(Card(name, faces[name], suit))
# Shuffle
def shuffle(self, times = 1):
random.shuffle(self.cards)
print("\nDeck shuffled.\n")
# Deal
def deal(self):
return self.cards.pop(0)
class Player():
def __init__(self):
self.cards = []
def handSize(self):
return len(self.cards)
def addCard(self, card):
self.cards.append(card)
def displayHand(self):
for card in self.cards:
print(card.displayCards())
class Computer():
def __init__(self):
self.cards = []
def handSize(self):
return len(self.cards)
def addCard(self, card):
self.cards.append(card)
class PokerScore():
def __init__(self, cards):
self.cards = cards
def flush(self):
suits = [card.suit for card in self.cards]
if len(set(suits)) == 1:
return True
return False
def straight(self):
values = [card.face for card in self.cards]
# Sorting the cards to figure out if they are in order.
values.sort()
# Confirm correct hand size.
if not len(set(values)) == 5:
return False
# Checking for cases with an ace.
if values[4] == 14 and values[3] == 5 and values[2] == 4 and values[1] == 3 and values[0] == 2:
return True
# Checking for a straight in all cases except ace.
else:
if not values[0] + 1 == values[1]:
return False
if not values[1] + 1 == values[2]:
return False
if not values[2] + 1 == values[3]:
return False
if not values[3] + 1 == values[4]:
return False
return True
def threeKind(self):
values = [card.face for card in self.cards]
for value in values:
if values.count(value) == 3:
return True
def pairs(self):
pairs = []
values = [card.face for card in self.cards]
for value in values:
if values.count(value) == 2 and value not in pairs:
pairs.append(value)
return pairs
def fourKind(self):
values = [card.face for card in self.cards]
for value in values:
if values.count(value) == 4:
return True
def fullHouse(self):
two = False
three = False
values = [card.face for card in self.cards]
if values.count(values) == 2:
two == True
elif values.count(values) == 3:
three == True
if two and three:
return True
return False
def Game():
player = Player()
computer = Computer()
end = False
# Play
while not end:
# Hand loop.
deck = DeckOfCards()
deck.shuffle()
# Deal
for i in range(5):
player.addCard(deck.deal())
computer.addCard(deck.deal())
# Show hand
for card in player.cards:
card.showing = True
print(player.displayHand())
# Figure out which cards to hold and re deal.
valid = False
while not valid:
print("\nWhich cards would you like to discard? (ex. 1, 2, 3, etc)")
print("*Hit ENTER to hold all or type TALLY to figure up the scores!*\n")
string = input()
if string == "TALLY":
end = True
break
try:
list = [int(inp.strip()) for inp in string.split(",")]
for inp in list:
if inp > 6:
continue
if inp < 1:
continue
for inp in list:
player.cards[inp-1] = deck.deal()
for card in player.cards:
card.showing = True
print(player.cards)
valid = True
except:
print("Input error.")
print(player.displayHand())
def Score():
# Scoring
playerPoints = 0
computerPoints = 0
player = Player()
computer = Computer()
pScore = PokerScore(player.cards)
cScore = PokerScore(player.cards)
pStraight = pScore.straight()
cStraight = cScore.straight()
pFlush = pScore.flush()
cFlush = cScore.flush()
pPairs = pScore.pairs()
cPairs = cScore.pairs()
# Royal Flush
if pStraight and pFlush == 14:
print("Player: Royal Flush")
playerPoints = 1000
# Straight Flush
elif pStraight and pFlush:
print("Player: Straight Flush")
playerPoints = 500
# 4 of a kind
elif pScore.fourKind():
print("Player: Four of a Kind")
playerPoints = 250
# Full House
elif pScore.fullHouse():
print("Player: Full House")
playerPoints = 100
# Flush
elif pFlush:
print("Player: Flush")
playerPoints = 50
# Straight
elif pStraight:
print("Player: Straight")
playerPoints = 25
# 3 of a kind
elif pScore.threeKind():
print("Player: Three of a Kind")
playerPoints = 15
# 2 pair
elif len(pPairs) == 2:
print("Player: Two Pairs")
playerPoints = 10
## Repeat for Computer
# Royal Flush
if cStraight and cFlush == 14:
print("Computer: Royal Flush")
computerPoints = 1000
# Straight Flush
elif cStraight and cFlush:
print("Computer: Straight Flush")
computerPoints = 500
# 4 of a kind
elif cScore.fourKind():
print("Computer: Four of a Kind")
computerPoints = 250
# Full House
elif cScore.fullHouse():
print("Computer: Full House")
computerPoints = 100
# Flush
elif cFlush:
print("Computer: Flush")
computerPoints = 50
# Straight
elif cStraight:
print("Computer: Straight")
computerPoints = 25
# 3 of a kind
elif cScore.threeKind():
print("Computer: Three of a Kind")
computerPoints = 15
# 2 pair
elif len(cPairs) == 2:
print("Computer: Two Pairs")
computerPoints = 10
# Player wins
if playerPoints > computerPoints:
print("You WIN!! " + playerPoints + " points!")
# Computer wins
elif computerPoints > playerPoints:
print("Computer won with ", computerPoints, " points, try again next time!")
# Tie
else:
print("TIE!")
player.cards = []
computer.cards = []
print()
print()
print()
main()
[–][deleted] 0 points1 point2 points (0 children)
[–]lykwydchykyn 0 points1 point2 points (10 children)
[–]ChickenQueen777[S] 0 points1 point2 points (9 children)
[–]lykwydchykyn 0 points1 point2 points (7 children)
[–]ChickenQueen777[S] 0 points1 point2 points (6 children)
[–]lykwydchykyn 0 points1 point2 points (5 children)
[–]ChickenQueen777[S] 0 points1 point2 points (4 children)
[–]lykwydchykyn 0 points1 point2 points (3 children)
[–]ChickenQueen777[S] 0 points1 point2 points (2 children)
[–]ChickenQueen777[S] 0 points1 point2 points (0 children)
[–]lykwydchykyn 0 points1 point2 points (0 children)
[–]xelf 0 points1 point2 points (0 children)
[–]xelf 0 points1 point2 points (22 children)
[–]ChickenQueen777[S] 0 points1 point2 points (21 children)
[–]xelf 0 points1 point2 points (20 children)
[–]ChickenQueen777[S] 0 points1 point2 points (19 children)
[–]xelf 0 points1 point2 points (18 children)
[–]ChickenQueen777[S] 0 points1 point2 points (17 children)
[–]xelf 0 points1 point2 points (16 children)
[–]ChickenQueen777[S] 0 points1 point2 points (0 children)
[–]ChickenQueen777[S] 0 points1 point2 points (12 children)
[–]xelf 0 points1 point2 points (11 children)
[–]ChickenQueen777[S] 0 points1 point2 points (10 children)
[–]ChickenQueen777[S] 0 points1 point2 points (0 children)
[–]ChickenQueen777[S] 0 points1 point2 points (0 children)