Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |
 User Name: Password: Remember me

New Free Tools on Dev Shed!
We're Excited to announce that Dev Shed now has 70 free tools on the site. To learn more, click here!

 Dev Shed Forums Sponsor:
#1
January 3rd, 2013, 02:41 PM
 jeppsson
Registered User

Join Date: Jan 2013
Posts: 5
Time spent in forums: 1 h 2 m 17 sec
Reputation Power: 0
Python - yahtzee game!

Hi, i am currently working on a program in python which is supposed to make a protocol for yahtzee where players can insert their scores. This is what i've got so far:

grupper = ["Ones", "Twos", "Threes","Fours","Fives","Sixes","Over sum","Bonus","One pair","Two paires","Three","Four","Small ladder","Big ladder", "Full house", "Chance", "Yatzy"]

class Spelare():

def __init__(self, namn):
self.namn = namn
self.points = []
for a in range(len(grupper)):
self.points.append(0)

#Calculates the total sum
def summa(self):
summa = 0
for a in range(len(grupper)):
summa += self.points[a]
return summa

#Calculates the sum of the first six categories
def övresumma(self):
övresumma = 0

mindre = 0
while (mindre < 6):
övresumma += self.points[mindre]
mindre += 1
return övresumma

#Calculates a bonus which is 50 point if the over sum is over 63 points
def bonus(self):
sumover = 0

category = 0
while(category < 6):
sumover += self.points[category]
category += 1

if(sumover >= 63):
for category in range(len(grupper)):
if(grupper[category] == "Bonus"):
self.points[category] = 50
break
#Here is where the game starts

players = []
print ("Welcome to Yahtzee!")

antal = int(input("How many players?: "))

for i in range(antal):
players.append(Spelare(str(input("Names %d: " % (i + 1)))))

def printTable():

print("Namn", end="\t\t")

for spelare in players:
print(spelare.namn , end="\t")
print()

for antal in range(len(grupper)):
print(grupper[antal], end="\t\t")
for spelare in players:
print(spelare.points[antal], end="\t")
print()

print("Summa", end="\t\t")

for spelare in players:
#print (spelare.points)
print(spelare.summa(), end="\t")
print()

varv = 0
while(varv < 2):

for spelare in players:

print()
printTable()

print()
print("Now its %ss turn to play\n" % spelare.namn)

i = -1
while (i < 0):
category = str(input("Which category? "))

for antal in range(len(grupper)):
if grupper[antal] == category:
i = antal
if i < 0:
print("%s är inte ett giltigt moment. Försök igen.\n" % category)

points = int(input("How many points? (-1 to strike one) "))

spelare.points[i] += points

print("Okej, protokollet har uppdaterats.")

varv += 1
#varv <2

for spelare in players:
spelare.bonus()

print("Det har gått 15 rundor, spelet är slut!")
printTable()

What im having a problem with is making the protocol update itself with both bonus, over sum and total sum after each new player... If anyone would be able to help me i would be so grateful!!

#2
January 3rd, 2013, 04:38 PM
 StevenJM
Registered User

Join Date: Dec 2012
Posts: 12
Time spent in forums: 3 h 51 m 27 sec
Reputation Power: 0
I'll have a look later, but could you update your code with code tags?

i.e. repaste the code in between [code ] and [/code ] (omit the space between e and ])

#3
January 3rd, 2013, 04:48 PM
 jeppsson
Registered User

Join Date: Jan 2013
Posts: 5
Time spent in forums: 1 h 2 m 17 sec
Reputation Power: 0
Quote:
 Originally Posted by StevenJM I'll have a look later, but could you update your code with code tags? i.e. repaste the code in between [code ] and [/code ] (omit the space between e and ])

Thank you so much! I'm sorry i don't really understand how you want me to change it with code tags, i've never used that before .. :/

#4
January 3rd, 2013, 05:03 PM
 StevenJM
Registered User

