56 lines
1.6 KiB
Python
56 lines
1.6 KiB
Python
import random
|
|
|
|
|
|
class Game:
|
|
def __init__(self):
|
|
self.sticks = [10.0]
|
|
self.current = "Игрок"
|
|
|
|
def can_move(self):
|
|
return any(s >= 1 for s in self.sticks)
|
|
|
|
def split(self, index, x):
|
|
stick = self.sticks[index]
|
|
if stick < 1 or x <= 0 or x >= stick:
|
|
return False
|
|
self.sticks.pop(index)
|
|
self.sticks.append(x)
|
|
self.sticks.append(stick - x)
|
|
return True
|
|
|
|
def computer_move(self):
|
|
for i, s in enumerate(self.sticks):
|
|
if s >= 1:
|
|
x = round(s / 2, 2)
|
|
self.split(i, x)
|
|
print(f"Компьютер ломает палочку {s} на {x} и {round(s - x, 2)}")
|
|
return
|
|
|
|
|
|
while True:
|
|
game = Game()
|
|
while True:
|
|
print("\nПалочки:", game.sticks)
|
|
if not game.can_move():
|
|
print(f"Проиграл {game.current}")
|
|
break
|
|
|
|
if game.current == "Игрок":
|
|
try:
|
|
cmd = input("Введите индекс палочки и длину разлома (или exit): ")
|
|
if cmd == "exit":
|
|
break
|
|
i, x = cmd.split()
|
|
if game.split(int(i), float(x)):
|
|
game.current = "Компьютер"
|
|
else:
|
|
print("Ошибка хода")
|
|
except:
|
|
print("Ошибка ввода")
|
|
else:
|
|
game.computer_move()
|
|
game.current = "Игрок"
|
|
|
|
if input("Сыграть ещё? (y/n): ") != "y":
|
|
break
|