-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
115 lines (93 loc) · 2.92 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
from random import randint
# establish number of pencils in game
# returns number of pencils in the pot
def pick_starting_pencils():
print("How many pencils would you like to use:")
while True:
try:
pencil_count = int(input())
except ValueError:
print("The number of pencils should be numeric")
continue
if pencil_count < 0:
print("The number of pencils should be numeric") # This was a project requirement
continue
if pencil_count == 0:
print("The number of pencils should be positive")
continue
else:
return pencil_count
# query which player should go first
# return initial 'active_player'
def pick_starting_player():
players = ["Jack", "John"] # Jack is the name of the AI player
print("Who will be the first (John, Jack):")
while True:
starting_player = input()
if starting_player not in players:
print(f"Choose between Jack and John")
else:
return starting_player
# switch players
# returns name of active player
def player_switcher(player):
if player == "Jack":
return "John"
else:
return "Jack"
# AI turn
# returns number of pencils removed
def jack_turn(pencil_count):
print("|" * pencil_count)
print("Jack's turn:")
losing_positions = range(1, (pencil_count + 1), 4)
if pencil_count not in losing_positions:
for position in losing_positions:
if position + 1 == pencil_count:
print(1)
return 1
elif position + 2 == pencil_count:
print(2)
return 2
elif position + 3 == pencil_count:
print(3)
return 3
else:
if pencil_count == 1:
print(1)
return 1
else:
taken = randint(1, 3)
print(taken)
return taken
# Human turn
# returns number of pencils removed
def john_turn(pencil_count):
print("|" * pencil_count)
print("John's turn!")
while True:
remove_count = input()
valid_answer = ["1", "2", "3"]
if remove_count in valid_answer:
if int(remove_count) <= pencil_count:
return int(remove_count)
else:
print("Too many pencils were taken")
continue
else:
print("Possible values: '1', '2' or '3'")
continue
# play the game
pencils = pick_starting_pencils() # Global
active_player = pick_starting_player() # Global
while True:
if pencils > 0:
if active_player == "Jack":
pencils -= jack_turn(pencils)
active_player = player_switcher(active_player)
else:
pencils -= john_turn(pencils)
active_player = player_switcher(active_player)
else:
print(f"{active_player} won!")
break