той хто грав зі мною в цю гру запропонував мені доробити алгоритм щоб туди можна було використовувати будь які числа там з 3 до 5 і не 21, а наприклад 98
from random import randint
import sys
MIN_NUMBER = 1
MAX_NUMBER = 3
TOTAL = 21
class HumanPlayer:
def next(self, total:int) -> int:
while 1:
try:
move = int(input(f"Введіть хід (можна вводити {MIN_NUMBER} - {MAX_NUMBER} або нову суму): "))
if MIN_NUMBER <= move <= MAX_NUMBER:
return move
if MIN_NUMBER+total < move <= total+MAX_NUMBER:
return move - total
except Exception:
pass
def name(self) -> str:
return "людяка"
class SmartPlayer:
def __init__(self):
self.number = self.think()
def think(self) -> int:
number = MAX_NUMBER + 1
min_number = MIN_NUMBER
while min_number > 0:
while number > MIN_NUMBER + 1:
if (TOTAL - min_number) % number == 0:
print(number)
return number
else:
number -= 1
min_number -= 1
print("100% перемога НЕМОЖЛИВА")
return 10000
def next(self, total:int) -> int:
if (TOTAL - total) > MAX_NUMBER + 1:
move = self.number - total % self.number
else:
move = TOTAL - MIN_NUMBER - total
print(f"Я обережно оберу {move}")
return move
def name(self) -> str:
return "розумаха"
class RandomPlayer:
def next(self, total:int) -> int:
move = randint(MIN_NUMBER,MAX_NUMBER); print(f"А я зроблю {move}")
return move
def name(self) -> str:
return "тупак"
def main() -> None:
if MIN_NUMBER < 1:
sys.exit(0)
elif MAX_NUMBER >= TOTAL - MIN_NUMBER - 1:
sys.exit(0)
while 1:
try:
Opponent = [SmartPlayer(), RandomPlayer()][int(input("Виберіть, з ким граєте (1 - розумаха, 2 - тупак): ")) - 1]
break
except:
pass
players = [HumanPlayer(), Opponent]
total = 0
while total < TOTAL:
print(f"\nСума {total}, ходить {players[0].name()}")
total += players[0].next(total)
players = players[::-1]
print(f"Сума {total}, виграв {players[0].name()}")
if __name__ == "__main__":
main()
незнаю чи працє але ніби все добре :3