|
| 1 | +import random |
| 2 | +from config.config import ROCK, PAPER, SCISSORS |
| 3 | + |
| 4 | + |
| 5 | +class ConstantOpponent(): |
| 6 | + """ |
| 7 | + Always play same move |
| 8 | + """ |
| 9 | + def __init__(self, constant_choice): |
| 10 | + self.constant_choice = constant_choice |
| 11 | + |
| 12 | + def move(self, last_opponent_action): |
| 13 | + return self.constant_choice |
| 14 | + |
| 15 | + |
| 16 | +class RandomStrategyChangeOpponent(): |
| 17 | + """ |
| 18 | + Switches strategy with probability p |
| 19 | + """ |
| 20 | + def __init__(self, p=0.1): |
| 21 | + self.p = p |
| 22 | + self.choice = random.randint(0, 2) |
| 23 | + |
| 24 | + def move(self, last_opponent_action): |
| 25 | + if random.random() < self.p: |
| 26 | + self.choice = random.randint(0, 2) |
| 27 | + return self.choice |
| 28 | + |
| 29 | + |
| 30 | +class RoundRobinOpponent(): |
| 31 | + """ |
| 32 | + Rock, paper, scissors, rock, paper, scissors, ... |
| 33 | + """ |
| 34 | + def __init__(self): |
| 35 | + self.choice = random.randint(0, 2) |
| 36 | + self.update_rule = random.choice([-1, 1]) |
| 37 | + |
| 38 | + def move(self, last_opponent_action): |
| 39 | + self.choice += self.update_rule # be able to cycle both directions |
| 40 | + self.choice = self.choice % 3 |
| 41 | + return self.choice |
| 42 | + |
| 43 | + |
| 44 | +class ReverseRoundRobinDoubleTapOpponent(): |
| 45 | + """ |
| 46 | + Rock, rock, scissors, scissors, paper, paper, ... |
| 47 | + """ |
| 48 | + def __init__(self): |
| 49 | + self.random_offset = random.randint(0, 5) |
| 50 | + self.pattern = [0, 0, 2, 2, 1, 1] |
| 51 | + self.round_num = 0 |
| 52 | + |
| 53 | + def move(self, last_opponent_action): |
| 54 | + choice = self.pattern[(self.round_num + self.random_offset)% len(self.pattern)] |
| 55 | + self.round_num += 1 |
| 56 | + return choice |
| 57 | + |
| 58 | + |
| 59 | +class RandomOpponent(): |
| 60 | + """ |
| 61 | + The classic Nash equilibrium strategy that cannot be exploited |
| 62 | + if we assume symmetrical rewards. |
| 63 | + """ |
| 64 | + def move(self, last_opponent_action): |
| 65 | + return random.randint(0, 2) |
| 66 | + |
| 67 | + |
| 68 | +class PreviousLossOrDrawOpponent(): |
| 69 | + """ |
| 70 | + Play whichever option lost or got a draw last time (so click the red or gray square) |
| 71 | + """ |
| 72 | + def __init__(self): |
| 73 | + self.last_move = None |
| 74 | + |
| 75 | + def move(self, last_opponent_move): |
| 76 | + if self.last_move is None: |
| 77 | + choice = random.randint(0, 2) |
| 78 | + else: |
| 79 | + if last_opponent_move is None: |
| 80 | + choice = random.randint(0, 2) |
| 81 | + elif last_opponent_move == ROCK: |
| 82 | + if self.last_move == ROCK: |
| 83 | + choice = ROCK |
| 84 | + elif self.last_move == PAPER: |
| 85 | + choice = ROCK |
| 86 | + elif self.last_move == SCISSORS: |
| 87 | + choice = SCISSORS |
| 88 | + elif last_opponent_move == PAPER: |
| 89 | + if self.last_move == ROCK: |
| 90 | + choice = ROCK |
| 91 | + elif self.last_move == PAPER: |
| 92 | + choice = PAPER |
| 93 | + elif self.last_move == SCISSORS: |
| 94 | + choice = PAPER |
| 95 | + elif last_opponent_move == SCISSORS: |
| 96 | + if self.last_move == ROCK: |
| 97 | + choice = SCISSORS |
| 98 | + elif self.last_move == PAPER: |
| 99 | + choice = PAPER |
| 100 | + elif self.last_move == SCISSORS: |
| 101 | + choice = SCISSORS |
| 102 | + self.last_move = choice |
| 103 | + return choice |
0 commit comments