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
  87. 87
  88. 88
  89. 89
  90. 90
  91. 91
  92. 92
  93. 93
  94. 94
  95. 95
  96. 96
  97. 97
  98. 98
  99. 99
  100. 100
  101. 101
  102. 102
  103. 103
  104. 104
  105. 105
  106. 106
  107. 107
  108. 108
  109. 109
  110. 110
  111. 111
  112. 112
  113. 113
  114. 114
  115. 115
  116. 116
  117. 117
  118. 118
  119. 119
  120. 120
  121. 121
  122. 122
  123. 123
  124. 124
  125. 125
  126. 126
  127. 127
  128. 128
  129. 129
  130. 130
  131. 131
  132. 132
  133. 133
  134. 134
  135. 135
  136. 136
  137. 137
  138. 138
  139. 139
  140. 140
  141. 141
  142. 142
  143. 143
  144. 144
  145. 145
  146. 146
  147. 147
  148. 148
  149. 149
  150. 150
  151. 151
  152. 152
  153. 153
  154. 154
  155. 155
  156. 156
  157. 157
  158. 158
  159. 159
  160. 160
  161. 161
  162. 162
  163. 163
  164. 164
  165. 165
  166. 166
  167. 167
  168. 168
  169. 169
  170. 170
  171. 171
  172. 172
  173. 173
  174. 174
  175. 175
  176. 176
  177. 177
  178. 178
  179. 179
  180. 180
  181. 181
  182. 182
  183. 183
  184. 184
  185. 185
  186. 186
  187. 187
  188. 188
  189. 189
  190. 190
  191. 191
  192. 192
  193. 193
  194. 194
  195. 195
  196. 196
  197. 197
  198. 198
  199. 199
  200. 200
  201. 201
  202. 202
  203. 203
  204. 204
  205. 205
  206. 206
  207. 207
  208. 208
  209. 209
#!/usr/bin/python
# Bet Starter File

# NOTE: You can run this file locally to test if your program is working.

#=============================================================================

# INPUT FORMAT: hand, others, card, scores

# hand: Your current hand (a list of integers 2 to 14)
# others: All other players' hands, in a fixed order
# card: The card currently being bet on (an integer 2 to 14)
# scores: All current scores of players (from this game only) in a
#   fixed order. Your score is displayed as the first value.

# Example Input:

# hand: [2, 4, 14]
# others: [[6, 8, 9], [5, 11, 13]]
# card: 9
# scores: [34, 21, 18]

#=============================================================================

# OUTPUT FORMAT: A single integer between 2 and 14, inclusive, that is
# currently in your hand.

# Invalid outputs will result in your lowest card being played by default.

### WARNING: COMMENT OUT YOUR DEBUG/PRINT STATEMENTS BEFORE SUBMITTING !!!
### (this file uses standard IO to communicate with the grader!)

#=============================================================================

# Write your bot in the bet_bot class. Helper functions and standard library
# modules are allowed, and can be written before/inside these classes.

# You can define as many different strategies as you like, but only the class
# currently named "bet_bot" will be run officially.


# Example bot that plays a random card every round:

import random

class bet_bot:
    
    def __init__(self):
        # You can define global states (that last between moves) here
        pass
    
    def move(self, hand, others, card, scores):
        # You should write your code that moves every turn here
        return random.choice(hand)

#=============================================================================

# Local testing parameters

# If you would like to view a turn by turn game display while testing locally,
# set this parameter to True

LOCAL_VIEW = False

# Set a list of 3 strategies you would like to test locally

LOCAL_STRATS = [bet_bot(), bet_bot(), bet_bot()]

#=============================================================================












































# You don't need to change any code below this point

import json
import sys

def WAIT():
    return json.loads(input())

def SEND(data):
    print(json.dumps(data), flush=True)

MASK = lambda a,i: a[:i] + a[i+1:]

def PLAY(players):
    total = [0] * 3
    for ROUND in range(1, 6):
        scores = [0] * 3
        hands = [[*range(2, 15)] for i in range(3)]
        deck = [*range(2, 15)]
        if LOCAL_VIEW:
            print("ROUND",ROUND)
        for _ in range(13):
            card = random.choice(deck)
            deck.remove(card)
            if LOCAL_VIEW:
                for i in range(3):
                    print(f"Player {i+1}'s hand:", ' '.join(map(str, hands[i])))
                print("Card being bet on:", card)
            values = []
            for i in range(3):
                try:
                    val=players[i].move(hands[i],MASK(hands,i),card,[scores[i]]+MASK(scores,i))
                    if not (val==int(val) and val in hands[i]):
                        raise Exception('invalid move')
                except Exception as e:
                    print(f"Player {i+1} has an error: {e}! Playing the lowest card instead.")
                    val = min(hands[i])
                values.append(val)
                if LOCAL_VIEW:
                    print(f"Player {i+1}'s bet:", val)
            for i in range(3):
                hands[i].remove(values[i])
            big = max(values)
            if values.count(big) == 1:
                if LOCAL_VIEW:
                    print(f"Player {values.index(big)+1} has won the card!")
                scores[values.index(big)] += card
            else:
                if LOCAL_VIEW:
                    print("There was a tie! Nobody won the card.")
            if LOCAL_VIEW:
                print("Current totals:", ' '.join(map(str, scores)))
                print()
                input("Enter to continue (change LOCAL_VIEW to toggle this)")
        for i in range(3):
            scores[i] /= 100
        big = max(scores)
        tie = scores.count(big)
        if tie == 3:
            for i in range(3):
                scores[i] += 1
        elif tie == 2:
            for i in range(3):
                if scores[i] == big:
                    scores[i] += 2
                else:
                    scores[i] += 1
        else:
            index = scores.index(big)
            scores[index] += 5
            small = min(scores)
            for i in range(3):
                if scores[i] == small:
                    scores[i] += 1
                elif i != index:
                    scores[i] += 2
        for i in range(3):
            total[i] += scores[i]
        print()
        print("Round",ROUND,"scores:",' '.join(map(str, scores)))
        print()
    print("Game finished!")
    print("Final scores:",' '.join(map(str, total)))
    if not LOCAL_VIEW:
        print("Change LOCAL_VIEW to True to see a turn by turn replay")

if len(sys.argv) < 2 or sys.argv[1] != 'REAL':
    PLAY(LOCAL_STRATS)
    input()

else:
    player = bet_bot()
    while True:
        data = WAIT()
        play = player.move(data["hand"],data["others"],data["card"],data["scores"])
        SEND(play)