cmimc

Code for the 2021 CMIMC Programming Contest

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
  80. 80
  81. 81
  82. 82
  83. 83
  84. 84
  85. 85
  86. 86
#!/usr/bin/python
import json
import copy

fileName = str(input("Please type the exact file name of the match you want to visualize. Ensure that your match data and this file are in the same directory:\n"))

with open (fileName) as f:
    data = json.load(f)

table = [['0' for i in range(7)] for j in range(13)]

numRounds = len(data['gamedata'])
print("There are {} games in this match.".format(numRounds))
index = int(input("Enter which game you want to view from 1 to {}:\n".format(numRounds)))
assert index > 0 and index < numRounds+1,"Did not enter valid game number."
seatNum = data['seat']

for item in data['gamedata'][index-1]['hist']: 
    # each item is a round corresponding to one of the 13 cards from the deck
    table[item['time']-1][0] = item['card']
    
    for i in range(3):
        table[item['time']-1][i+1] = item['moves'][i]['using']
    if item['moves'][seatNum-1]['got'] == [] or item['moves'][seatNum-1]['got']!=item['moves'][seatNum-1]['using']:
        table[item['time']-1][3+seatNum] = item['moves'][seatNum-1]['error']

output = "\nYou are P1. Your opponents are P2 and P3.\n\n"
totals = {}
totals["P1"] = 0
totals["P2"] = 0
totals["P3"] = 0

for row in table:
    scores = {}
    scores["P1"] = row[seatNum]
    if seatNum == 1:
        scores["P2"] = row[2]
        scores["P3"] = row[3]
    elif seatNum == 2:
        scores["P2"] = row[1]
        scores["P3"] = row[3]  
    else:
        scores["P2"] = row[1]
        scores["P3"] = row[2]  
    

    vals = copy.deepcopy(row)
    vals = [vals[1],vals[2],vals[3]]
    scoresInc = sorted(vals)
    currRound = str(table.index(row)+1)
    if row[3+seatNum] == '0':     
        if scoresInc[2] != scoresInc[1]:
            player = max(scores, key=scores.get)
            added = "Round {}: {} wins {} points\n".format(currRound,player,str(row[0]))
            totals[player] += row[0]
        elif len(set(scoresInc))==2:
            a,b = [player for player, score in scores.items() if score == max(scores.values())]
            added = "Round {}: There was a collision between {} and {}\n".format(currRound,a,b)
        elif len(set(scoresInc))==1:
            added = "Round {}: There was a collision between P1, P2, and P3\n".format(currRound)
    else:
        added = "Round {}: Your code threw this error: {}.\n".format(currRound, row[3+seatNum])
        added += "Round {}: As a result, your lowest card was played.\n".format(currRound)
        if scoresInc[2] != scoresInc[1]:
            player = max(scores, key=scores.get)
            added += "Round {}: {} wins {} points\n".format(currRound,player,str(row[0]))
            totals[player] += row[0]
        elif len(set(scoresInc))==2:
            a,b = [player for player, score in scores.items() if score == max(scores.values())]
            added += "Round {}: There was a collision between {} and {}\n".format(currRound,a,b)
        elif len(set(scoresInc))==1:
            added += "Round {}: There was a collision between P1, P2, and P3\n".format(currRound)
    output += added

sortedTotals = {player: total for player, total in sorted(totals.items(), key=lambda item: item[1])}

print(output)

print("Player {} came in 1st place with a score of {}".format(list(sortedTotals)[2],list(sortedTotals.values())[2]))
print("Player {} came in 2nd place with a score of {}".format(list(sortedTotals)[1],list(sortedTotals.values())[1]))
print("Player {} came in 3rd place with a score of {}".format(list(sortedTotals)[0],list(sortedTotals.values())[0]))
yourScore = totals['P1']
yourRank = 3-list(sortedTotals).index('P1') 
rankPoints=[5,2,1]
yourRankPoints = rankPoints[2-list(sortedTotals).index('P1')]
bonusPoints = round(0.01*yourScore,3)
print("As a result, you ranked in place {} and received {} + {} = {} points for this game.".format(yourRank,yourRankPoints,bonusPoints,yourRankPoints+bonusPoints))