Hasli4

Untitled

Mar 27th, 2025
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.38 KB | None | 0 0
  1. def simulate_game(cherry_juice, pear_juice, first_player="Андрей"):
  2.     """
  3.    Симуляция игры с соком между дядей Андреем и девочкой Машей.
  4.    
  5.    Улучшенная версия с корректной проверкой остатка сока.
  6.    
  7.    Параметры:
  8.    cherry_juice - количество вишневого сока в литрах
  9.    pear_juice - количество грушевого сока в литрах
  10.    first_player - кто ходит первым ("Андрей" или "Маша")
  11.    
  12.    Возвращает кортеж (победитель, количество выпитого Андреем, количество выпитого Машей)
  13.    """
  14.     # Емкость кружки Андрея в мл
  15.     andrey_cup = 500
  16.    
  17.     # Емкость двух кружек Маши в мл
  18.     masha_cups = 240 * 2
  19.    
  20.     # Счетчики выпитого сока
  21.     andrey_drunk = 0
  22.     masha_drunk = 0
  23.    
  24.     # Конвертируем литры в мл
  25.     cherry_juice *= 1000
  26.     pear_juice *= 1000
  27.    
  28.     current_player = first_player
  29.    
  30.     # Главный цикл игры
  31.     while True:
  32.         if current_player == "Андрей":
  33.             # Проверка возможности хода для Андрея с учетом остатка сока
  34.             can_drink_cherry = cherry_juice >= andrey_cup
  35.             can_drink_pear = pear_juice >= andrey_cup
  36.            
  37.             if can_drink_cherry or can_drink_pear:
  38.                 # Стратегия выбора сока: забирать меньший объем
  39.                 if can_drink_cherry and can_drink_pear:
  40.                     if cherry_juice <= pear_juice:
  41.                         cherry_juice -= andrey_cup
  42.                         andrey_drunk += andrey_cup
  43.                     else:
  44.                         pear_juice -= andrey_cup
  45.                         andrey_drunk += andrey_cup
  46.                 elif can_drink_cherry:
  47.                     cherry_juice -= andrey_cup
  48.                     andrey_drunk += andrey_cup
  49.                 else:
  50.                     pear_juice -= andrey_cup
  51.                     andrey_drunk += andrey_cup
  52.                
  53.                 current_player = "Маша"
  54.             else:
  55.                 # Андрей не может сделать ход
  56.                 current_player = "Маша"
  57.                
  58.                 # Если и Маша не может ходить - игра окончена
  59.                 if not (cherry_juice >= masha_cups or pear_juice >= masha_cups):
  60.                     break
  61.         else:  # Ход Маши
  62.             # Проверка возможности хода для Маши с учетом остатка сока
  63.             can_drink_cherry = cherry_juice >= masha_cups
  64.             can_drink_pear = pear_juice >= masha_cups
  65.            
  66.             if can_drink_cherry or can_drink_pear:
  67.                 # Стратегия выбора сока: забирать меньший объем
  68.                 if can_drink_cherry and can_drink_pear:
  69.                     if cherry_juice <= pear_juice:
  70.                         cherry_juice -= masha_cups
  71.                         masha_drunk += masha_cups
  72.                     else:
  73.                         pear_juice -= masha_cups
  74.                         masha_drunk += masha_cups
  75.                 elif can_drink_cherry:
  76.                     cherry_juice -= masha_cups
  77.                     masha_drunk += masha_cups
  78.                 else:
  79.                     pear_juice -= masha_cups
  80.                     masha_drunk += masha_cups
  81.                
  82.                 current_player = "Андрей"
  83.             else:
  84.                 # Маша не может сделать ход
  85.                 current_player = "Андрей"
  86.                
  87.                 # Если и Андрей не может ходить - игра окончена
  88.                 if not (cherry_juice >= andrey_cup or pear_juice >= andrey_cup):
  89.                     break
  90.    
  91.     # Определение победителя
  92.     winner = "Андрей" if andrey_drunk > masha_drunk else "Маша"
  93.     if andrey_drunk == masha_drunk:
  94.         winner = "Ничья"
  95.    
  96.     # Возвращаем результаты в литрах
  97.     return winner, andrey_drunk / 1000, masha_drunk / 1000
  98.  
  99. # Демонстрация игры с разными начальными условиями
  100. def main():
  101.     # Начальный сценарий с Андреем, ходящим первым
  102.     print("Сценарий 1: Андрей ходит первым")
  103.     winner1, andrey_total1, masha_total1 = simulate_game(24, 24, "Андрей")
  104.     print(f"Победитель: {winner1}")
  105.     print(f"Андрей выпил: {andrey_total1:.2f} литров")
  106.     print(f"Маша выпила: {masha_total1:.2f} литров")
  107.  
  108.     # Сценарий с Машей, ходящей первой
  109.     print("\nСценарий 2: Маша ходит первой")
  110.     winner2, andrey_total2, masha_total2 = simulate_game(24, 24, "Маша")
  111.     print(f"Победитель: {winner2}")
  112.     print(f"Андрей выпил: {andrey_total2:.2f} литров")
  113.     print(f"Маша выпила: {masha_total2:.2f} литров")
  114.  
  115. if __name__ == "__main__":
  116.     main()
Add Comment
Please, Sign In to add comment