# Homework Help: Winning poker hand

1. Dec 7, 2009

### DanielJackins

1. The problem statement, all variables and given/known data

I have to create a function which determines which hand is the winning (3-card) poker hand.

Code (Text):
import random
from subprocess import Popen, PIPE
from bpipe import *
from time import sleep

class Card:
def __init__(self, suit='club', rank ='A'):
self.suit = suit
self.rank = rank
def __str__(self):
return self.suit + '-' + self.rank

class Deck:
def __init__(self):
self.cards = []
for rank in ['A','2','3','4','5','6','7','8', \
'9','10','J','Q','K']:
self.cards += [Card(suit,rank)]

def shuffle(self):
for i in range(len(self.cards)):
j = random.randrange(i, len(self.cards))
self.cards[i],self.cards[j] = self.cards[j],self.cards[i]

def deal(self):
return self.cards.pop()

class Hand:
def __init__(self, cards = [], displayed=0):
self.cards = cards
self.displayed = displayed

def get_card(self, card):
self.cards += [card]

def eval(self):
return

def display(hands, round):
for i in range(2):
for j in range(hands[i].displayed, len(hands[i].cards)):
s = hands[i].cards[j].__str__()
qdin.write('loadimage %s.png %s\n' % (s, s+str(round)))
qdin.write('drawimage %d %d %s\n' % (265+j*80, 100+200*i, s+str(round)))
hands[i].displayed = len(hands[i].cards)

qd = Popen("java -jar quickdraw.jar", shell=True, bufsize=1, \
stdin=PIPE, stdout=PIPE, close_fds=True)
qdin, qdout = qd.stdin, qd.stdout
qdin.write( "mouseclick True\n" )

deck = Deck()
deck.shuffle()

qdin.write("color 30 20 60\n fillrect 650 100 100 60\n" )
qdin.write("color 255 255 255\n text (Re)play 675 135\n")
qdin.write("color 30 20 60\n fillrect 650 250 100 60\n" )
qdin.write("color 255 255 255\n text Quit 675 285\n")

round = 0
while True:
event, val = ParseEvent( qdout.readline() )
if event == "MouseClicked":
x, y = val[0], val[1]
if 650<x<750:
if 100<y<160:
round +=1
qdin.write("color 0 0 0\n text YouWin!              100 350\n")
qdin.write("color 0 0 0\n text HouseWins!           100 150\n")
qdin.write("color 0 0 0\n text Tie!                 370 500\n")
deck = Deck()
deck.shuffle()
hands = [Hand([deck.deal()]+[deck.deal()] + [deck.deal()] )] + \
[Hand([deck.deal()]+[deck.deal()] + [deck.deal()] )]
display(hands,round)
p_score = hands[1].eval()
h_score = hands[0].eval()
if h_score < p_score:
qdin.write("color 255 255 255\n text YouWin! 100 350\n"  )
elif h_score > p_score:
qdin.write("color 255 255 255\n text HouseWins! 100 150\n")
else:
qdin.write("color 255 255 255\n text Tie! 370 500\n")

if 250<y<310: break

qdin.write("quit")
So I have to define the function eval.

3. The attempt at a solution

None thus far. I don't really know where to start. The only thing I can really think of is a bunch of if statements with complicated conditions, but I'm not sure about that. A nudge in the right direction would be greatly appreciated

2. Dec 7, 2009

### CompuChip

Well, basically all you need is a comparing algorithm that can compare two hands.
Then you can simply loop through all players, keeping track of the highest hand, i.e. something like

Code (Text):

int highestHand = 0;
for(i = 1; i < playerCount; i++) {
if(hands[i].higherThan( hands[highestHand] )
highestHand = i;
}

Here the higherThan method compares a hand to another set of cards passed in the parameter, and returns true if it scores more.
If you want to solve it neatly, you can even give the hands ability to tell the sorting function themselves how many pairs they have, if there is a flush, etc.

Alternatively you could assign scores to different hands in such a way that better hands score more points; then assign the score to every hand and simply do integer sorting.