Do not modify RPS_game.py. Write all your code in RPS.py. For development, you can use main.py to test your code.
main.py imports the game function and bots from RPS_game.py.
To test your code, play a game with the play function. The play function takes four arguments:
-
two players to play against each other (the players are actually functions)
-
the number of games to play in the match
-
an optional argument to see a log of each game. Set it to
Trueto see these messages.
play(player1, player2, num_games[, verbose])
For example, here is how you would call the function if you want player and quincy to play 1000 games against each other and you want to see the results of each game:
play(player, quincy, 1000, verbose=True)
Testing
The unit tests for this project are in test_module.py. We imported the tests from test_module.py to main.py for your convenience. If you uncomment the last line in main.py, the tests will run automatically whenever you run python main.py in the console.
solution is there
RPS.py
import random
Transition table:
{(prev_move): {next_move: count}}
transition = {
“R”: {“R”: 0, “P”: 0, “S”: 0},
“P”: {“R”: 0, “P”: 0, “S”: 0},
“S”: {“R”: 0, “P”: 0, “S”: 0},
}
opponent_history =
def counter(move):
“”“Return the move that beats the given move.”“”
if move == “R”:
return “P”
if move == “P”:
return “S”
return “R”
def predict_next(last_move):
“”“Predict opponent’s next move based on transition counts.”“”
next_moves = transition[last_move]
return max(next_moves, key=next_moves.get)
def player(prev_play, opponent_history_list=opponent_history):
Initialize first move
if not prev_play:
return “R”
# Record opponent move
opponent_history_list.append(prev_play)
# Update Markov chain
if len(opponent_history_list) > 1:
prev = opponent_history_list[-2]
curr = opponent_history_list[-1]
transition[prev][curr] += 1
# If not enough data, default play
if len(opponent_history_list) < 3:
return random.choice(["R", "P", "S"])
last_move = opponent_history_list[-1]
predicted = predict_next(last_move)
return counter(predicted)