└── game.py /game.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | users = ['John', 'Jack'] 4 | 5 | 6 | def game_turn(active_player_, amount_): 7 | if active_player_ != bot: 8 | print(amount_ * '|') 9 | return input(f"{active_player_}'s turn!\n") 10 | elif active_player_ == bot: 11 | print(amount_ * '|') 12 | print(f"{active_player_}'s turn:") 13 | if amount_ % 4 == 0: 14 | number = '3' 15 | elif amount_ % 4 == 3: 16 | number = '2' 17 | elif amount_ % 4 == 2: 18 | number = '1' 19 | elif amount_ == 1: 20 | number = '1' 21 | else: 22 | number = random.choice(['1', '2', '3']) 23 | print(number) 24 | return number 25 | 26 | 27 | print('How many pencils would you like to use:') 28 | while True: 29 | amount = input() 30 | if not amount.isdigit(): 31 | print("The number of pencils should be numeric") 32 | elif int(amount) == 0: 33 | print("The number of pencils should be positive") 34 | else: 35 | break 36 | 37 | print(f"Who will be the first ({', '.join(users)}):") 38 | while True: 39 | first_player = input() 40 | if first_player not in users: 41 | print(f"Choose between '{users[0]}' and '{users[1]}'") 42 | else: 43 | # bot = users[1] if first_player == users[0] else users[0] 44 | real_man = users[0] 45 | bot = users[1] 46 | active_player = first_player 47 | break 48 | 49 | taken_amount = game_turn(first_player, int(amount)) 50 | while True: 51 | amount = int(amount) 52 | if not taken_amount.isdigit() or int(taken_amount) not in [1, 2, 3]: 53 | print("Possible values: '1', '2' or '3'") 54 | taken_amount = game_turn(active_player, amount) 55 | elif int(taken_amount) > amount: 56 | print("Too many pencils were taken") 57 | taken_amount = game_turn(active_player, amount) 58 | else: 59 | taken_amount = int(taken_amount) 60 | amount -= taken_amount 61 | active_player = bot if active_player == real_man else real_man 62 | if amount == 0: 63 | print(f"{active_player} won!") 64 | break 65 | taken_amount = game_turn(active_player, amount) 66 | --------------------------------------------------------------------------------