Question: Please use python 1 a ) Modify the Game class so it takes as input two player classes. These will be assigned as players in

Please use python
1a)Modify the Game class so it takes as input two player classes. These will be assigned as players in Game. Modify also the play method so that it optionally takes arguments for the player indices (default 0 and 1). Use these player indices to refer to players inside the play method (this feature will be used in Problem 2). Set up 100 rounds of a game between two identical random choice players . Output the winning scores at the end.
1b) Using inheritance define a new type of player. This player should alternate their choice (so if one round they defect, the next round they cooperate and so
on). You can call this player Flipper. Implement the game between this player and the player who randomly chooses their action each time. Is there a difference between the strategies?
1c)Set up another player type called TitForTat. This player initially cooperates, but then takes the same action as the opponent's last action.
1d)Come up with your own strategy for a player and set them up.
1e) Play your player against the TitForTat player. Create a plot which shows total points after 100 games for each player. Let's run multiple 100 game sessions. Run 10 sessions, starting from zero each time and clearing the player histories. Plot the points won by each player versus the session number.
2a)Set up a new class MultiPlayerGame which inherits from Game . Modify the method so that it now takes a list of players as input. Use the operator so that it can take any arbitrary length list, and perform the appropriate update inside the method. Add also two methods: a reset method that clears the history and scores of all players; and a tournament method that plays each player against each other player. Have each player perform num_turns =100 games and then save the results in a results array, where the row and column specify the index of the players playing. The parameter num_turns can be passed in to the tournament method (default 100).
2b)Set up two additional players: a player who always cooperates and a player who always defects.
2c)Define a player which is slower to respond to the Opponent than TitForTat.
2d)Play the players against each other. Sum up their winnings at the end and plot as a bar graph. On the x-axis label each player type. You should have at least six player types, but you can include more if you wish.
class Player:
def __init__(self):
self.coop =1
self.defect =0
self.own_choice_history =[]
self.opponent_choice_history =[]
self.score_history =[]
def choose(self):
action = np.random.randint(0,2)
return action
def update_score(self,score):
self.score_history.append(score)
def update_choice_history(self,self_choice,other_choice):
self.own_choice_history.append(self_choice)
self.opponent_choice_history.append(other_choice)
def get_total(self):
scores = np.sum(self.score_history)
return scores
class Game:
def __init__(self):
self.num_players =2
self.players =[]
self.sucker =0
self.winner =5
self.coop =3
self.defector =1
self.players.append(Flipper())
self.players.append(TitForTat())
def play(self):
c1= self.players[0].choose()
c2= self.players[1].choose()
if c1==1:
if c2==0:
p1= self.sucker
p2= self.winner
else:
p1= self.coop
p2= self.coop
else:
if c2==0:
p1= self.defector
p2= self.defector
else:
p1= self.winner
p2= self.sucker
self.players[0].update_score(p1)
self.players[1].update_score(p2)
self.players[0].update_choice_history(c1,c2)
self.players[1].update_choice_history(c2,c1)
def final_tally(self):
tallies = np.zeros(self.num_players)
i =0
for player in self.players:
tallies[i]= player.get_total()
i+=1
return tallies
game1= Game()
num_turns =100
for i in range(num_turns):
game1.play()
print(game1.final_tally())

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Finance Questions!