last lab
This commit is contained in:
55
labPython/lab18-19/task_2.py
Normal file
55
labPython/lab18-19/task_2.py
Normal 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
|
||||
Reference in New Issue
Block a user