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
aModify 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 and Use these player indices to refer to players inside the play method this feature will be used in Problem Set up rounds of a game between two identical random choice players Output the winning scores at the end.
b 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?
cSet up another player type called TitForTat. This player initially cooperates, but then takes the same action as the opponent's last action.
dCome up with your own strategy for a player and set them up
e Play your player against the TitForTat player. Create a plot which shows total points after games for each player. Let's run multiple game sessions. Run sessions, starting from zero each time and clearing the player histories. Plot the points won by each player versus the session number.
aSet 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 numturns games and then save the results in a results array, where the row and column specify the index of the players playing. The parameter numturns can be passed in to the tournament method default
bSet up two additional players: a player who always cooperates and a player who always defects.
cDefine a player which is slower to respond to the Opponent than TitForTat.
dPlay the players against each other. Sum up their winnings at the end and plot as a bar graph. On the xaxis label each player type. You should have at least six player types, but you can include more if you wish.
class Player:
def initself:
self.coop
self.defect
self.ownchoicehistory
self.opponentchoicehistory
self.scorehistory
def chooseself:
action nprandom.randint
return action
def updatescoreselfscore:
self.scorehistory.appendscore
def updatechoicehistoryselfselfchoice,otherchoice:
self.ownchoicehistory.appendselfchoice
self.opponentchoicehistory.appendotherchoice
def gettotalself:
scores npsumselfscorehistory
return scores
class Game:
def initself:
self.numplayers
self.players
self.sucker
self.winner
self.coop
self.defector
self.players.appendFlipper
self.players.appendTitForTat
def playself:
c self.playerschoose
c self.playerschoose
if c:
if c:
p self.sucker
p self.winner
else:
p self.coop
p self.coop
else:
if c:
p self.defector
p self.defector
else:
p self.winner
p self.sucker
self.playersupdatescorep
self.playersupdatescorep
self.playersupdatechoicehistorycc
self.playersupdatechoicehistorycc
def finaltallyself:
tallies npzerosselfnumplayers
i
for player in self.players:
talliesi player.gettotal
i
return tallies
game Game
numturns
for i in rangenumturns:
gameplay
printgamefinaltally
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
