-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRPS.py
More file actions
26 lines (22 loc) · 841 Bytes
/
Copy pathRPS.py
File metadata and controls
26 lines (22 loc) · 841 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
def player(prev_play, opponent_history=[], ngram_model={}):
import random
from collections import defaultdict
if prev_play:
opponent_history.append(prev_play)
n = 3
if len(opponent_history) >= n + 1:
key = ''.join(opponent_history[-n - 1:-1])
next_move = opponent_history[-1]
if key not in ngram_model:
ngram_model[key] = defaultdict(int)
ngram_model[key][next_move] += 1
if len(opponent_history) >= n:
key = ''.join(opponent_history[-n:])
if key in ngram_model:
prediction = max(ngram_model[key], key=ngram_model[key].get)
else:
prediction = random.choice(['R', 'P', 'S'])
else:
prediction = random.choice(['R', 'P', 'S'])
counter = {'R': 'P', 'P': 'S', 'S': 'R'}
return counter[prediction]