70 lines
1.8 KiB
Python
70 lines
1.8 KiB
Python
import random
|
|
import math
|
|
|
|
|
|
class Game:
|
|
def __init__(self):
|
|
self.cells = ["_"] * 10
|
|
|
|
def show(self):
|
|
print("".join(self.cells))
|
|
|
|
def set_digit(self, pos, digit):
|
|
if 0 <= pos < 10 and self.cells[pos] == "_":
|
|
self.cells[pos] = str(digit)
|
|
return True
|
|
return False
|
|
|
|
def computer_move(self):
|
|
free = [i for i, x in enumerate(self.cells) if x == "_"]
|
|
if free:
|
|
pos = random.choice(free)
|
|
digit = random.randint(0, 9)
|
|
self.cells[pos] = str(digit)
|
|
print(f"Компьютер поставил {digit} в позицию {pos}")
|
|
|
|
def result(self):
|
|
num = int("".join(self.cells))
|
|
root = int(math.sqrt(num))
|
|
return root * root == num
|
|
|
|
|
|
while True:
|
|
game = Game()
|
|
|
|
print("\nПервый этап (ваши ходы)")
|
|
while True:
|
|
game.show()
|
|
cmd = input("Введите позицию и цифру (или stop): ")
|
|
if cmd == "stop":
|
|
break
|
|
try:
|
|
pos, digit = map(int, cmd.split())
|
|
if not game.set_digit(pos, digit):
|
|
print("Ошибка")
|
|
except:
|
|
print("Ошибка ввода")
|
|
|
|
print("\nХод компьютера")
|
|
game.computer_move()
|
|
|
|
print("\nФинальный этап")
|
|
while "_" in game.cells:
|
|
game.show()
|
|
try:
|
|
pos, digit = map(int, input("Введите позицию и цифру: ").split())
|
|
if not game.set_digit(pos, digit):
|
|
print("Ошибка")
|
|
except:
|
|
print("Ошибка ввода")
|
|
|
|
game.show()
|
|
|
|
if game.result():
|
|
print("Вы выиграли!")
|
|
else:
|
|
print("Победил компьютер")
|
|
|
|
if input("Сыграть ещё? (y/n): ") != "y":
|
|
break
|