Created
May 11, 2024 19:17
-
-
Save johnjosephhorton/89fef16aaa509915f1b0f1e73c058241 to your computer and use it in GitHub Desktop.
repateated game w/ edsl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import textwrap | |
from edsl import QuestionMultipleChoice, Scenario | |
pay_offs = { | |
('red', 'red'):(1, -1), | |
('red', 'black'):(-1, 1), | |
('black', 'red'):(1, -1), | |
('black', 'black'): (-1, 1) | |
} | |
instructions = textwrap.dedent("""\ | |
You are playing a game with another player and you want to win. | |
You can choose to play either a red or a black card. | |
The payoffs are as follows: {{pay_offs}}. | |
""") | |
history = [] | |
player_1_total, player_2_total = 0, 0 | |
for round in range(1, 10): | |
print("Now starting round", round) | |
scenarios = [Scenario({'player': "player 1", "history":history}), Scenario({'player': "player 2", "history":history})] | |
q = QuestionMultipleChoice(question_text = instructions + """You are player {{player}}. The history of the game so far, if any is: {{ history }}. What do you choose?""", | |
question_options = ['red', 'black'], question_name = "choice") | |
results = q.by(scenarios).run() | |
player_1_choice, player_2_choice = list(results.select('answer.*')[0].values())[0] | |
pay_off = pay_offs[(player_1_choice, player_2_choice)] | |
outcome = f"""In round {round}, player 1 chose {player_1_choice} and player 2 chose {player_2_choice}. | |
Player 1's payoff is {pay_off[0]} and player 2's payoff is {pay_off[1]}.""" | |
player_1_total += pay_off[0] | |
player_2_total += pay_off[1] | |
print(outcome) | |
history.append(outcome) | |
print(f"Player 1's total payoff is {player_1_total} and player 2's total payoff is {player_2_total}.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment