Skip to content

Instantly share code, notes, and snippets.

@akreer135
Last active August 29, 2015 14:02
Show Gist options
  • Save akreer135/e92ecc060b3c4d4bb507 to your computer and use it in GitHub Desktop.
Save akreer135/e92ecc060b3c4d4bb507 to your computer and use it in GitHub Desktop.
import itertools
values = "34567890JQKA2"
def cardRanker(card):
return values.index(card[0]), card
def sort_cards(hand):
return [c[1] for c in sorted(map(cardRanker,hand))]
def isStraight(play):
if len(play)<3: return False
cards = list(sorted(map(cardRanker,play),key=lambda card: card[0]))
suits = [card[1][1] for card in cards]
if len(set(suits))>1: return False # not all the same suit
values = [card[0] for card in cards]
value = values.pop(0)
while values:
new_value = values.pop(0)
if new_value!=(value+1):
return False
value = new_value
return True
def isKind(play):
cards = list(sorted(map(cardRanker,play),key=lambda card: card[0]))
values = [card[0] for card in cards]
if len(set(values))>1: return False
return True
def isPlay(play):
return (len(play)==1) or isKind(play) or isStraight(play)
def swap_cards(hand,pid):
hand = sort_cards(hand)
if pid==0:
return hand[-2:]
if pid==1:
return hand[-1:]
cards = [hand.pop(0)]
if pid>2:
cards.append(hand.pop(0))
return cards
def generate_plays(hand):
plays = [[play] for play in sort_cards(hand)]
for i in range(2,len(hand)+1):
plays.extend(map(sort_cards,filter(isPlay, itertools.combinations(hand,r=i))))
return plays
def is_valid_play(play,rnd):
check_suit = None
if len(rnd) == 0:
#for passing
if play is None:
return False
return isPlay(play)
first_play = rnd[0]
prev_play = rnd[-1]
if len(rnd) > 1:
second_play = rnd[1]
if isStraight(hand):
if first_play[0][1] == second_play[0][1]:
check_suit = first_play[0][1]
def play(rnd, hand, discard, holding, generate=generate_plays, valid=is_valid_play):
for play in generate(hand):
print play
if valid(play, rnd):
return play
return None
hand = ["3S", "3D", "4D", "5D", "6S"]
print play([],hand, None, None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment