#! /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)
# print(cards)
def evalGame(cards, lastGame):
if cards is None or len(cards) != 3:
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) % 3
player = cards[playerIdx]
for i in range(3):
if i != playerIdx:
otherCards.append(int(cards[i][1]))
possibles = [x for x in range(1, 6) 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]])
# print("otherCards", otherCards)
if chk:
possibles = [x for x in possibles if x != p]
# print("please exclude:", [p, otherCards[i]])
results = []
# print("possible: " , possibles)
for p in possibles:
onePossible = []
for o in otherCards:
onePossible.append(p > o)
results.append(onePossible)
checkResult = evalFromOther(otherCards, possibles)
if checkResult:
check = results[0]
if check[0] != check[1]:
lastGame[player[0]] = 'Mid'
else:
lastGame[player[0]] = 'Max' if check[0] else 'Min'
return lastGame
else:
lastGame[player[0]] = '?'
# print(lastGame)
return evalGame(cards, lastGame)
def evalFromOther(otherCards, possibles = None):
if possibles is None:
possibles = [x for x in range(1, 6) 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(obj):
rslt = ""
for key, value in obj.items():
tmp = key + "=>" + value
if rslt == "":
rslt += tmp
else:
rslt += ',' + tmp
print(rslt)
printResult(evalGame(cards, None))
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.