Machine Learning with Python Projects - Rock Paper Scissors

Tell us what’s happening:
I just cannot get most of these over 60% no matter how hard i try or how detailed i make the algorithm, and nothing is working. If anyone can suggest any potential solutions, please do, it would help so much. Thanks!

Your code so far
import random

class LearningBot:
def init(self):
self.opponent_history =
self.move_distribution = {‘R’: 0, ‘P’: 0, ‘S’: 0}
self.markov_chain = {}

def update_history(self, prev_play):
    self.opponent_history.append(prev_play)

def update_move_distribution(self):
    for move in self.opponent_history:
        if move:
            self.move_distribution[move] += 1

def update_markov_chain(self):
    if len(self.opponent_history) >= 4:
        past_moves = tuple(self.opponent_history[-4:])
        current_move = self.opponent_history[-1]
        
        if past_moves not in self.markov_chain:
            self.markov_chain[past_moves] = {'R': 0, 'P': 0, 'S': 0}
        self.markov_chain[past_moves][current_move] += 1

def predict_opponent_move(self):
    if len(self.opponent_history) >= 4:
        past_moves = tuple(self.opponent_history[-4:])
        next_move_distribution = self.markov_chain.get(past_moves, None)
        
        if next_move_distribution:
            max_count = max(next_move_distribution.values())
            predicted_moves = [move for move, count in next_move_distribution.items() if count == max_count]
            return random.choice(predicted_moves)
    
    # If not enough history for Markov Chain, fall back to counter strategy
    max_count = max(self.move_distribution.values())
    predicted_moves = [move for move, count in self.move_distribution.items() if count == max_count]
    return random.choice(predicted_moves)

def player(prev_play, opponent_history=):
if prev_play:
opponent_history.append(prev_play)

bot = LearningBot()
bot.update_history(prev_play)
bot.update_move_distribution()
bot.update_markov_chain()

# Opponent-specific strategies
if 'PP' in ''.join(opponent_history[-4:]):
    my_move = 'S'  # Counter Abbey's PP pattern
elif 'SS' in ''.join(opponent_history[-4:]):
    my_move = 'R'  # Counter Kris's SS pattern
else:
    predicted_move = bot.predict_opponent_move()
    if 'abbey' in opponent_history[-4:]:
        # Adjust strategy against Abbey
        my_move = 'P' if predicted_move == 'S' else 'R'
    elif 'kris' in opponent_history[-4:]:
        # Adjust strategy against Kris
        my_move = 'S' if predicted_move == 'R' else 'P'
    else:
        # General counter strategy
        counter_moves = {'R': 'P', 'P': 'S', 'S': 'R'}
        my_move = counter_moves[predicted_move]

return my_move

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36

Challenge: Machine Learning with Python Projects - Rock Paper Scissors

Link to the challenge:

Can I interpreted my language in python?

i don’t know what you mean by that but sure

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