Skip to content

Instantly share code, notes, and snippets.

@geekdinazor
Created April 10, 2020 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geekdinazor/2eb14689cba307f4e4f20e242cb0d2d6 to your computer and use it in GitHub Desktop.
Save geekdinazor/2eb14689cba307f4e4f20e242cb0d2d6 to your computer and use it in GitHub Desktop.
yahtzee game
# Daniel Dixon
# Due: 6/02/17
# Yahtzee Code
import random
# Function
# Main menu and rules
while True:
def mainMenu():
while True:
inputMenu = input("Welcome to Yahtzee!\n\nPress R for the rules of the game\nOr press any key to continue...")
if inputMenu == ("R") or inputMenu ==("r"):
rulesStart = input("\n\n\t\t\t\tObjective\n\nThe object of Yahtzee is to obtain the highest score from rolling 5 dice.\nAfter rolling you can either score the current roll, or re-roll any or all of the dice.\nYou may only roll the dice a total of 2 times.\n\n\t\t\t\tScoring\n\nYour score can be totaled by getting a 3 and 4 of a kind, small and large straight, full house, and Yahtzee\n\nTo get 3 or 4 of a kind you must have at least 3 or 4 of the same die faces accordingly. You score the total of all the dice .\n\nA small straight is 4 consecutive die faces, and a large straight 5 consecutive die faces.Small straights score 30 and a large 40 points.\n\nAnd Yahtzee is 5 of a kind and scores 50 points\n\n\t\t\t\t How to Play\n\nIn order to keep dice, you must enter a number from 1 - 5 which match the dice accordingly.\n\ni.e. If you rolled (5,2,4,1,5) and you wanted to keep the 2,4, and 1, you must enter \"2,3,4\"\n\nWould you like to start now? (Y/N):")
if rulesStart == ("Y") or rulesStart == ("y"):
print("\n"*50)
break
elif rulesStart == ("N") or rulesStart == ("n"):
print("\n"*50,"Back to Main Menu\n")
else:
print("\n"*50)
break
# Looks for 3 or 4 of a kind and yahtzee in code
def of_kind(dice):
ofAKind = 1
for x in range(5):
current=x
count=1
while count != -1:
if current + 1 < 5:
if dice[current + 1] == dice[x]:
current = current + 1
count = count + 1
if count > ofAKind:
ofAKind = count
else:
count = -1
else:
count = -1
# Looks for yahtzee
if ofAKind == 5:
print("You matched \"YAHTZEE\": 50 points.\n\n")
return 50
# Looks for 3 or 4 of a kind
elif ofAKind == 4 or ofAKind == 3:
total = 0
for index in range(5):
total = total + int(dice[index])
if ofAKind == 4:
total = total + 10
print("You matched \"Four of a Kind\": " + str(total) + " points.\n\n")
return total
else:
noDuplicates = []
for index in range(5):
if dice[index] not in noDuplicates:
noDuplicates.append(dice[index])
if len(noDuplicates) == 2:
print("You matched \"Full House\": 25 points.\n\n")
return 25
else:
total=total+5
print("You matched \"Three of a Kind\": " + str(total) + " points.\n\n")
return total
else:
return 0
# Looks for small or large straights in code
def straight(dice):
straight = 1
for index in range(len(dice)):
current = index
count = 1
while(current != -1):
if current + 1 < len(dice):
if int(dice[current + 1]) - int(dice[current]) == 1:
current = current + 1
count = count + 1
elif int(dice[current + 1]) == int(dice[current]):
current = current + 1
else:
current = -1
else:
current = -1
if count > straight:
straight = count
if straight == 4:
print("You matched \"Small Straight\": 30 points.\n\n")
return 30
elif straight ==5:
print("You matched \"Large Straight\": 40 points.\n\n")
return 40
else:
return 0
# Returns the user score as 0 if user doesn't recieve any matches
def noScore(dice):
score = of_kind(dice)
if score == 0:
score = straight(dice)
if score == 0:
print("No match: 0 points\n\n")
return score
# Actual game code - Uses all of the functions above to play the game
def userGameplay():
global totalScore
totalScore = 0
for x in range(1,6):
# Declares User's turn
print("Turn " + str(x) + "!")
dice = []
for x in range(5):
dice.append(random.randint(1,6))
# Prints dice User rolled
print("Roll 1: ", end ="")
for x in range(len(dice)):
if x < len(dice) - 1:
print(dice[x], end =" ")
else:
print(dice[x])
# Asks the user which numbers to keep or roll again
save = input("Which numbers would you like to keep? (1,2,3,4,5)")
save = save.split(',')
print("Roll 2: ", end ="")
for x in range(5):
if str(x + 1) not in save:
dice[x] = random.randint(1,6)
if x < len(dice) - 1:
print(dice[x], end =" ")
else:
print(dice[x])
dice.sort()
score = noScore(dice)
totalScore = totalScore + score
# End screen - play again or quit
def endScreen():
global totalScore
global straight
print("\n"*5,"Total score:",totalScore)
# Loop to beginning or quit
exit = input("Do you want to play Yahtzee? (Y/N)")
if exit == ("N") or exit == ("n"):
print("Quit")
break
elif exit == ("Y") or exit == ("y"):
print("\n"*5)
elif exit == ("N") or exit == ("n"):
print("Quit")
else:
print("Invalid answer... quitting.")
break
# Main code
mainMenu()
userGameplay()
endScreen()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment