Skip to content

Instantly share code, notes, and snippets.

@iliakonnov
Created July 5, 2020 18:04
Show Gist options
  • Save iliakonnov/9562bc4cef89060aeca97d4ff8fb4b4a to your computer and use it in GitHub Desktop.
Save iliakonnov/9562bc4cef89060aeca97d4ff8fb4b4a to your computer and use it in GitHub Desktop.
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