Completed smart hand building, passes all 7 card tests now :)

git-svn-id: file:///srv/svn/euler@12 e5f4c3ec-3c0c-11df-b522-21efaa4426b5
This commit is contained in:
Correl Roush 2010-04-01 03:44:03 +00:00
parent fc89b6e2c7
commit 987dc151cf

View file

@ -208,6 +208,18 @@ class Hand:
# We've already got a flush, return it! # We've already got a flush, return it!
return flushes[0] return flushes[0]
# Look for a straight! # Look for a straight!
mvals = sorted(merged.keys(), reverse=True)
for i in range(0, len(mvals) -4, 1):
if (mvals[i] - mvals[i + 4]) == 4:
# Regular straight
h = [[c for c in cards if c.value == v][0] for v in mvals[i:i + 5]]
return Hand([str(c) for c in h])
elif 14 in [c.value for c in cards] and mvals[i + 1] == 5 and mvals[i + 4] == 2:
# Ace low straight
h = [[c for c in cards if c.value == v][0] for v in mvals[i + 1:i + 5]]
h.append([c for c in cards if c.value == 14][0])
return Hand([str(c) for c in h])
if trips: if trips:
h = trips[:3] h = trips[:3]
remaining = [c for c in cards if c.value not in [cc.value for cc in h]][:2] remaining = [c for c in cards if c.value not in [cc.value for cc in h]][:2]
@ -225,8 +237,8 @@ class Hand:
for r in remaining: h.append(r) for r in remaining: h.append(r)
return Hand([str(c) for c in h]) return Hand([str(c) for c in h])
# High card, send the top 5 reverse-sorted cards
return Hand.create_best_hand_bruteforce([str(c) for c in cards]) return Hand([str(c) for c in cards[:5]])
def __cmp__(self, other): def __cmp__(self, other):
# Compare hand rankings # Compare hand rankings
result = cmp(self.rank(), other.rank()) result = cmp(self.rank(), other.rank())