I want to build a straight function for a Texas hold'em program. I've created some test values and want the function to return the list of cards that satisfy the straight.
This is what I have so far:
import cards
c1=cards.Card(1,1)
c2=cards.Card(2,1)
c3=cards.Card(3,2)
c4=cards.Card(4,2)
c5=cards.Card(5,2)
c6=cards.Card(6,4)
c7=cards.Card(3,4)
c8=cards.Card(7,3)
H1=[c7,c3,c2,c6,c5,c4,c1]
H2=[c1,c2,c3,c2,c3,c3,c8]
def build_rank_D(H):
dict1={}
for item in H:
A=item.get_rank()
if A not in dict1:
dict1[A]=[item]
else:
dict1[A].append(item)
return dict1
def straight(H):
sequence=set()
for item in H:
A=item.get_rank()
sequence.add(A)
list_seq=list(sequence)
n=list_seq[0]
new_list=[]
if list_seq[1]==n+1 and list_seq[2]==n+2 and list_seq[3]==n+3 and list_seq[4]==n+4
print("you have a straight")
return H
else:
print("no straight found")
return []
print(straight(H1))
straight(H2)
Right now the function prints the entire set of cards, not the cards that satisfy the straight, which is what I want.
This is a sample of the cards class program that I've imported:
import random # required for shuffle method of Deck
class Card(object):
''' Suit and rank are ints, and index into suit_list and rank_list.
Value is different from rank: for example face cards are equal in value (all 10)
'''
# Use these lists to map the ints of suit and rank to nice words.
# The 'x' is a place holder so that index-2 maps to '2', etc.
suit_list = ['x','c','d','h','s']
rank_list = ['x', 'A', '2', '3', '4', '5', '6', '7', '8', '9', '10','J', 'Q', 'K']
def __init__(self, rank=0, suit=0):
''' Rank and suit must be ints. This checks that they are in the correct range.
Blank card has rank and suit set to 0.
'''
if type(suit) == int and type(rank) == int:
# only good indicies work
if suit in range(1,5) and rank in range(1,15):
self.__suit = suit
self.__rank = rank
else:
self.__suit = 0
self.__rank = 0
else:
self.__suit = 0
self.__rank = 0
def get_rank(self):
return self.__rank
def get_suit(self):
return self.__suit