#! /usr/bin/env python
import sys
# print (sys.argv)
cards = []
for idx, val in enumerate(sys.argv):
if idx > 0:
each = val.split('=')
cards.append(each)
def evalGame(cards, lastGame = None, playerNum = 3, cardNum = 5):
if cards is None:
return []
player = None
otherCards = []
if lastGame is None:
lastGame = []
player = [cards[0][0], int(cards[0][1])]
otherCards.append(int(cards[1][1]))
otherCards.append(int(cards[2][1]))
else:
playerIdx = len(lastGame) % playerNum
player = cards[playerIdx]
for i in range(playerNum):
if i != playerIdx:
otherCards.append(int(cards[i][1]))
possibles = [x for x in range(1, cardNum + 1) if x not in otherCards]
count = len(lastGame)
for i in [1, 0]:
if count == 0:
break
count -= 1
for p in possibles:
chk = evalFromOther([p, otherCards[i]], cardNum = 5)
if chk:
possibles = [x for x in possibles if x != p]
results = []
for p in possibles:
onePossible = []
for o in otherCards:
onePossible.append(p > o)
results.append(onePossible)
checkResult = evalFromOther(otherCards, possibles, cardNum)
if checkResult:
check = results[0]
if check[0] != check[1]:
lastGame.append([player[0], 'Mid'])
else:
minOrMax = 'Max' if check[0] else 'Min'
lastGame.append([player[0], minOrMax])
return lastGame
else:
lastGame.append([player[0], '?'])
return evalGame(cards, lastGame)
def evalFromOther(otherCards, possibles = None, cardNum = 5):
if possibles is None:
possibles = [x for x in range(1, cardNum + 1) if x not in otherCards]
results = []
for p in possibles:
onePossible = []
for o in otherCards:
onePossible.append(p > o)
results.append(onePossible)
checkResult = True
for r in results:
if r != results[0]:
checkResult = False
break
return checkResult
def printResult(results):
rslt = ""
for item in results:
player = item[0]
value = item[1]
tmp = player + " => " + value
if rslt == "":
rslt += tmp
else:
rslt += ',' + tmp
print(rslt)
printResult(evalGame(cards))
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.