Join Date: Dec 2012
Posts: 12
Time spent in forums: 3 h 51 m 27 sec
Reputation Power: 0
The code tags preserve indents, which make code much more readable. You need to put [code ] at the beginning of your code and [/code ] (again without the spaces... If I didn't include those, the words "at the beginning of your code and" would appear as code) at the end of it. This preserves the formatting. Editing the original post won't do the job, though, as the formatting has already been removed. So just create a reply and paste your code with the tags

#5
January 3rd, 2013, 05:31 PM
 jeppsson
Registered User

Join Date: Jan 2013
Posts: 5
Time spent in forums: 1 h 2 m 17 sec
Reputation Power: 0
Like this?

Code:
`grupper = ["Ones", "Twos","Threes","Foures","Fives","Sixes","Oversum","Bonus","Onepair","Twopair", "Thee", "Four", "Smallladder","Bigladder","Chance", "Yatzy"]`

class Spelare():

Code:
```def __init__(self, namn):
self.namn = namn
self.points = []
for a in range(len(grupper)):
self.points.append(0)```

#Calculates total sum
Code:
```def summa(self):
summa = 0
for a in range(len(grupper)):
summa += self.points[a]
return summa```

#Calculates over sum, first six categories
Code:
``` def övresumma(self):
övresumma = 0

mindre = 0
while (mindre < 6):
övresumma += self.points[mindre]
mindre += 1
return övresumma ```

#Calculates bonus
Code:
``` def bonus(self):
sumover = 0

category = 0
while(category < 6):
sumover += self.points[category]
category += 1

if(sumover >= 63):
for category in range(len(grupper)):
if(grupper[category] == "Bonus"):
self.points[category] = 50
break```

#Game Starts

Code:
```players = []
print ("Välkomna till Yatzy!")```

Code:
`antal = int(input("Hur många är det som vill spela?: "))`

Code:
```or i in range(antal):
players.append(Spelare(str(input("Ange namn för spelare %d: " % (i + 1)))))```

Code:
```def printTable():

print("Namn", end="\t\t")

for spelare in players:
print(spelare.namn , end="\t")
print()

for antal in range(len(grupper)):
print(grupper[antal], end="\t\t")
for spelare in players:
print(spelare.points[antal], end="\t")
print()

print("Summa", end="\t\t")

for spelare in players:
print(spelare.summa(), end="\t")
print()```

Code:
`varv = 0`

Code:
`while(varv < 2):`

for spelare in players:

print()
printTable()

print()
print("Nu är det %ss tur att spela\n" % spelare.namn)

i = -1
while (i < 0):
category = str(input("Vilket moment? "))

for antal in range(len(grupper)):
if grupper[antal] == category:
i = antal
if i < 0:
print("%s är inte ett giltigt moment. Försök igen.\n" % category)

points = int(input("Hur många poäng? (-1 för att stryka) "))

spelare.points[i] += points
spelare.points[6] += points

print("Okej, protokollet har uppdaterats.")[/code]

Code:
` varv += 1`

#varv <2

Code:
```for spelare in players:
spelare.bonus()```

Code:
`print("Det har gått 15 rundor, spelet är slut!")`

Code:
`printTable()`

#6
January 3rd, 2013, 05:41 PM
 StevenJM
Registered User

Join Date: Dec 2012
Posts: 12
Time spent in forums: 3 h 51 m 27 sec
Reputation Power: 0
Sort of...

Code:
```grupper = ["Ones", "Twos","Threes","Foures","Fives","Sixes","Oversum","Bonus","Onepair","Twopair", "Thee", "Four", "Smallladder","Bigladder","Chance", "Yatzy"

class Spelare():

def __init__(self, namn):
self.namn = namn
self.points = []
for a in range(len(grupper)):
self.points.append(0)

#Calculates total sum
def summa(self):
summa = 0
for a in range(len(grupper)):
summa += self.points[a]
return summa

#Calculates over sum, first six categories
def övresumma(self):
övresumma = 0

mindre = 0
while (mindre < 6):
övresumma += self.points[mindre]
mindre += 1
return övresumma

#Calculates bonus
def bonus(self):
sumover = 0

category = 0
while(category < 6):
sumover += self.points[category]
category += 1

if(sumover >= 63):
for category in range(len(grupper)):
if(grupper[category] == "Bonus"):
self.points[category] = 50
break
#Game Starts

players = []
print ("Välkomna till Yatzy!")

antal = int(input("Hur många är det som vill spela?: "))

for i in range(antal):
players.append(Spelare(str(input("Ange namn för spelare %d: " % (i + 1)))))

def printTable():

print("Namn", end="\t\t")

for spelare in players:
print(spelare.namn , end="\t")
print()

for antal in range(len(grupper)):
print(grupper[antal], end="\t\t")
for spelare in players:
print(spelare.points[antal], end="\t")
print()

print("Summa", end="\t\t")

for spelare in players:
print(spelare.summa(), end="\t")
print()

[code]varv = 0
[code]while(varv < 2):

for spelare in players:

print()
printTable()

print()
print("Nu är det %ss tur att spela\n" % spelare.namn)

i = -1
while (i < 0):
category = str(input("Vilket moment? "))

for antal in range(len(grupper)):
if grupper[antal] == category:
i = antal
if i < 0:
print("%s är inte ett giltigt moment. Försök igen.\n" % category)

points = int(input("Hur många poäng? (-1 för att stryka) "))

spelare.points[i] += points
spelare.points[6] += points

print("Okej, protokollet har uppdaterats.")

varv += 1
#varv <2

for spelare in players:
spelare.bonus()

print("Det har gått 15 rundor, spelet är slut!")
printTable()```

Anyway, you're in python 3.x?

#7
January 3rd, 2013, 05:53 PM
 jeppsson
Registered User

Join Date: Jan 2013
Posts: 5
Time spent in forums: 1 h 2 m 17 sec
Reputation Power: 0
Quote:
 Originally Posted by StevenJM Sort of... Code: ```grupper = ["Ones", "Twos","Threes","Foures","Fives","Sixes","Oversum","Bonus","Onepair","Twopair", "Thee", "Four", "Smallladder","Bigladder","Chance", "Yatzy" class Spelare(): def __init__(self, namn): self.namn = namn self.points = [] for a in range(len(grupper)): self.points.append(0) #Calculates total sum def summa(self): summa = 0 for a in range(len(grupper)): summa += self.points[a] return summa #Calculates over sum, first six categories def övresumma(self): övresumma = 0 mindre = 0 while (mindre < 6): övresumma += self.points[mindre] mindre += 1 return övresumma #Calculates bonus def bonus(self): sumover = 0 category = 0 while(category < 6): sumover += self.points[category] category += 1 if(sumover >= 63): for category in range(len(grupper)): if(grupper[category] == "Bonus"): self.points[category] = 50 break #Game Starts players = [] print ("Välkomna till Yatzy!") antal = int(input("Hur många är det som vill spela?: ")) for i in range(antal): players.append(Spelare(str(input("Ange namn för spelare %d: " % (i + 1))))) def printTable(): print("Namn", end="\t\t") for spelare in players: print(spelare.namn , end="\t") print() for antal in range(len(grupper)): print(grupper[antal], end="\t\t") for spelare in players: print(spelare.points[antal], end="\t") print() print("Summa", end="\t\t") for spelare in players: print(spelare.summa(), end="\t") print() [code]varv = 0 [code]while(varv < 2): for spelare in players: print() printTable() print() print("Nu är det %ss tur att spela\n" % spelare.namn) i = -1 while (i < 0): category = str(input("Vilket moment? ")) for antal in range(len(grupper)): if grupper[antal] == category: i = antal if i < 0: print("%s är inte ett giltigt moment. Försök igen.\n" % category) points = int(input("Hur många poäng? (-1 för att stryka) ")) spelare.points[i] += points spelare.points[6] += points print("Okej, protokollet har uppdaterats.") varv += 1 #varv <2 for spelare in players: spelare.bonus() print("Det har gått 15 rundor, spelet är slut!") printTable()``` Anyway, you're in python 3.x?

haha alright! 3.3!

#8
January 3rd, 2013, 06:31 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,214
Time spent in forums: 1 Month 3 Weeks 2 Days 18 h 10 m 8 sec
Reputation Power: 455
This, I believe, is the program of the original post. I don't know Yahtzee rules and, while I admit the internet is readily available, further that jeppson shouldn't have to repeat that which is already available, I don't understand the problem.

"It's been 15 rounds, the game is over!"---the game didn't last that long when I played. Could be related to the issue.
Code:
```grupper = ["Ones", "Twos", "Threes","Fours","Fives","Sixes","Over sum","Bonus","One pair","Two paires","Three","Four","Small ladder","Big ladder", "Full house", "Chance", "Yatzy"]

class Spelare():

def __init__(self, namn):
self.namn = namn
self.points = []
for a in range(len(grupper)):
self.points.append(0)

#Calculates the total sum
def summa(self):
summa = 0
for a in range(len(grupper)):
summa += self.points[a]
return summa

#Calculates the sum of the first six categories
def övresumma(self):
övresumma = 0

mindre = 0
while (mindre < 6):
övresumma += self.points[mindre]
mindre += 1
return övresumma

#Calculates a bonus which is 50 point if the over sum is over 63 points
def bonus(self):
sumover = 0

category = 0
while(category < 6):
sumover += self.points[category]
category += 1

if(sumover >= 63):
for category in range(len(grupper)):
if(grupper[category] == "Bonus"):
self.points[category] = 50
break
#Here is where the game starts

players = []
print ("Welcome to Yahtzee!")

antal = int(input("How many players?: "))

for i in range(antal):
players.append(Spelare(str(input("Names %d: " % (i + 1)))))

def printTable():

print("Namn", end="\t\t")

for spelare in players:
print(spelare.namn , end="\t")
print()

for antal in range(len(grupper)):
print(grupper[antal], end="\t\t")
for spelare in players:
print(spelare.points[antal], end="\t")
print()

print("Summa", end="\t\t")

for spelare in players:
#print (spelare.points)
print(spelare.summa(), end="\t")
print()

varv = 0
while(varv < 2):

for spelare in players:

print()
printTable()

print()
print("Now its %ss turn to play\n" % spelare.namn)

i = -1
while (i < 0):
category = str(input("Which category? "))

for antal in range(len(grupper)):
if grupper[antal] == category:
i = antal
if i < 0:
print("%s är inte ett giltigt moment. Försök igen.\n" % category)

points = int(input("How many points? (-1 to strike one) "))

spelare.points[i] += points

print("Okej, protokollet har uppdaterats.")

varv += 1
#varv <2

for spelare in players:
spelare.bonus()

print("Det har gått 15 rundor, spelet är slut!")
printTable()```
__________________
[code]Code tags[/code] are essential for python code!

Last edited by b49P23TIvg : January 3rd, 2013 at 06:34 PM.

#9
January 3rd, 2013, 06:38 PM
 jeppsson
Registered User

Join Date: Jan 2013
Posts: 5
Time spent in forums: 1 h 2 m 17 sec
Reputation Power: 0
Quote:
 Originally Posted by b49P23TIvg This, I believe, is the program of the original post. I don't know Yahtzee rules and, while I admit the internet is readily available, further that jeppson shouldn't have to repeat that which is already available, I don't understand the problem. "It's been 15 rounds, the game is over!"---the game didn't last that long when I played. Could be related to the issue. Code: ```grupper = ["Ones", "Twos", "Threes","Fours","Fives","Sixes","Over sum","Bonus","One pair","Two paires","Three","Four","Small ladder","Big ladder", "Full house", "Chance", "Yatzy"] class Spelare(): def __init__(self, namn): self.namn = namn self.points = [] for a in range(len(grupper)): self.points.append(0) #Calculates the total sum def summa(self): summa = 0 for a in range(len(grupper)): summa += self.points[a] return summa #Calculates the sum of the first six categories def övresumma(self): övresumma = 0 mindre = 0 while (mindre < 6): övresumma += self.points[mindre] mindre += 1 return övresumma #Calculates a bonus which is 50 point if the over sum is over 63 points def bonus(self): sumover = 0 category = 0 while(category < 6): sumover += self.points[category] category += 1 if(sumover >= 63): for category in range(len(grupper)): if(grupper[category] == "Bonus"): self.points[category] = 50 break #Here is where the game starts players = [] print ("Welcome to Yahtzee!") antal = int(input("How many players?: ")) for i in range(antal): players.append(Spelare(str(input("Names %d: " % (i + 1))))) def printTable(): print("Namn", end="\t\t") for spelare in players: print(spelare.namn , end="\t") print() for antal in range(len(grupper)): print(grupper[antal], end="\t\t") for spelare in players: print(spelare.points[antal], end="\t") print() print("Summa", end="\t\t") for spelare in players: #print (spelare.points) print(spelare.summa(), end="\t") print() varv = 0 while(varv < 2): for spelare in players: print() printTable() print() print("Now its %ss turn to play\n" % spelare.namn) i = -1 while (i < 0): category = str(input("Which category? ")) for antal in range(len(grupper)): if grupper[antal] == category: i = antal if i < 0: print("%s är inte ett giltigt moment. Försök igen.\n" % category) points = int(input("How many points? (-1 to strike one) ")) spelare.points[i] += points print("Okej, protokollet har uppdaterats.") varv += 1 #varv <2 for spelare in players: spelare.bonus() print("Det har gått 15 rundor, spelet är slut!") printTable()```

yeah actually it's supposed to last longer, i just changed "varv" to 2, when i was trying if everything worked, if you change it back to 15, it works better. My problem is that i would like the program to sum up both the over sum, the bonus and the total sum everytime a player enters something

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Python - yahtzee game!

## Developer Shed Advertisers and Affiliates

 Thread Tools Search this Thread Search this Thread: Advanced Search Display Modes Rate This Thread Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts vB code is On Smilies are On [IMG] code is On HTML code is Off
 View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox Forum Jump Please select one User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home -------------------- Programming Languages    PHP Development        PHP FAQs and Stickies    Perl Programming        Perl FAQs and Stickies    C Programming        C Programming FAQs and Stickies    Java Help        Java FAQs    Python Programming        Python Programming FAQs    Ruby Programming        Ruby Programming FAQs    Game Development        Game Development FAQs Programming Languages - More    ASP Programming        ASP Programming FAQs    .Net Development        .Net Development FAQs    Visual Basic Programming        Visual Basic Programming FAQs    Software Design        Software Design FAQs    ColdFusion Development        ColdFusion Development FAQs    Delphi Programming        Delphi Programming FAQs    Regex Programming        Regex Programming FAQs    XML Programming        XML Programming FAQs    Other Programming Languages        Other Programming Languages FAQs Web Design    HTML Programming        HTML Programming FAQs    JavaScript Development        JavaScript Development FAQs    CSS Help        CSS Help FAQs    Flash Help        Flash Help FAQs    Photoshop Help        Photoshop Help FAQs    Web Design Help        Web Design Help FAQs    Website Critiques        Website Critiques FAQs    Search Engine Optimization        Search Engine Optimization FAQs Mobile Programming    Mobile Programming        Mobile Programming FAQs    iPhone SDK Development        iPhone SDK Development FAQs    Android Development        Android Development FAQs    BlackBerry Development        BlackBerry Development FAQs Web Site Management    Business Help        Business Help FAQs    Development Software        Development Software FAQs    Scripts        Scripts FAQs Databases    Database Management        Database Management FAQs    DB2 Development        DB2 Development FAQs    MySQL Help        MySQL Help FAQs    PostgreSQL Help        PostgreSQL Help FAQs    Firebird SQL Development        Firebird SQL Development FAQs    MS SQL Development        MS SQL Development FAQs    Oracle Development        Oracle Development FAQs    LDAP Programming        LDAP Programming FAQs System Administration    Mail Server Help        Mail Server Help FAQs    Apache Development        Apache Development FAQs    Security and Cryptography        Security and Cryptography FAQs    Antivirus Protection        Antivirus Protection FAQs    DNS        DNS FAQs    IIS        IIS FAQs    Networking Help        Networking Help FAQs    FTP Help        FTP Help FAQs Operating Systems    BSD Help        BSD Help FAQs    Linux Help        Linux Help FAQs    UNIX Help        UNIX Help FAQs    Windows Help        Windows Help FAQs    Mac Help        Mac Help FAQs Web Hosting    Web Hosting        Web Hosting FAQs    Free Web Hosting        Free Web Hosting FAQs    Web Hosting Requests        Web Hosting Requests FAQs    Web Hosting Offers        Web Hosting Offers FAQs Computer Hardware    Computer Hardware    CPUs        CPUs FAQs    Cooling        Cooling FAQs    Embedded Programming        Embedded Programming FAQs    Motherboards        Motherboards FAQs    Multimedia Hardware        Multimedia Hardware FAQs Other    Dev Shed Lounge        Dev Shed Lounge FAQs    Development Articles        Development Articles FAQs    Beginner Programming        Beginner Programming FAQs    Hire A Programmer        Hire A Programmer FAQs    Project Help Wanted        Project Help Wanted FAQs Latest News Updated Hourly    Technology News    Business News    Science News Forum Information    Forum Rules/Guidelines        Forum Rules/Guidelines FAQs    Forum Announcements        Forum Announcements FAQs    Dev Shed Gaming Center        Go to the Dev Shed Battle Arena        Go to the Dev Shed Arcade Games        Go to the Legend of the Green Dragon    Suggestions & Feedback        Suggestions & Feedback FAQs

 Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |