Rock Paper Scissors

def player(prev_play, opponent_history=):
# Track opponent’s history
if prev_play:
opponent_history.append(prev_play)

# Use a Markov Chain prediction based on the last 2 moves
guess = "R"
if len(opponent_history) < 2:
    return guess

# Build dictionary of patterns (length-2) and what followed
pattern_counts = {}
for i in range(len(opponent_history) - 2):
    key = (opponent_history[i], opponent_history[i + 1])
    next_move = opponent_history[i + 2]
    if key not in pattern_counts:
        pattern_counts[key] = {"R": 0, "P": 0, "S": 0}
    pattern_counts[key][next_move] += 1

# Predict based on the last 2 moves
last_two = tuple(opponent_history[-2:])
if last_two in pattern_counts:
    prediction = max(pattern_counts[last_two], key=pattern_counts[last_two].get)
else:
    prediction = opponent_history[-1]

# Counter the predicted move
counters = {"R": "P", "P": "S", "S": "R"}
return counters[prediction]

Hi, welcome to the forum! :wave:
We see you have posted some code but did you have a question?

(You have not filled out the “Tell us what’s happening:” field above)

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.