### Thread: Python - yahtzee game!

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jan 2013
Posts
5
Rep 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. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2012
Posts
12
Rep 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. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jan 2013
Posts
5
Rep Power
0
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. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2012
Posts
12
Rep 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. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jan 2013
Posts
5
Rep 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. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2012
Posts
12
Rep Power
0
Sort of...

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. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jan 2013
Posts
5
Rep Power
0
Originally Posted by StevenJM
Sort of...

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. 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()```
Last edited by b49P23TIvg; January 3rd, 2013 at 05:34 PM.
9. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jan 2013
Posts
5
Rep Power
0
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