RSS Forums RSS
Please support our Python advertiser: Programming Forums
Views: 1060 | Replies: 2
Reply
Join Date: Mar 2007
Location: Australia
Posts: 1
Reputation: bkerr06 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
bkerr06 bkerr06 is offline Offline
Newbie Poster

Help racquetball simulation HELP!

  #1  
Mar 31st, 2007
Hi all,

I've been working on a program that simulates a racquetball match. The problem I am having is getting the match to end when one player is the clear winner of 'n' games. I've tried adding modules, altering loops, adding variables, etc.

Is anyone able to help by pointing me in the right direction? I've spent many days and sleepless nights on this and have just hit a brick wall. I don't want it to get the better of me.
:-|


Here is my code so far:

  1. # rballmatch.py
  2. # Simulation of a racquetball game. .
  3.  
  4. from random import random
  5.  
  6.  
  7. def printIntro():
  8. # Prints an introduction to the program
  9. print "This program simulates a game of racquetball between two"
  10. print 'players called "A" and "B". The abilities of each player is'
  11. print "indicated by a probability (a number between 0 and 1) that"
  12. print "the player wins the point when serving. Player A always"
  13. print "has the first serve.\n"
  14.  
  15. def getInputs():
  16. # Returns probA, probB, number of games to simulate
  17. a = input("What is the prob. player A wins a serve? ")
  18. b = input("What is the prob. player B wins a serve? ")
  19. n = input("How many games to simulate? ")
  20. return a, b, n
  21.  
  22. def simNGames(n, probA, probB):
  23. # Simulates n games of racquetball between players whose
  24. # abilities are represented by the probability of winning a serve.
  25. # Returns number of wins for A and B
  26. winsA = winsB = 0
  27. if (winsA) or (winsB) = ((n / 2) + 1:
  28. break
  29. else:
  30. for i in range(n):
  31. scoreA, scoreB = simOneGame(probA, probB)
  32. if scoreA > scoreB:
  33. winsA = winsA + 1
  34. else:
  35. winsB = winsB + 1
  36. return winsA, winsB
  37.  
  38. def simOneGame(probA, probB):
  39. # Simulates a single game of racquetball between two players whose
  40. # abilities are represented by the probability of winning a serve.
  41. # Returns final scores for A and B
  42. serving = "A"
  43. scoreA = 0
  44. scoreB = 0
  45. while not gameOver(scoreA, scoreB):
  46. if serving == "A":
  47. if random() < probA:
  48. scoreA = scoreA + 1
  49. else:
  50. serving = "B"
  51. else:
  52. if random() < probB:
  53. scoreB = scoreB + 1
  54. else:
  55. serving = "A"
  56. return scoreA, scoreB
  57.  
  58. def gameOver(a,b):
  59. # a and b are scores for players in a racquetball game
  60. # Returns true if game is over, false otherwise
  61.  
  62. return a == 15 or b == 15 and ((a - b) > 2 ) or ((a - b) < -2)
  63.  
  64. def printSummary(winsA, winsB):
  65. # Prints a summary of wins for each player.
  66. n = winsA + winsB
  67. print "\nGames simulated:", n
  68. print "Wins for A: %d (%0.1f%%)" % (winsA, float(winsA)/n*100)
  69. print "Wins for B: %d (%0.1f%%)" % (winsB, float(winsB)/n*100)
  70.  
  71. def main():
  72. printIntro()
  73. probA, probB, n = getInputs()
  74. winsA, winsB = simNGames(n, probA, probB)
  75. printSummary(winsA, winsB)
  76.  
  77.  
  78. if __name__ == "__main__": main()
  79.  
  80.  
  81.  
  82.  
  83.  
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2006
Posts: 1,669
Reputation: sneekula is on a distinguished road 
Rep Power: 6
Solved Threads: 47
sneekula's Avatar
sneekula sneekula is offline Offline
Posting Virtuoso

Re: racquetball simulation HELP!

  #2  
Apr 1st, 2007
One of your problems is right here:
    if (winsA) or (winsB) == ((n / 2) + 1):
        break
You have to change the if statement logic a bit, and you are using break when you are clearly not in a loop.
No one died when Clinton lied.
Reply With Quote  
Join Date: Mar 2007
Location: Georgia
Posts: 13
Reputation: liz517 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
liz517 liz517 is offline Offline
Newbie Poster

Re: racquetball simulation HELP!

  #3  
Apr 4th, 2007
  1. def gameOver(a,b):
  2. # a and b are scores for players in a racquetball game
  3. # RETURNS true if game is over, false otherwise
  4. return a == 15 or b == 15 and abs(a-b)>= 2
try this
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 12:33 pm.
Newsletter Archive - Sitemap - Privacy Statement - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC