Advertisement
makispaiktis

Yahtzee Game

May 2nd, 2024 (edited)
636
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.59 KB | None | 0 0
  1. # 1. Global variables
  2. valid_combs = ["1. Ones", "2. Twos", "3. Threes", "4. Fours", "5. Fives", "6. Sixes", "7. Three of a kind", "8. Four of a kind", "9. Full House", "10.Small Straight", "11. Large Straight", "12. Yahtzee", "13. Chance"]
  3. valid_rules = ["Sum of aces", "Sum of twos", "Sum of threes", "Sum of fours", "Sum of fives", "Sum of sixes", "Sum of all dice", "Sum of all dice", "25", "30", "40", "50", "Sum of all dice"]
  4. LEN = len(valid_combs)
  5.  
  6.  
  7. # 2. Auxiliary Functions
  8. def count(pentada):
  9.     # Count how many 1s, 2s, ...., 6s there are
  10.     counters = list()
  11.     for i in range(1, 7):
  12.         counters.append(pentada.count(i))
  13.     return counters
  14.  
  15. def score(pentada, rule):
  16.  
  17.     if rule in list(range(1, 7)):
  18.         return rule * pentada.count(rule)
  19.  
  20.     elif rule == 7:
  21.       # Three of a kind
  22.       counters = count(pentada)
  23.       return sum(pentada) if ((3 in counters) or (4 in counters) or (5 in counters)) else 0
  24.  
  25.     elif rule == 8:
  26.         # Four of a kind
  27.         counters = count(pentada)
  28.         return sum(pentada) if ((4 in counters) or (5 in counters)) else 0
  29.  
  30.     elif rule == 9:
  31.         # Full House
  32.         counters = count(pentada)
  33.         counters = sorted(counters, reverse=True)
  34.         return 25 if counters == [3, 2, 0, 0, 0, 0] else 0
  35.  
  36.     elif rule == 10:
  37.         # Small Straight = 1-2-3-4 or 2-3-4-5 or 3-4-5-6
  38.         pentada = set(pentada)
  39.         valids = [{1,2,3,4}, {2,3,4,5}, {3,4,5,6}]
  40.         for valid in valids:
  41.             if valid.issubset(pentada):
  42.                 return 30
  43.         return 0
  44.  
  45.     elif rule == 11:
  46.         # Large Straight = 1-2-3-4-5 or 2-3-4-5-6
  47.         counters = count(pentada)
  48.         counters = sorted(counters, reverse=True)
  49.         return 40 if counters == [1, 1, 1, 1, 1, 0] else 0
  50.  
  51.     elif rule == 12:
  52.         # Yahtzee
  53.         counters = count(pentada)
  54.         counters = sorted(counters, reverse=True)
  55.         return 50 if counters == [5, 0, 0, 0, 0, 0] else 0
  56.  
  57.     elif rule == 13:
  58.         # Chance
  59.         return sum(pentada)
  60.  
  61. num_combs = list(range(1, LEN + 1))
  62. def decide(num_combs_used, dices, player, points):
  63.     declaration = ""
  64.     for num_comb in num_combs:
  65.         if num_comb not in num_combs_used:
  66.             declaration = declaration + valid_combs[num_comb - 1] + ", "
  67.     if declaration:
  68.         declaration = declaration[:-2]
  69.     print("!!!! Available combinations !!!!\n", declaration)
  70.  
  71.     rule = int(input("Select combination: "))
  72.     while rule in num_combs_used:
  73.         rule = int(input("Select combination: "))
  74.  
  75.     num_combs_used.append(rule)
  76.     new_points = score(dices, rule)
  77.     COMB = valid_combs[rule - 1]
  78.     STR = COMB[3:]
  79.     points = points + new_points
  80.     print(f"{player} scored {new_points} points with {STR} ----> Total points = {points}")
  81.     return points, num_combs_used
  82.  
  83.  
  84. # 3. Playing Functions
  85. from random import randrange
  86. import copy
  87. def roll(N):
  88.     return [randrange(1, 7) for i in range(N)]
  89.  
  90. rerolls_permitted = 2
  91.  
  92. def show(dices, round, rerolls_used, player):
  93.     # print(40 * "*")
  94.     print(f"{rerolls_permitted - rerolls_used} rerolls left for {player} in round {round}:")
  95.     dices_str = [str(elem) for elem in dices]
  96.     dice_show = " - ".join(dices_str)
  97.     print(f"{player} threw:\t {dice_show}\n")
  98.  
  99. def rerolls0(player, rerolls_used, N, round, points, num_combs_used):
  100.     print(f"\n\n----> {player}'s turn ({points} points)    -    Combinations used = {num_combs_used}")
  101.     dices = roll(N)
  102.     show(dices, round, rerolls_used, player)
  103.     return dices
  104.  
  105. def rerolls1(player, rerolls_used, N, round, dices, positions):
  106.     positions = positions.split(", ")
  107.     positions = [int(position) for position in positions]
  108.     new_rolls = roll(len(positions))
  109.     # Checking if positions are between 1 and N - Invalid input, then dices are not changed
  110.     for position in positions:
  111.         if position not in list(range(1, N+1)):
  112.             return dices
  113.     # Changing the dices
  114.     for i in range(len(positions)):
  115.         index = positions[i] - 1
  116.         value = new_rolls[i]
  117.         dices[index] = value
  118.     show(dices, round, rerolls_used, player)
  119.     return dices
  120.  
  121.  
  122. # MAIN FUNCTION
  123. players = ["Thomas", "PC"]
  124. num_combs_used_list = [[], []]
  125. points_list = [0, 0]
  126. N = 5
  127. bold_start = "\033[1m"
  128. bold_end = "\033[0;0m"
  129.  
  130. for round in list(range(1, LEN + 1)):
  131.     print(20 * "*", 20 * "*", sep=f" Round {round} ", end="\n")
  132.     for i, player in enumerate(players):
  133.  
  134.           num_combs_used = num_combs_used_list[i]
  135.           points = points_list[i]
  136.  
  137.           rerolls_used = 0
  138.           dices = rerolls0(player, rerolls_used, N, round, points, num_combs_used)
  139.           positions = input("Positions to reroll: ")
  140.           if positions == "":
  141.               # print("Time to decide (0 rerolls used)\n")
  142.               points, num_combs_used = decide(num_combs_used, dices, player, points)
  143.               continue
  144.           else:
  145.               rerolls_used = 1
  146.  
  147.           dices = rerolls1(player, rerolls_used, N, round, dices, positions)
  148.           positions = input("Positions to reroll:\n")
  149.           if positions == "":
  150.               # print("Time to decide (1 reroll used)\n")
  151.               points, num_combs_used = decide(num_combs_used, dices, player, points)
  152.               continue
  153.           else:
  154.               rerolls_used = 2
  155.  
  156.           dices = rerolls1(player, rerolls_used, N, round, dices, positions)
  157.           points, num_combs_used = decide(num_combs_used, dices, player, points)
  158.  
  159.           # UPDATE THE VALUES OF LISTS
  160.           num_combs_used_list[i] = num_combs_used
  161.           points_list[i] = points
  162.     print("\n\n\n\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement