last lab
This commit is contained in:
89
labPython/lab18-19/task_1.py
Normal file
89
labPython/lab18-19/task_1.py
Normal file
@@ -0,0 +1,89 @@
|
||||
import random
|
||||
|
||||
|
||||
class Game:
|
||||
def __init__(self):
|
||||
self.red = 2021
|
||||
self.green = 2022
|
||||
self.current = "Игрок"
|
||||
|
||||
def get_divisors(self, n):
|
||||
return [i for i in range(1, n + 1) if n % i == 0]
|
||||
|
||||
def move(self, color, n):
|
||||
if color == "r":
|
||||
if self.green == 0:
|
||||
return False
|
||||
if n in self.get_divisors(self.green) and n <= self.red:
|
||||
self.red -= n
|
||||
return True
|
||||
elif color == "g":
|
||||
if self.red == 0:
|
||||
return False
|
||||
if n in self.get_divisors(self.red) and n <= self.green:
|
||||
self.green -= n
|
||||
return True
|
||||
return False
|
||||
|
||||
def computer_move(self):
|
||||
if self.red == 0 and self.green == 0:
|
||||
return
|
||||
|
||||
options = []
|
||||
|
||||
if self.red > 0 and self.green > 0:
|
||||
for d in self.get_divisors(self.green):
|
||||
if d <= self.red:
|
||||
options.append(("r", d))
|
||||
for d in self.get_divisors(self.red):
|
||||
if d <= self.green:
|
||||
options.append(("g", d))
|
||||
|
||||
if not options:
|
||||
return
|
||||
|
||||
color, n = random.choice(options)
|
||||
self.move(color, n)
|
||||
|
||||
if color == "r":
|
||||
print(f"Компьютер убрал {n} красных камней")
|
||||
else:
|
||||
print(f"Компьютер убрал {n} зелёных камней")
|
||||
|
||||
def is_over(self):
|
||||
return self.red == 0 and self.green == 0
|
||||
|
||||
|
||||
while True:
|
||||
game = Game()
|
||||
|
||||
while True:
|
||||
print(f"\nКрасные: {game.red}, Зелёные: {game.green}")
|
||||
|
||||
if game.current == "Игрок":
|
||||
try:
|
||||
color = input("Выберите цвет (r/g) или exit: ")
|
||||
if color == "exit":
|
||||
break
|
||||
|
||||
n = int(input("Введите количество: "))
|
||||
|
||||
if game.move(color, n):
|
||||
if game.is_over():
|
||||
print("Вы победили!")
|
||||
break
|
||||
game.current = "Компьютер"
|
||||
else:
|
||||
print("Некорректный ход")
|
||||
except:
|
||||
print("Ошибка ввода")
|
||||
|
||||
else:
|
||||
game.computer_move()
|
||||
if game.is_over():
|
||||
print("Победил компьютер")
|
||||
break
|
||||
game.current = "Игрок"
|
||||
|
||||
if input("Сыграть ещё? (y/n): ") != "y":
|
||||
break
|
||||
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
|
||||
69
labPython/lab18-19/task_3.py
Normal file
69
labPython/lab18-19/task_3.py
Normal 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
|
||||
Reference in New Issue
Block a user