Find the Best 3-Card Poker Hand with this Python Function - No Quotation Marks

  • Thread starter DanielJackins
  • Start date
  • Tags
    hand
In summary, the conversation involves creating a function to determine the winning 3-card poker hand out of a deck of cards. The code provided involves creating classes for Card, Deck, and Hand, as well as a display function. The challenge is to define the eval function which will evaluate the hands and determine the winner. One approach is to use a comparing algorithm to loop through each player's hand and keep track of the highest scoring hand. Another approach is to assign scores to different hands and use integer sorting to determine the winner.
  • #1
DanielJackins
40
0

Homework Statement



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

Code:
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 suit in ['club','spade','heart','diamond']:
			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.

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
 
Physics news on Phys.org
  • #2
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:
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.
 
  • #3
.

my first approach would be to break down the problem into smaller, more manageable steps. Here are some suggestions on how you can approach this problem:

1. Understand the rules of 3-card poker: Before attempting to create a function, make sure you fully understand the rules of 3-card poker and the different hands that can be formed.

2. Define the necessary data structures: In the code provided, there are already classes for Card, Deck, and Hand. You may need to modify these or create new data structures to store and manipulate the cards.

3. Determine the criteria for each hand: Each hand in poker has its own criteria for being considered a winning hand. For example, a straight flush is when all three cards have the same suit and are in sequential order. Make a list of all the possible hands and their criteria.

4. Create a function to check for each hand: Once you have the criteria for each hand, you can create a function that checks if a given hand meets that criteria. This function can return a boolean value indicating whether the hand is a winning hand or not.

5. Implement the eval() function: In the provided code, there is already a function named eval(). This is where you will need to call the functions you created in the previous step to determine the winning hand. You may also need to add additional logic to handle ties and determine the value of each hand.

6. Test your function: Before using your function in a larger program, it's important to test it with different inputs to make sure it's working correctly. You can create a few sample hands and manually check if the function is correctly identifying the winning hand.

I hope this helps guide you in creating your function to determine the best 3-card poker hand. Remember to break down the problem into smaller steps and don't be afraid to ask for help if you get stuck. Good luck!
 

Related to Find the Best 3-Card Poker Hand with this Python Function - No Quotation Marks

What is the purpose of the "Find the Best 3-Card Poker Hand with this Python Function - No Quotation Marks" function?

The purpose of this function is to help users find the best possible three-card poker hand using a Python programming language without the use of quotation marks.

What are the inputs for this function?

The inputs for this function are three cards, which can be represented as strings or numbers.

How does this function work?

This function works by taking the three input cards and comparing them to the various possible three-card poker hands. It then returns the best possible hand based on the rules of three-card poker.

What are the benefits of using this function?

Using this function can save time and effort for individuals trying to determine the best three-card poker hand. It also eliminates the need for manual calculations and reduces the chances of human error.

Are there any limitations to this function?

Yes, this function is limited to determining the best three-card poker hand based on the specific rules of three-card poker. It does not take into account any external factors or strategies that may influence the outcome of the game.

Similar threads

  • Programming and Computer Science
Replies
3
Views
4K
  • Programming and Computer Science
Replies
2
Views
2K
Back
Top