I am trying to simulate the scoring of a squash game using english rules.
These are:
- Only the server is awarded a point if they win a rally.
- If the server wins a rally, they receive a point and continue as server.
- If the returner wins a rally, they become the server but don’t receive a point.
- The first player to reach 9 points wins the game unless the score has reached 8-8.
- If the score reaches 8-8, the player who reached 8 first decides whether to play to 9 or to 10.
The code I have is this:
import random
def eng_game(a,b):
A = 'bob'
B = 'susie'
players = [A, B]
server = random.choice(players)
print server
points_bob = 0
points_susie= 0
prob_A_wins = 0.4
prob_B_wins = 0.6
while points_bob or points_susie < 9:
probability = random.random()
print probability
if probability < prob_A_wins and server == A:
points_bob += 1
elif probability < prob_A_wins and server == B:
server == A
print server
if probability > prob_A_wins and server == B:
points_susie += 1
elif probability > prob_A_wins and server == A:
server == B
print server
print points_bob
print points_susie
This code returns that Susie wins 9-0 when in some cases the server should be swapped to Bob to win the point, but this doesn’t happen. The serve stays with Susie and she wins the point.
I think the issue is the statements
server == Aandserver == Bshould beserver = Aandserver = Bso that assignment takes place instead of a comparison.Another edge case bug I see is if probability ends up exactly 0.4 your program will act like that virtual serve never took place.
I would change your loop to: