Advertisement
MichalDK

BackGammon

Feb 8th, 2023 (edited)
768
0
128 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.62 KB | Software | 0 0
  1. class Player:
  2.     def __init__(self, number=0):
  3.         self.number = number
  4.         self.attemts = 0
  5.         self.history_of_rolls=[]
  6.  
  7.     def dice_roll(self):
  8.         from random import randrange
  9.         dice = randrange(1 ,7)
  10.         self.history_of_rolls.append(dice)
  11.         return  dice
  12.  
  13.     def __repr__(self):
  14.             message = f"Player {self.number + 1} history: \n {self.history_of_rolls}"
  15.             return message
  16.  
  17.  
  18. if __name__ == "__main__":
  19.     participants = 4
  20.     list_of_players = []
  21.     for _ in range (0, participants):
  22.         the_player=Player(_)
  23.         attempt = 0
  24.         while attempt != 6:
  25.             attempt = the_player.dice_roll()
  26.         list_of_players.append(the_player)
  27.  
  28.     min_attempts = len(list_of_players[0].history_of_rolls)
  29.     winner = 0
  30.  
  31.     max_attempts = 0
  32.     looser = 0
  33.  
  34.     for single_player in list_of_players:
  35.         if len(single_player.history_of_rolls)<min_attempts:
  36.             min_attempts = len(single_player.history_of_rolls)
  37.             winner = single_player.number
  38.  
  39.         if len(single_player.history_of_rolls)>max_attempts:
  40.             max_attempts = len(single_player.history_of_rolls)
  41.             looser = single_player.number
  42.  
  43.         print(single_player.__repr__())
  44.  
  45.     print(f"Winner is player {winner + 1}. He needs {min_attempts} attempts to throw a six.")
  46.     print(f"Winning series is {list_of_players[winner].history_of_rolls}")
  47.     print("Congratulations !")
  48.  
  49.     print(f"Looser is player {looser + 1}. He needs {max_attempts} attempts to throw a six.")
  50.     print(f"Black series is {list_of_players[looser].history_of_rolls}")
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement