##Tommy Luo
##4/19/13
##Mr. Potter
##Contest #4
##Enloe HS
##Senior 5 Team
value = {'A':1,
'2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, 'T':10,
'J':11, 'Q':12, 'K':13}
suit = {'S':4,
'H':3, 'C':2, 'D':1}
def
findset(hand):
ret = ''
for x in xrange(len(hand)-2):
hold = hand[x]
ret = list()
ret.append(hold)
for y in xrange(x+1,len(hand)):
hold2 = hand[y]
if hold[0] == hold2[0] and len(ret)
< 4:
ret.append(hold2)
if len(ret) >= 3:
return ret
return False
def
withinone(hand, card):
for thing in hand:
if abs(value[card[0]]-value[thing[0]])
<= 1:
return True
return False
def findrun(hand):
ret = ''
for x in xrange(len(hand)-2):
hold = hand[x]
ret = list()
ret.append(hold)
for y in xrange(x+1, len(hand)):
hold2 = hand[y]
if hold[1] == hold2[1] and
withinone(ret, hold2) and len(ret) < 4:
ret.append(hold2)
if len(ret) >= 3:
return ret
return False
def sortrun(run):
for x in xrange(len(run)):
for y in xrange(x, len(run)):
if value[run[x][0]] > value[run[y][0]]:
hold = run[x]
run[x] = run[y]
run[y] = hold
def
sortset(sets):
for x in xrange(len(sets)):
for y in xrange(x, len(sets)):
if suit[sets[x][1]] <
suit[sets[y][1]]:
hold = sets[x]
sets[x] = sets[y]
sets[y] = hold
def
sortfiller(filler):
for x in xrange(len(filler)):
for y in xrange(x, len(filler)):
if value[filler[x][0]] <
value[filler[y][0]]:
hold = filler[x]
filler[x] = filler[y]
filler[y] = hold
elif value[filler[x][0]] ==
value[filler[y][0]] and suit[filler[x][1]] < suit[filler[y][1]]:
hold = filler[x]
filler[x] = filler[y]
filler[y] = hold
def
gameover(hand):
holdset = list()
holdrun = list()
if findset(hand):
holdset.append(findset(hand))
for thing in holdset[0]:
hand.remove(thing)
if findset(hand):
holdset.append(findset(hand))
for thing in holdset[1]:
hand.remove(thing)
if findrun(hand) and not hand == []:
holdrun.append(findrun(hand))
for thing in holdrun[0]:
hand.remove(thing)
if findrun(hand):
holdrun.append(findrun(hand))
for thing in holdrun[1]:
hand.remove(thing)
if len(hand) == 0:
return True
return False
def
keepcard(hand, card):
rem = False
if not gameover(hand[:]):
hand.append(card)
else:
rem = True
ret = list()
holdset = list()
holdrun = list()
if findset(hand):
holdset.append(findset(hand))
for thing in holdset[0]:
hand.remove(thing)
if findset(hand):
holdset.append(findset(hand))
for thing in holdset[1]:
hand.remove(thing)
if findrun(hand) and not hand == []:
holdrun.append(findrun(hand))
for thing in holdrun[0]:
hand.remove(thing)
if findrun(hand):
holdrun.append(findrun(hand))
for thing in holdrun[1]:
hand.remove(thing)
for run in holdrun:
sortrun(run)
for sets in holdset:
sortset(sets)
sortfiller(hand)
if (card in hand) and not rem:
hand.remove(card)
rem = True
bighold = list()
if len(holdrun) == 2:
if (value[holdrun[0][0][0]] >
value[holdrun[1][0][0]] or (value[holdrun[0][0][0]] == value[holdrun[1][0][0]]
and suit[holdrun[0][0][1]] > suit[holdrun[1][0][1]])) and len(holdrun[0]) ==
len(holdrun[1]):
hold = holdrun[1]
holdrun[1] = holdrun[0]
holdrun[0] = hold
if len(holdset) == 2:
if value[holdset[0][0][0]] >
value[holdset[1][0][0]] and len(holdset[0]) == len(holdset[1]):
hold = holdset[1]
holdset[1] = holdset[0]
holdset[0] = hold
for run in holdrun:
if len(run) == 4:
bighold.append(run)
for sets in holdset:
if len(sets) == 4:
bighold.append(sets)
for run in holdrun:
if len(run) == 3:
bighold.append(run)
for sets in holdset:
if len(sets) == 3:
bighold.append(sets)
bighold.append(hand)
for thing in bighold:
ret = ret + thing
if not rem:
return ret[:-1]
else:
return ret
f =
open("RummyInput2.txt", "r")
hand = ''
decks = list()
count = -1
for line in f:
if count == -1:
hand = line.strip().split(',')
else:
decks.append(line.strip().split(','))
count+=1
for deck in decks:
hi = ''
holdhand = hand[:]
for card in deck:
holdhand = keepcard(holdhand[:], card)
for thing in holdhand:
hi = hi+thing+', '
print hi[:-2]