Created
July 5, 2020 18:04
-
-
Save iliakonnov/9562bc4cef89060aeca97d4ff8fb4b4a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
ROCK = 1 | |
SCISSORS = 2 | |
PAPER = 3 | |
DRAW = 'Draw.' | |
HUMAN = 'Human' | |
BOT = 'BIP-BOP' | |
wins = { | |
ROCK: SCISSORS, | |
SCISSORS: PAPER, | |
PAPER: ROCK | |
} | |
reverse = { | |
SCISSORS: ROCK, | |
PAPER: SCISSORS, | |
ROCK: PAPER | |
} | |
def most_common(lst): | |
if not lst: | |
return None | |
return max(set(lst), key=lst.count) | |
history = [] | |
def get_turn(): | |
N = 5 | |
humans_like = most_common([i for i, _, _ in history][-N:]) | |
if humans_like is None: | |
return random.choice((ROCK, SCISSORS, PAPER)) | |
else: | |
return reverse[humans_like] | |
def main(): | |
while True: | |
res = DRAW | |
while res == DRAW: | |
human = input('> ') | |
if not human or not human.isalnum(): | |
print(' Oops') | |
continue | |
human = int(human) | |
if human not in wins: | |
print(' Oops') | |
continue | |
bot = get_turn() | |
res = None | |
if human == bot: | |
res = DRAW | |
elif wins[human] == bot: | |
res = HUMAN | |
else: | |
res = BOT | |
print(f' {bot}') | |
history.append((human, bot, res)) | |
hum_wins = sum(1 for _, _, i in history if i == HUMAN) | |
bot_wins = sum(1 for _, _, i in history if i == BOT) | |
print(f'[H:{hum_wins} | B:{bot_wins}] {res}') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment