473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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
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 9562
bartonc
6,596 Expert 4TB
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 Expert 100+
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 Expert 4TB
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 Expert 100+
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 Expert 4TB
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
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 Expert 4TB
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 Expert 100+
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
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
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
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
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
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
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
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
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
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
by: Michael Lubker | last post by:
Any people that use Python as the predominant language for their game development here? ~Michael
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.