FlowerGirl1027 0 Newbie Poster

I need to analyze a list of five cards as a poker hand. After printing the cards, the program needs to categorize accordingly.

Royal Flush: 10, Jack, Queen, King, Ace all of the same suit.
Straight Flush: Five ranks in a row, all of the same suit
Four of a Kind: Four of the same rank
Full House: Three of one rank and two of another
Flush: Five cards of the same suit
Straight: Five ranks in a row
Three of a Kind: Three of one rank (but not a full house or four of a kind)
Two Pair: Two each of two different ranks
Pair: Two of the same rank
X High: If none of the previous categories fit, X is the value of the highest rank.

In my program, the Ace represent a 1.

Here is what my program looks like so far. I need help finishing the def analyzePoker(theHand): function.

class Card:

def __init__(self, rank, suit):
self.rank = rank
self.suit = suit


def getRank(self):
return self.rank


def getSuit(self):
return self.suit


def BJValue(self):
# returns the BlackJack value of the card
if self.rank > 10:
return 10
else:
return self.rank

rankList = ["narf", "Ace", "Two", "Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine", "Ten", "Jack", "Queen", "King"]

def __str__(self):
if self.suit == "c":
return (self.rankList[int(self.rank)]) + " of Clubs "
if self.suit == "d":
return (self.rankList[int(self.rank)]) + " of Diamonds "
if self.suit == "h":
return (self.rankList[int(self.rank)]) + " of Hearts "
if self.suit == "s":
return (self.rankList[int(self.rank)]) + " of Spades "


def printOptions():
# print menu of options to screen

print
print "1. Input a hand interactively."
print "2. Randomly generate a new hand."
print "3. Load a hand from a file."
print "4. Save the current hand to a file."
print "5. Print by rank."
print "6. Print by suit."
print "7. Analyze the poker category."
print "8. Re-print this list of options to the screen."
print "9. Exit the program."
print

def inputCard():
inputRank = raw_input ("Enter a rank.")
inputRank = int(inputRank)
while inputRank < 1 or inputRank > 13:
print "Not a valid number, must be between 1 and 13."
inputRank = raw_input ("Please enter a valid rank.")
inputRank = int(inputRank)


inputSuit = raw_input ("Enter a suit.")
while inputSuit != "c" and inputSuit != "d" and inputSuit != "h" and inputSuit != "s":
print "Not a valid suit. Please enter c, d, h, or s."
inputSuit = raw_input ("Please enter a valid suit.")

newCard = Card(inputRank, inputSuit)
return newCard

def inputHand():
card1 = inputCard()
print "Enter the second card."
card2 = inputCard()
print "Enter the third card."
card3 = inputCard()
print "Enter the fourth card."
card4 = inputCard()
print "Enter the fifth card."
card5 = inputCard()
newHand = [card1, card2, card3, card4, card5]
return newHand

def randomCard():
randSuit
newCard = Card(randRank, randSuit)
return newCard

def randomHand():
newHand = []
return newHand

def loadHand():
newHand = []
infilename = raw_input("What is the name of the file containing the hand? ")
infile = open(infilename, 'r')

for line in infile:
rank, suit = line.split()
theCard = Card(int(rank),suit)
newHand.append(theCard)
infile.close()

return newHand

def saveHand(theHand):
outfilename = raw_input("Save hand to what file? ")
outfile = open(outfilename, 'w')

for card in theHand:
outfile.write("%d %s" % (card.getRank(), card.getSuit()))

outfile.close()

def printHand(theHand):
# Print the hand to the screen in a nice way.
# Basically just print each card one by one.
print "The hand is: "
for card in theHand:
print card
print

def cmpRankSuit(card1, card2):
# Defines a cmp function that prioritizes first rank, then suit
if card1.getRank() < card2.getRank():
return -1
elif card1.getRank() > card2.getRank():
return 1
else:
if card1.getSuit() < card2.getSuit():
return -1
elif card1.getSuit() > card2.getSuit():
return 1
else:
return 0

def sortRankSuit(theHand):
# sort the hand by rank, suit
theHand.sort(cmpRankSuit)

def cmpSuitRank(card1, card2):
if card1.getSuit() < card2.getSuit():
return -1
elif card1.getSuit() > card2.getSuit():
return 1
else:
if card1.getRank() < card2.getRank():
return -1
elif card1.getRank() > card2.getRank():
return 1
else:
return 0

def sortSuitRank(theHand):
# sort the hand by suit, rank
theHand.sort(cmpSuitRank)

def analyzePoker(theHand):
card1Suit = theHand.index(0).getSuit()
if theHand.index(1).getSuit() == card1Suit:
# the suit of card1 is the same
else:
# not a royal flush, straight flush, or a flush
sameSuit = True
for cardNum in range(1,5):
if theHand.index(cardNum).getSuit() != card1Suit:
sameSuit = False

if sameSuit:
#if there the same suit, can be royal flush, straight flush, or flush

else:
#they aren't all the same suit

# Analyze as a poker hand and print the result.


return 0


def main():

# print menu of options to screen
printOptions()

hand = [] # create empty hand

action = input("Enter the number of the action that you would like to perform: ")

while action != 9:

if action == 1:
# input a hand from the user
hand = inputHand()

elif action == 2:
# randomly generate a hand
hand = randomHand()

elif action == 3:
# load hand from a file
hand = loadHand()

elif action == 4:
# save hand to a file
saveHand(hand)

elif action == 5:
# print hand to screen sorted by rank, suit
# changing the actual order of the hand
sortRankSuit(hand)
printHand(hand)

elif action == 6:
# print hand to the screen sorted by suit, rank
# changing the actual order of the hand
sortSuitRank(hand)
printHand(hand)

elif action == 7:
# Analyze the value of the hand as a poker hand
# and print the result
analyzePoker(hand)

elif action == 8:
# print menu options to screen again
printOptions()

else:
print "What you entered was not a number from one to nine."

action = input("Enter the number of the action that you would like to perform: ")


if __name__ == '__main__':
main()