473,507 Members | 2,377 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Creating VolleyBall game in python [solved]

12 New Member
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
Expand|Select|Wrap|Line Numbers
  1. #volleyball.py
  2.  
  3. def main ():
  4.     print Intro ()
  5.     probA, probB, n = getInputs ()
  6.     winsA, winsB = simNGames (n, probA, probB)
  7.     printSummary (winsA, winsB)
  8. def Intro ():
  9.     print "This program simulates a gsme of volleyball between"
  10.     print "two players called A and B."
  11.     print "player A always has the first serve"
  12. def getInputs ():
  13.     a = input ("Player A wins a serve")
  14.     b = input ("Player B wins a serve")
  15.     n = input ("How many games do you want to simulate?")
  16.     return a, b, n
  17. def simNGames(n, probA, probB) :
  18.     winsA= winsB = 0
  19.     for i in range(n):
  20.         scoreA, scoreB = simOneGame (probA, probB)
  21.         if scoreA > scoreB:
  22.             winsA =winsA + 1
  23.         else:
  24.             winsB = winB + 1
  25.         return winsA, winsB
  26. def simOneGame (probA, probB):
  27.     serving= "A"
  28.     scoreA = 0
  29.     scoreB = 0
  30.     while not gameOver (scoreA, scoreB):
  31.         if serving == "A":
  32.             if random() < probA:
  33.                 scoreA = scoreA + 1
  34.             else:
  35.                 serving = "B"
  36.         else:
  37.             if random() < probB:
  38.                     scoreB = scoreB + 1
  39.             else:
  40.                      serving = "A"
  41.  
  42.     return scoreA, scoreB
  43. def gameOver(a, b):
  44.     return a==15 or b==15
  45. def printSummary(winsA, winsB):
  46.     n= winsA + winsB
  47.     print "\nGames simulated", n
  48.     print "Wins for A: %d (%0.1f%%)" % (winsA, float(winsA)/n*100)
  49.  
  50. if __name__ == '__main__':main()
Oct 22 '06 #1
9 9585
bartonc
6,596 Recognized Expert Expert
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
Expand|Select|Wrap|Line Numbers
  1. from random import randint
  2. for a in range(10):
  3.     print randint(0, 100) 
  4.  
55
12
55
9
70
61
51
53
24
79

In order to satisfy the last part
Expand|Select|Wrap|Line Numbers
  1. return (a >= 15 and a > (b + 2)) or (b >= 15 and b > (a+2))
  2.  
should be pretty close
Oct 22 '06 #2
kudos
127 Recognized Expert New Member
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

Expand|Select|Wrap|Line Numbers
  1. # playes ONE game
  2. # and some def around it to simulate more 
  3. # games..
  4.  
  5. import random, math
  6.  
  7. server = 0
  8. probA = .7
  9. teamA = 0
  10. teamB = 0
  11.  
  12. while(True):
  13.  if(server == 0):
  14.   print "Team A serves!"
  15.   if(random.random() < probA):
  16.    print "Team A wins the serve!"
  17.    teamA+=1
  18.   else:
  19.    server = 1
  20.  else:
  21.   print "Team B serves!"
  22.   if(random.random()>probA):
  23.    print "Team B wins the serve!"
  24.    teamB+=1
  25.   else:
  26.    server = 0
  27.  
  28.  if(teamA >= 15 or teamB >=15):
  29.   if(abs(teamA - teamB) >=2):
  30.    break;
  31.  
  32.  
  33. print "team A : " + str(teamA)
  34. print "team B : " + str(teamB)

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
Expand|Select|Wrap|Line Numbers
  1. #volleyball.py
  2.  
  3. def main ():
  4.     print Intro ()
  5.     probA, probB, n = getInputs ()
  6.     winsA, winsB = simNGames (n, probA, probB)
  7.     printSummary (winsA, winsB)
  8. def Intro ():
  9.     print "This program simulates a gsme of volleyball between"
  10.     print "two players called A and B."
  11.     print "player A always has the first serve"
  12. def getInputs ():
  13.     a = input ("Player A wins a serve")
  14.     b = input ("Player B wins a serve")
  15.     n = input ("How many games do you want to simulate?")
  16.     return a, b, n
  17. def simNGames(n, probA, probB) :
  18.     winsA= winsB = 0
  19.     for i in range(n):
  20.         scoreA, scoreB = simOneGame (probA, probB)
  21.         if scoreA > scoreB:
  22.             winsA =winsA + 1
  23.         else:
  24.             winsB = winB + 1
  25.         return winsA, winsB
  26. def simOneGame (probA, probB):
  27.     serving= "A"
  28.     scoreA = 0
  29.     scoreB = 0
  30.     while not gameOver (scoreA, scoreB):
  31.         if serving == "A":
  32.             if random() < probA:
  33.                 scoreA = scoreA + 1
  34.             else:
  35.                 serving = "B"
  36.         else:
  37.             if random() < probB:
  38.                     scoreB = scoreB + 1
  39.             else:
  40.                      serving = "A"
  41.  
  42.     return scoreA, scoreB
  43. def gameOver(a, b):
  44.     return a==15 or b==15
  45. def printSummary(winsA, winsB):
  46.     n= winsA + winsB
  47.     print "\nGames simulated", n
  48.     print "Wins for A: %d (%0.1f%%)" % (winsA, float(winsA)/n*100)
  49.  
  50. if __name__ == '__main__':main()
