Advertisement
Guest User

Untitled

a guest
Dec 29th, 2021
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.18 KB | None | 0 0
  1. import random
  2.  
  3. ladders = {
  4.     1: 38,
  5.     4: 14,
  6.     8: 30,
  7.     21: 42,
  8.     28: 76,
  9.     50: 67,
  10.     71: 92,
  11.     80: 99,
  12. }
  13.  
  14. snakes = {
  15.     97: 78,
  16.     62: 18,
  17.     95: 56,
  18.     88: 24,
  19.     36: 6,
  20.     48: 26,
  21.     32: 10,
  22. }
  23.  
  24.  
  25. class Player:
  26.     list_of_players = []
  27.  
  28.     def __init__(self, name="", moves=0, position=0, AI=False):
  29.         self.name = name
  30.         self.moves = moves
  31.         self.position = position
  32.         self.AI = AI
  33.         self.winner = False
  34.         Player.list_of_players.append(self)
  35.  
  36.     def __repr__(self):
  37.         return f"""player(AI={self.AI}, name={self.name})"""
  38.  
  39.  
  40. class Game:
  41.     def __init__(self):
  42.         human = input("How many human players? ")
  43.         computer = input("How many AI's? ")
  44.         for i in range(int(human)):
  45.             player_name = input(f"What is your player {i + 1} name? ")
  46.             self.player = Player(player_name)
  47.         for j in range(int(computer)):
  48.             self.AIplayer = Player(name=f"AI {j+1}", AI=True)
  49.  
  50.     def play(self):
  51.         while self.player.winner is False:
  52.             for each_player in Player.list_of_players:
  53.                 if not each_player.AI:
  54.                     input("Press Enter to roll the dice: ")
  55.                     dice = random.randint(1, 6)
  56.                     if each_player.position == 100:
  57.                         return f"{each_player.name} won!"
  58.  
  59.                     elif (each_player.position + dice) > 100:
  60.                         intended_position = each_player.position + dice
  61.                         bounce_back = intended_position - 100
  62.                         each_player.position = 100 - bounce_back
  63.                         print(
  64.                             f"{each_player.name} overshot the winning tile by {bounce_back}, {each_player.name}'s position is currently {each_player.position}")
  65.  
  66.                     else:
  67.                         each_player.position += dice
  68.                         print(f"{each_player.position} is {each_player.name}'s current position {dice}")
  69.                         if each_player.position == 100:
  70.                             return f"{each_player.name} won!"
  71.  
  72.                     if each_player.position in ladders:
  73.                         print(f"{each_player.name} landed on a ladder! Your position is currently {ladders.get(each_player.position)}")
  74.                         each_player.position = ladders.get(each_player.position)
  75.  
  76.                     elif each_player.position in snakes:
  77.                         print(f"{each_player.name} landed in a snake, Your position is current {snakes.get(each_player.position)}")
  78.                         each_player.position = snakes.get(each_player.position)
  79.  
  80.                 else:
  81.                     dice = random.randint(1, 6)
  82.                     if each_player.position == 100:
  83.                         return f"{each_player.name} won!"
  84.  
  85.                     elif (each_player.position + dice) > 100:
  86.                         intended_position = each_player.position + dice
  87.                         bounce_back = intended_position - 100
  88.                         each_player.position = 100 - bounce_back
  89.                         print(f"{each_player.name} overshot the winning tile by {bounce_back}, your position is currently {each_player.position}")
  90.  
  91.                     else:
  92.                         each_player.position += dice
  93.                         print(f"{each_player.position} is the {each_player.name} current position {dice}")
  94.                         if each_player.position == 100:
  95.                             return f"{each_player.name} won!"
  96.  
  97.                     if each_player.position in ladders:
  98.                         print(f"{each_player.name} landed on a ladder! {each_player.name} position is currently {ladders.get(each_player.position)}")
  99.                         each_player.position = ladders.get(each_player.position)
  100.  
  101.                     elif each_player.position in snakes:
  102.                         print(f"{each_player.name} landed in a snake, {each_player.name} position is current {each_player.position}")
  103.                         each_player.position = snakes.get(each_player.position)
  104.  
  105.  
  106.  
  107. game = Game()
  108. print(Player.list_of_players)
  109. print(game.play())
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement