-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathexercise_1.py
99 lines (88 loc) · 2.94 KB
/
exercise_1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from random import random
def main():
printIntro()
probA, probB, n = getInputs()
matchA, matchB = simNMatches(n, probA, probB)
printSummary(matchA, matchB)
def printIntro():
print("This program simulates a game of racquetball between two")
print('players called "A" and "B". The abilities of each player is')
print("indicated by a probability (a number between 0 and 1) that")
print("reflects the likelihood of a player winning the serve.")
print("Player A has the first serve.")
def getInputs():
#Returns the three simulation parameters
a = eval(input("What is the prob. player A wins a serve? "))
b = eval(input("What is the prob. player B wins a serve? "))
n = eval(input("How many games to simulate? "))
return a, b, n
def simNMatches(n, probA, probB):
#Simulates n Matches of racquetball between players whos
# abilities are represented by the probability of winning a serve.
#Returns number of match wins for A and B. Matches are determined
#by best of 3 games. Service alternates per game
matchA = matchB = 0
for i in range(n):
winsA, winsB = simOneMatch(probA, probB)
if winsA > winsB:
matchA = matchA + 1
else:
matchB = matchB + 1
return matchA, matchB
def simOneMatch(probA, probB):
winsA = winsB = 0
#accumulate number of games
x = 1
while winsA !=2 and winsB !=2:
scoreA, scoreB = simOneGame(probA, probB, x)
if scoreA > scoreB:
winsA = winsA + 1
x = x+1
else:
winsB = winsB + 1
x = x+1
return winsA, winsB
def simOneGame(probA, probB, x):
#Simulates a single game of racquetball between players whoe
# abilities are represented by the probability of winning a serve.
#Returns final scores for A and B
serving = findService(x)
scoreA = 0
scoreB = 0
while not gameOver(scoreA, scoreB):
if serving == "A":
if random() < probA:
scoreA = scoreA + 1
else:
serving = "B"
elif serving == "B":
if random() < probB:
scoreB = scoreB + 1
else:
serving = "A"
return scoreA, scoreB
def findService(x):
if x % 2 == 0:
return "A"
else:
return "B"
def gameOver(a, b):
#a and b represent scores for a racquetball game
#Returns True if the game is over, False otherwise
#Must win by 2
#7-0 is a shutout
if a == 0 and b == 7:
return b == 7
elif b == 0 and a == 7:
return a == 7
elif abs(a-b) >= 2:
return True
else:
return False
def printSummary(matchA, matchB):
# Prints a summary of wins for each players
n = matchA + matchB
print("\nGames simulated: ", n)
print("Wins for A: {0} ({1:0.1%})".format(matchA, matchA/n))
print("Wins for B: {0} ({1:0.1%})".format(matchB, matchB/n))
if __name__ == '__main__': main()