This commit is contained in:
2026-04-18 14:42:00 +05:00
commit ef77f101fa
96 changed files with 3134 additions and 0 deletions

View File

@@ -0,0 +1,55 @@
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