Election
Election
Let’s implement a game called Election. In this game, two players compete to try and earn the most votes. Both players start with 0 votes and 100 popularity.
The two players alternate turns, and the first player starts. Each turn, the current player chooses an action. There are two types of actions:
- The player can debate, and either gain or lose 50 popularity. If the player has popularity p1 and the other player has popularity p2, then the probability that the player gains 50 popularity is max(0.1, p1 / (p1 + p2)) Note that the max causes the probability to never be lower than 0.1.
- The player can give a speech. If the player has popularity p1 and the other player has popularity p2, then the player gains p1 // 10 votes and popularity and the other player loses p2 // 10 popularity.
The game ends when a player reaches 50 votes, or after a total of 10 turns have been played (each player has taken 5 turns). Whoever has more votes at the end of the game is the winner!
Question 2: Player
First, let’s implement the Player class. Fill in the debate and speech methods, that take in another Player other, and implement the correct behavior as detailed above. Here are two additional things to keep in mind:
- In the debate method, you should call the provided random function, which returns a random float between 0 and 1. The player should gain 50 popularity if the random number is smaller than the probability described above, and lose 50 popularity otherwise.
- Neither players’ votes or popularity should ever become negative. If this happens, set it equal to 0 instead.
### Phase 1: The Player Class class Player: """ >>> reset_random() >>> p1 = Player('Hill') >>> p2 = Player('Don') >>> p1.popularity 100 >>> p1.debate(p2) # random() should return 0.0 >>> p1.popularity 150 >>> p2.popularity 100 >>> p2.votes 0 >>> p2.speech(p1) >>> p2.votes 10 >>> p2.popularity 110 >>> p1.popularity 135 >>> # Additional correctness tests >>> p1.speech(p2) >>> p1.votes 13 >>> p1.popularity 148 >>> p2.votes 10 >>> p2.popularity 99 >>> for _ in range(4): # 0.1, 0.2, 0.3, 0.4 ... p1.debate(p2) >>> p2.debate(p1) >>> p2.popularity 49 >>> p2.debate(p1) >>> p2.popularity 0 """ def __init__(self, name): self.name = name self.votes = 0 self.popularity = 100 def debate(self, other): "*** YOUR CODE HERE ***" def speech(self, other): "*** YOUR CODE HERE ***" def choose(self, other): return self.speech