'''lecture_21_racquetball.py Build a terminal-based racquetball game simulation using top-down design Oliver W. Layton CS 151: Computational Thinking: Visual Media Spring 2021 Rules: - Players hit ball back and forth in the count. - Game starts by a player serving, like tennis. - Game played by players hitting ball back and forth (player A hits, then B, then A, then B...) Back and forth is called rally (like in Tennis). - The rally ends when one player fails to hit the ball during a rally - The point ends when the rally ends. - The loser was the one who failed to hit ball back during the rally. - Score only increases by 1 if winner also served. - If the loser was the one who served, the other player serves now. - e.g. if B was serving and fails to hit ball, now A serves. - IMPORTANT: Game ends when one player gets 15 points. - Rule: Player A always starts serving Question: If player A and B played in a championship (many games) who would win? ''' import random def printInstructions(): '''Task 1 from main''' print('This program simulates a game of racquetball between two', 'players called A and B. The abilities of each player is', 'indicated by a probability (number between 0 and 1) that', 'the player wins the point when serving. Player A always', 'has the first serve.') def getUserInput(): '''Task 2 from main''' playerAProbWin = float(input('Enter the chance that Player A wins a point: ')) playerBProbWin = float(input('Enter the chance that Player B wins a point: ')) numGames = int(input('Enter the number of games in the championship: ')) return playerAProbWin, playerBProbWin, numGames def printResults(gamesAWon, gamesBWon): '''Task 4 from main''' totalNumGames = gamesAWon + gamesBWon print('Player A won', gamesAWon, 'games (', 100*gamesAWon/totalNumGames, 'percent of games)') print('Player B won', gamesBWon, 'games (', 100*gamesBWon/totalNumGames, 'percent of games)') if gamesAWon > gamesBWon: print('Player A is the winner!') else: print('Player B is the winner!') def simulateNGames(playerAProbWin, playerBProbWin, numGames): '''Task 3 of main''' gamesAWon = 0 gamesBWon = 0 for g in range(numGames): scoreA, scoreB = simulateOneGame(playerAProbWin, playerBProbWin) # Determine the winner of the game if scoreA > scoreB: gamesAWon = gamesAWon + 1 else: gamesBWon = gamesBWon + 1 return gamesAWon, gamesBWon def simulateOneGame(playerAProbWin, playerBProbWin): scoreA = 0 scoreB = 0 server = 'A' # play the game, have player A serve, play until rally ends, etc. while not gameOver(scoreA, scoreB): if server == 'A': if random.random() < playerAProbWin: # A wins scoreA = scoreA + 1 else: # A loses server = 'B' # B serving else: if random.random() < playerBProbWin: # B wins scoreB = scoreB + 1 else: # B loses server = 'A' return scoreA, scoreB # We need to record who wins particulars and who is serving def main(): # 1. We need to print instructions to the user about what the program is doing # DONE! printInstructions() # 2. We need user input from the Terminal # DONE! playerAProbWin, playerBProbWin, numGames = getUserInput() # 3. Have Player A and Player B play some number of games gamesAWon, gamesBWon = simulateNGames(playerAProbWin, playerBProbWin, numGames) # 4. Print out the results to show how many games Player A and B won (who won the championship) printResults(gamesAWon, gamesBWon) if __name__ == '__main__': main()