Creating VolleyBall game in python [solved]

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • prince99
    New Member
    • Oct 2006
    • 12

    #1

    Creating VolleyBall game in python [solved]

    Q) Design and implement a volleyball simulation. In volleyball, only the serving team can score, and if the receiving team win the rally, they gain the serve. Games are played to a score of 15, but must be won by at least 2 points. If the scores are closer, the game can continue indefinitely.

    Code given below plz correct it
    Code:
    #volleyball.py
    
    def main ():
        print Intro ()
        probA, probB, n = getInputs ()
        winsA, winsB = simNGames (n, probA, probB)
        printSummary (winsA, winsB)
    def Intro ():
        print "This program simulates a gsme of volleyball between"
        print "two players called A and B."
        print "player A always has the first serve"
    def getInputs ():
        a = input ("Player A wins a serve")
        b = input ("Player B wins a serve")
        n = input ("How many games do you want to simulate?")
        return a, b, n
    def simNGames(n, probA, probB) :
        winsA= winsB = 0
        for i in range(n):
            scoreA, scoreB = simOneGame (probA, probB)
            if scoreA > scoreB:
                winsA =winsA + 1
            else:
                winsB = winB + 1
            return winsA, winsB
    def simOneGame (probA, probB):
        serving= "A"
        scoreA = 0
        scoreB = 0
        while not gameOver (scoreA, scoreB):
            if serving == "A":
                if random() < probA:
                    scoreA = scoreA + 1
                else:
                    serving = "B"
            else:
                if random() < probB:
                        scoreB = scoreB + 1
                else:
                         serving = "A"
                
        return scoreA, scoreB
    def gameOver(a, b):
        return a==15 or b==15
    def printSummary(winsA, winsB):
        n= winsA + winsB
        print "\nGames simulated", n
        print "Wins for A: %d (%0.1f%%)" % (winsA, float(winsA)/n*100)
    
    if __name__ == '__main__':main()
    Last edited by bartonc; May 18 '07, 05:39 AM. Reason: added [code][/code] tags
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    I won't format you code again!!! USE CODE TAGS!!!

    That said, your program look pretty good, but you need to understand the difference between built-in python functions and library modules. For example, print is built-in

    >>> print "hello"
    hello

    random is a module in the library that must be imported.

    >>> random()
    Traceback (most recent call last):
    File "<pyshell#0 >", line 1, in -toplevel-
    random()
    NameError: name 'random' is not defined

    actually, you want one of the functions in that library so you can use
    Code:
    from random import randint
    for a in range(10):
    	print randint(0, 100)
    55
    12
    55
    9
    70
    61
    51
    53
    24
    79

    In order to satisfy the last part
    Code:
    return (a >= 15 and a > (b + 2)) or (b >= 15 and b > (a+2))
    should be pretty close

    Comment

    • kudos
      Recognized Expert New Member
      • Jul 2006
      • 127

      #3
      here is some code to get you started...
      (but do you really need to write a program to get the answer for this, or would it be simpler to just look up a statistics book????

      -kudos

      Code:
      # playes ONE game
      # and some def around it to simulate more 
      # games..
      
      import random, math
      
      server = 0
      probA = .7
      teamA = 0
      teamB = 0
      
      while(True):
       if(server == 0):
        print "Team A serves!"
        if(random.random() < probA):
         print "Team A wins the serve!"
         teamA+=1
        else:
         server = 1
       else:
        print "Team B serves!"
        if(random.random()>probA):
         print "Team B wins the serve!"
         teamB+=1
        else:
         server = 0
      
       if(teamA >= 15 or teamB >=15):
        if(abs(teamA - teamB) >=2):
         break;
         
         
      print "team A : " + str(teamA)
      print "team B : " + str(teamB)

      Originally posted by prince99
      Q) Design and implement a volleyball simulation. In volleyball, only the serving team can score, and if the receiving team win the rally, they gain the serve. Games are played to a score of 15, but must be won by at least 2 points. If the scores are closer, the game can continue indefinitely.

      Code given below plz correct it
      Code:
      #volleyball.py
      
      def main ():
          print Intro ()
          probA, probB, n = getInputs ()
          winsA, winsB = simNGames (n, probA, probB)
          printSummary (winsA, winsB)
      def Intro ():
          print "This program simulates a gsme of volleyball between"
          print "two players called A and B."
          print "player A always has the first serve"
      def getInputs ():
          a = input ("Player A wins a serve")
          b = input ("Player B wins a serve")
          n = input ("How many games do you want to simulate?")
          return a, b, n
      def simNGames(n, probA, probB) :
          winsA= winsB = 0
          for i in range(n):
              scoreA, scoreB = simOneGame (probA, probB)
              if scoreA > scoreB:
                  winsA =winsA + 1
              else:
                  winsB = winB + 1
              return winsA, winsB
      def simOneGame (probA, probB):
          serving= "A"
          scoreA = 0
          scoreB = 0
          while not gameOver (scoreA, scoreB):
              if serving == "A":
                  if random() < probA:
                      scoreA = scoreA + 1
                  else:
                      serving = "B"
              else:
                  if random() < probB:
                          scoreB = scoreB + 1
                  else:
                           serving = "A"
                  
          return scoreA, scoreB
      def gameOver(a, b):
          return a==15 or b==15
      def printSummary(winsA, winsB):
          n= winsA + winsB
          print "\nGames simulated", n
          print "Wins for A: %d (%0.1f%%)" % (winsA, float(winsA)/n*100)
      
      if __name__ == '__main__':main()

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Looking up statistics in a book doesn't teach programming.

        Looks like you learning! Keep it up! Have fun!

        Barton

        Comment

        • kudos
          Recognized Expert New Member
          • Jul 2006
          • 127

          #5
          I only said that one could get the answer for this without doing a simulation.
          Do I learn programming? what do you mean?


          Originally posted by bartonc
          Looking up statistics in a book doesn't teach programming.

          Looks like you learning! Keep it up! Have fun!

          Barton

          Comment

          • bartonc
            Recognized Expert Expert
            • Sep 2006
            • 6478

            #6
            Originally posted by kudos
            I only said that one could get the answer for this without doing a simulation.
            Do I learn programming? what do you mean?
            Sorry, I didn't notice that you had jumped in on this thread. I was writing to the original poster of this thread. Thanks for keeping an eye on this forum and contributing. We need all the help we can get!
            Barton

            Comment

            • prince99
              New Member
              • Oct 2006
              • 12

              #7
              if(teamA >= 15 or teamB >=15):
              if(abs(teamA - teamB) >=2):
              break;

              this code is not working, because the qestion says that the teams should be loss by only 2 points, but result goes other way. The program wins the game for team A but it wins by many points

              i want to get something like if the winning team scored 15 and win the other team should atleast score 13. it will make 2 points different only.

              any help?

              thanx

              Prince

              Comment

              • bartonc
                Recognized Expert Expert
                • Sep 2006
                • 6478

                #8
                Originally posted by prince99
                if(teamA >= 15 or teamB >=15):
                if(abs(teamA - teamB) >=2):
                break;

                this code is not working, because the qestion says that the teams should be loss by only 2 points, but result goes other way. The program wins the game for team A but it wins by many points

                i want to get something like if the winning team scored 15 and win the other team should atleast score 13. it will make 2 points different only.

                any help?

                thanx

                Prince
                Prince, Please start using code tags - READ THE STICKY (first post) at the top of this forum - so your post looks like this:
                Code:
                if(teamA >= 15 or teamB >=15):
                	if(abs(teamA - teamB) >=2):
                		break;
                I ran the code that kudos gave you with probA = .5. It works for me.
                Thanks for using code tags in your next post,
                python forum moderator,
                Barton

                Comment

                • kudos
                  Recognized Expert New Member
                  • Jul 2006
                  • 127

                  #9
                  Didn't you say that one of the teams should win by atleast two points? Also, isn't it a bit odd (as a game) the constraint that the winning team should have 15 points, and the loosing team should have no less than 13 points?

                  -kudos



                  Originally posted by prince99
                  if(teamA >= 15 or teamB >=15):
                  if(abs(teamA - teamB) >=2):
                  break;

                  this code is not working, because the qestion says that the teams should be loss by only 2 points, but result goes other way. The program wins the game for team A but it wins by many points

                  i want to get something like if the winning team scored 15 and win the other team should atleast score 13. it will make 2 points different only.

                  any help?

                  thanx

                  Prince

                  Comment

                  • prince99
                    New Member
                    • Oct 2006
                    • 12

                    #10
                    yes that's right but i dunno some how its not working correctly, but anyway guys thanx for your help and time. I really appreciate you guys help

                    i got a better idea now and will try to solve it out

                    thanx a lot

                    Comment

                    Working...