Oct 22 '06 #3
bartonc
6,596 Recognized Expert Expert
Looking up statistics in a book doesn't teach programming.

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

Barton
Oct 22 '06 #4
kudos
127 Recognized Expert New Member
I only said that one could get the answer for this without doing a simulation.
Do I learn programming? what do you mean?


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

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

Barton
Oct 23 '06 #5
bartonc
6,596 Recognized Expert Expert
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
Oct 23 '06 #6
prince99
12 New Member
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
Oct 27 '06 #7
bartonc
6,596 Recognized Expert Expert
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:
Expand|Select|Wrap|Line Numbers
  1. if(teamA >= 15 or teamB >=15):
  2.     if(abs(teamA - teamB) >=2):
  3.         break;
  4.  
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
Oct 27 '06 #8
kudos
127 Recognized Expert New Member
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



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
Oct 27 '06 #9
prince99
12 New Member
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
Oct 28 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2694
by: O'Neal Computer Programmer | last post by:
I was reading here: http://groups.google.com/groups?q=elemental+group:comp.lang.python.*&hl=en&lr=&ie=UTF-8&group=comp.lang.python.*&selm=mailman.1044572235.32593.python-list%40python.org&rnum=3...
1
2469
by: BlackHawke | last post by:
Hello! My name is Nick Soutter, I am the owner of a small game company in San Diego, CA called Aepox Games (www.aepoxgames.net). Our first product, Andromeda Online...
3
2612
by: Sam | last post by:
Hey all, I want to create a computerised version of this game, though I'm not really sure how to go about it. For those who don't know how the game works, here's my attempt at a brief...
4
2702
by: Moosebumps | last post by:
When I have time, I am planning to evaluate Python for console game development (on Playstation 2, GameCube, and Xbox). Does anyone have any experience with this? Pretty much the only resource...
4
2731
by: Edvard Majakari | last post by:
Greetings, fellow Pythonistas! I'm about to create three modules. As an avid TDD fan I'd like to create typical 'use-cases' for each of these modules. One of them is rather large, and I wondered...
0
1259
by: kanedatakeshi | last post by:
Hi folks! The problem is the following: I use Python 2.1 embedded in a C++ game engine. Some of the engines complexer objects (AI, game logic, etc.) use python classes that act as an...
0
2162
by: Michael Goettsche | last post by:
Hi there, for a project in our computer science lessons at school we decided to write a client/server based battleship like game . I know this game could be written without a server, but the...
1
3680
by: Jerry Fleming | last post by:
Hi, I have wrote a game with python curses. The problem is that I want to confirm before quitting, while my implementation doesn't seem to work. Anyone can help me? #!/usr/bin/python # #...
10
1978
by: Michael Lubker | last post by:
Any people that use Python as the predominant language for their game development here? ~Michael
0
7111
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7319
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7031
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7485
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1542
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
412
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.