Machine Learning with Python Projects - Rock Paper Scissors

Tell us what’s happening:
The problem is with when in main file play(player, kris, 1000) is run it show the result of

Final results: {'p1': 996, 'p2': 4, 'tie': 0}
Player 1 win rate: 99.6%

but when it is run from the test file it fails the test.

FTesting game against kris...
Final results: {'p1': 1, 'p2': 501, 'tie': 498}
Player 1 win rate: 0.199203187250996%

Why is the same code show different result I can’t seem to find out.

Your code so far

def kris_strategy(opponent_history):
  if not opponent_history:
    return "R"
  if opponent_history[-1] == "P":
    return "R"
  if opponent_history[-1] == "S":
    return "P"
  if opponent_history[-1] == "R":
    return "S"

The function kris_strategy returns kris guess.

Replit link

Your browser information:

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

Challenge: Machine Learning with Python Projects - Rock Paper Scissors

Link to the challenge:

A repl will be necessary to debug since the bit of code you posted looks sensible. My guess is that there is a problem with having different initial conditions in these two cases, but that can’t be verified without the rest of the code.

Sorry about that forget the replit link

You seem to be predicting kris’s behavior from his last play while you should be using your last play (it’s possible to store and access your history, too). It also appeared that you were cycling your permutations once too often (too many strategy[...] calls), but that could be due to the first problem (just keep an eye out).

Coupled with how you’re identifying the other player and the initial conditions, it seems this is the problem. You can see the player identification problem if you log from your kris_strategy(); you’ll notice the first 4 trials don’t log. Interestingly the problem manifests as about 50% ties locally while those are losses on replit for reasons I didn’t really investigate; presumably it’s a difference in randomness and locking into a particular Markov chain.

I recommend tracking your history so that you can predict what kris plays and implementing a check to avoid ties. This should push the win percent to near 100% all the time.

1 Like

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