Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def simulate_game(cherry_juice, pear_juice, first_player="Андрей"):
- """
- Симуляция игры с соком между дядей Андреем и девочкой Машей.
- Улучшенная версия с корректной проверкой остатка сока.
- Параметры:
- cherry_juice - количество вишневого сока в литрах
- pear_juice - количество грушевого сока в литрах
- first_player - кто ходит первым ("Андрей" или "Маша")
- Возвращает кортеж (победитель, количество выпитого Андреем, количество выпитого Машей)
- """
- # Емкость кружки Андрея в мл
- andrey_cup = 500
- # Емкость двух кружек Маши в мл
- masha_cups = 240 * 2
- # Счетчики выпитого сока
- andrey_drunk = 0
- masha_drunk = 0
- # Конвертируем литры в мл
- cherry_juice *= 1000
- pear_juice *= 1000
- current_player = first_player
- # Главный цикл игры
- while True:
- if current_player == "Андрей":
- # Проверка возможности хода для Андрея с учетом остатка сока
- can_drink_cherry = cherry_juice >= andrey_cup
- can_drink_pear = pear_juice >= andrey_cup
- if can_drink_cherry or can_drink_pear:
- # Стратегия выбора сока: забирать меньший объем
- if can_drink_cherry and can_drink_pear:
- if cherry_juice <= pear_juice:
- cherry_juice -= andrey_cup
- andrey_drunk += andrey_cup
- else:
- pear_juice -= andrey_cup
- andrey_drunk += andrey_cup
- elif can_drink_cherry:
- cherry_juice -= andrey_cup
- andrey_drunk += andrey_cup
- else:
- pear_juice -= andrey_cup
- andrey_drunk += andrey_cup
- current_player = "Маша"
- else:
- # Андрей не может сделать ход
- current_player = "Маша"
- # Если и Маша не может ходить - игра окончена
- if not (cherry_juice >= masha_cups or pear_juice >= masha_cups):
- break
- else: # Ход Маши
- # Проверка возможности хода для Маши с учетом остатка сока
- can_drink_cherry = cherry_juice >= masha_cups
- can_drink_pear = pear_juice >= masha_cups
- if can_drink_cherry or can_drink_pear:
- # Стратегия выбора сока: забирать меньший объем
- if can_drink_cherry and can_drink_pear:
- if cherry_juice <= pear_juice:
- cherry_juice -= masha_cups
- masha_drunk += masha_cups
- else:
- pear_juice -= masha_cups
- masha_drunk += masha_cups
- elif can_drink_cherry:
- cherry_juice -= masha_cups
- masha_drunk += masha_cups
- else:
- pear_juice -= masha_cups
- masha_drunk += masha_cups
- current_player = "Андрей"
- else:
- # Маша не может сделать ход
- current_player = "Андрей"
- # Если и Андрей не может ходить - игра окончена
- if not (cherry_juice >= andrey_cup or pear_juice >= andrey_cup):
- break
- # Определение победителя
- winner = "Андрей" if andrey_drunk > masha_drunk else "Маша"
- if andrey_drunk == masha_drunk:
- winner = "Ничья"
- # Возвращаем результаты в литрах
- return winner, andrey_drunk / 1000, masha_drunk / 1000
- # Демонстрация игры с разными начальными условиями
- def main():
- # Начальный сценарий с Андреем, ходящим первым
- print("Сценарий 1: Андрей ходит первым")
- winner1, andrey_total1, masha_total1 = simulate_game(24, 24, "Андрей")
- print(f"Победитель: {winner1}")
- print(f"Андрей выпил: {andrey_total1:.2f} литров")
- print(f"Маша выпила: {masha_total1:.2f} литров")
- # Сценарий с Машей, ходящей первой
- print("\nСценарий 2: Маша ходит первой")
- winner2, andrey_total2, masha_total2 = simulate_game(24, 24, "Маша")
- print(f"Победитель: {winner2}")
- print(f"Андрей выпил: {andrey_total2:.2f} литров")
- print(f"Маша выпила: {masha_total2:.2f} литров")
- if __name__ == "__main__":
- main()
Add Comment
Please, Sign In to add comment