Advertisement
elihenson13

Python Yahtzee

Mar 19th, 2025
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.36 KB | Source Code | 0 0
  1. import random
  2. import statistics
  3.  
  4. scoreList = ["ones","twos","threes","fours","fives","sixes","three of a kind","four of a kind","full house","small straight","large straight","yahtzee","chance"]
  5. plays = ["ones","twos","threes","fours","fives","sixes","three of a kind","four of a kind","full house","small straight","large straight","yahtzee","chance"]
  6. scoreValues = ["?","?","?","?","?","?","?","?","?","?","?","?","?"]
  7. bonus = 0
  8. total = 0
  9. ybonus = 0
  10. dice = [0,0,0,0,0]
  11.  
  12. def displayLock(l,d):
  13.     for x in range(0,len(l)):
  14.         if l[x] == 0:
  15.             if x < len(l) - 1:
  16.                 print(d[x],", ",sep='',end='')
  17.             else:
  18.                 print(d[x],".\n",sep='')
  19.         else:
  20.             if x < len(l) - 1:
  21.                 print("[",d[x],"], ",sep='',end='')
  22.             else:
  23.                 print("[",d[x],"].\n",sep='')
  24.                
  25. def displayRoll(d):
  26.     for x in range(0,len(d)):
  27.         if x < len(d) - 1:
  28.             print(d[x],", ",sep='',end='')
  29.         else:
  30.             print(d[x],".\n",sep='')
  31.  
  32. def roll(l,d,c):
  33.     if len(l) != len(d):
  34.         print("Lock list and dice list not the same length. Returning original dice list.")
  35.         return d
  36.     if c < 2:
  37.         print("You rolled: ",end='')
  38.     else:
  39.         print("Your final roll: ",end='')
  40.     for x in range(0,len(l)):
  41.         if l[x] == 0:
  42.             d[x] = random.randint(1,6)
  43.     if c == 1:
  44.         displayLock(l,d)
  45.     else:
  46.         displayRoll(d)
  47.     return d
  48.    
  49. def getLock(l,d):
  50.     response = "initial"
  51.     confirm = 0
  52.     confirmLoop = False
  53.     while confirm == 0:
  54.         valid = 0
  55.         while valid == 0:
  56.             numbers = 0
  57.             if confirmLoop == False:
  58.                 response = input("Lock/unlock dice by entering numbers corresponding to their position in the roll. Eg, typing 145 will toggle the lock state of the first, fourth, and fifth dice. You will be able to confirm your decision before committing.\n")
  59.             valid = 1
  60.             for i,x in enumerate(response):
  61.                 try:
  62.                     int(x)
  63.                 except:
  64.                     print("Invalid response, contains something other than a number.")
  65.                     valid = 0
  66.                     confirmLoop = False
  67.                     break
  68.                 else:
  69.                     if int(x) == 0 or int(x) > 5:
  70.                         print("Invalid response, at least one digit not in 1-5 range.")
  71.                         valid = 0
  72.                         confirmLoop = False
  73.                         break
  74.         locklist = []
  75.         for i,x in enumerate(response):
  76.             locklist.append(int(x))
  77.         locklist = list(set(locklist))
  78.         for i,x in enumerate(locklist):
  79.             l[x - 1] = (l[x - 1] + 1) % 2
  80.         print("\nNumbers in brackets will not be rolled: ")
  81.         displayLock(l,d)
  82.         if l == [1,1,1,1,1]:
  83.             response = input("All dice are locked, hitting ENTER will confirm your roll. Enter numbers again if you want to unlock some dice and roll again.")
  84.         else:
  85.             response = input("Just hit ENTER to roll, or enter numbers again to continue locking/unlocking.\n")
  86.         if response == "":
  87.             confirm = 1
  88.         else:
  89.             confirmLoop = True
  90.     return l
  91.    
  92. def getBonus(v):
  93.     b = 0
  94.     s = 0
  95.     for x in list(range(0,6)):
  96.         if type(v[x]) == type(1):
  97.             s = s + v[x]
  98.         if s >= 63:
  99.             b = 35
  100.     return b
  101.    
  102. def getTotal(v,b,y):
  103.     t = b + y
  104.     for x in list(range(0,13)):
  105.         if type(v[x]) == type(1):
  106.             t = t + v[x]
  107.     return t
  108.  
  109. def scoreCard(scores,points,b,t,y):
  110.     print("==UPPER SECTION==")
  111.     for x in list(range(0, 13)):
  112.         print(x + 1,". ",scores[x].title(),": ",points[x],sep='')
  113.         if x == 5:
  114.             s = 0
  115.             for n in [0,1,2,3,4,5]:
  116.                 if type(points[n]) == type(1):
  117.                     s = s + points[n]
  118.             print("Upper Section Total:",s)
  119.             print("Upper Section Bonus:",b,"\n\n==LOWER SECTION==")
  120.     if points[11] == "?":
  121.         print("Bonus Yahtzees: ?")
  122.     if points[11] == 0:
  123.         print("Bonus Yahtzees: 0")
  124.     if points[11] == 50:
  125.         print("Bonus Yahtzees:",y)
  126.     print("\nGrand Total:",t,"\n")
  127.  
  128. def getRoll():
  129.     lock = [0,0,0,0,0]
  130.     dice = [0,0,0,0,0]
  131.     rollcount = 0
  132.     while rollcount < 3:
  133.         if rollcount == 0:
  134.             input("Press ENTER to roll the dice!")
  135.         dice = roll(lock,dice,rollcount)
  136.         rollcount += 1
  137.         if rollcount < 3:
  138.             lock = getLock(lock,dice)
  139.         if lock == [1,1,1,1,1]:
  140.             print("Your final roll: ",end='')
  141.             displayRoll(dice)
  142.             return dice
  143.     return dice
  144.    
  145. def getScoreSelect(list,all):
  146.     response = 'blank'
  147.     i = 0
  148.     while response.lower() not in all:
  149.         if i > 0 and response not in ["help","used"]:
  150.             print("Invalid input. Please try again.")
  151.         response = input("Enter the name or number of the score you want to use for this roll. Type HELP for a description list.\n")
  152.         if response in ["1","2","3","4","5","6","7","8","9","10","11","12","13"]:
  153.             response = all[int(response) - 1]
  154.         if i == 0:
  155.             i = 1
  156.         if response.lower() in all and response.lower() not in list:
  157.             print("You've already used that score this game! Score on something with a ?")
  158.             response = "used"
  159.         if response.lower() == "help":
  160.             print("\n==UPPER SECTION==\nOnes: Add the value of all 1s")
  161.             print("Twos: Add the value of all 2s")
  162.             print("Threes: Add the value of all 3s")
  163.             print("Fours: Add the value of all 4s")
  164.             print("Fives: Add the value of all 5s")
  165.             print("Sixes: Add the value of all 6s")
  166.             print("Upper Section Total: Shows the sum of all points earned in the Upper Section.")
  167.             print("Upper Section Bonus: If Upper Section Total is 63 or higher, this bonus changes from 0 to 35.")
  168.             print("\n==LOWER SECTION==\nThree of a Kind: If three or more dice are the same, take the sum of all dice.")
  169.             print("Four of a Kind: If four or more dice are the same, take the sum of all dice.")
  170.             print("Full House: If your roll contains three of one number and two of another, get 25 points.")
  171.             print("Small Straight: If your roll contains a consecutive sequence of 4 dice, get 30 points.")
  172.             print("Large Straight: If your roll contains a consecutive sequence of 5 dice, get 40 points.")
  173.             print("Yahtzee: If your roll is five of a kind, get 50 points.")
  174.             print("Chance: Take the sum of all dice.")
  175.             print("Bonus Yahtzees: If you scored a 50 on Yahtzee, each subsequent five of a kind earns you 100 bonus points. (You still need to put the roll towards one of the 13 scores.)")
  176.             print("\nIf you've entered a score in Yahtzee, any subsequent five of a kind is a JOKER. JOKERs must be used on the appropriate Upper Section score. Eg, you roll five 4s after 0ing out your Yahtzee. If you haven't scored Fours yet, you must take the Fours this turn (20 points). Otherwise, you can score in the Lower Section if any are open. For Full House, Small Straight, and Large Straight, JOKERs will satisfy the condition. If the appropriate Upper Section and all Lower Section items are already scored, you must choose an Upper Section category to 0 out.\n")
  177.     return response.lower()
  178.    
  179. def countSingleType(r,n):
  180.     t = 0
  181.     for x in [0,1,2,3,4]:
  182.         if r[x] == n:
  183.             t = t + r[x]
  184.     return t
  185.    
  186. def straightCheck(roll,length):
  187.         r = list(set(roll))
  188.         count = 0
  189.         for i,x in enumerate(r):
  190.             if i == len(r) - 1:
  191.                 break
  192.             if r[i + 1] == x + 1:
  193.                 count += 1
  194.             else:
  195.                 count = 0
  196.             if count == length:
  197.                 return True
  198.         return False
  199.  
  200. def updateScores(v,s,d,p):
  201.     upper = ["ones","twos","threes","fours","fives","sixes"]
  202.     joker = 0
  203.     if d.count(statistics.mode(d)) == 5 and v[11] in [0,50]:
  204.         joker = 1
  205.         if s not in upper:
  206.             input("Because you rolled a Yahtzee and you've already used your Yahtzee score, this is a JOKER roll!\n")
  207.             if v[11] == 50:
  208.                 input("You already scored on Yahtzee! You earned a bonus 100 points!\n")
  209.         if v[d[0] - 1] == "?":
  210.             s = upper[d[0] - 1]
  211.             print("You rolled ",d[0],"s, and you haven't used your ",s.title()," yet, so your score for this roll must be used on it!",sep='')
  212.         else:
  213.             print("You already scored on ",d[0],"s, so you will score on ",s.title()," with your JOKER roll!\n",sep='')
  214.     p.remove(s)
  215.     if s == 'ones':
  216.         v[0] = countSingleType(d,1)
  217.         return v,p
  218.     if s == 'twos':
  219.         v[1] = countSingleType(d,2)
  220.         return v,p
  221.     if s == 'threes':
  222.         v[2] = countSingleType(d,3)
  223.         return v,p
  224.     if s == 'fours':
  225.         v[3] = countSingleType(d,4)
  226.         return v,p
  227.     if s == 'fives':
  228.         v[4] = countSingleType(d,5)
  229.         return v,p
  230.     if s == 'sixes':
  231.         v[5] = countSingleType(d,6)
  232.         return v,p
  233.     if s == 'three of a kind':
  234.         t = 0
  235.         if d.count(statistics.mode(d)) >= 3:
  236.             t = sum(d)
  237.         v[6] = t
  238.         return v,p
  239.     if s == 'four of a kind':
  240.         t = 0
  241.         if d.count(statistics.mode(d)) >= 4:
  242.             t = sum(d)
  243.         v[7] = t
  244.         return v,p
  245.     if s == 'full house':
  246.         t = 0
  247.         c = []
  248.         for x in [1,2,3,4,5,6]:
  249.             if d.count(x) in [2,3]:
  250.                 c.append(d.count(x))
  251.         c.sort()
  252.         if c == [2,3] or joker == 1:
  253.             t = 25
  254.         v[8] = t
  255.         return v,p
  256.     if s == 'small straight':
  257.         t = 0
  258.         if straightCheck(d,3) or joker == 1:
  259.             t = 30
  260.         v[9] = t
  261.         return v,p
  262.     if s == 'large straight':
  263.         t = 0
  264.         if straightCheck(d,4) or joker == 1:
  265.             t = 40
  266.         v[10] = t
  267.         return v,p
  268.     if s == 'yahtzee':
  269.         t = 0
  270.         if d.count(statistics.mode(d)) == 5:
  271.             t = 50
  272.         v[11] = t
  273.         return v,p
  274.     if s == 'chance':
  275.         v[12] = sum(d)
  276.         return v,p
  277.  
  278. input("Welcome to Yahtzee! Press ENTER to begin!\n")
  279. while len(plays) > 0:
  280.     if len(plays) < 13:
  281.         bonus = getBonus(scoreValues)
  282.         total = getTotal(scoreValues,bonus,ybonus)
  283.     scoreCard(scoreList,scoreValues,bonus,total,ybonus)
  284.     dice = getRoll()
  285.     if dice.count(statistics.mode(dice)) == 5 and scoreValues[11] == 50:
  286.         ybonus += 100
  287.     scoreCard(scoreList,scoreValues,bonus,total,ybonus)
  288.     selection = getScoreSelect(plays,scoreList)
  289.     print("You selected ",selection.title(),".\n",sep='')
  290.     (scoreValues,plays) = updateScores(scoreValues,selection,dice,plays)
  291. bonus = getBonus(scoreValues)
  292. total = getTotal(scoreValues,bonus,ybonus)
  293. scoreCard(scoreList,scoreValues,bonus,total,ybonus)
  294. print("Congratulations! Your final score was",total)
  295. input("Thanks for playing!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement