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,69 @@
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