-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnim_game_bot.py
130 lines (123 loc) · 5.38 KB
/
nim_game_bot.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
def computer_chooses_move(n, m):
#the computer always leaves a multiple of (m+1) pieces on the board, and if it can't, it just takes the maximum number of pieces
if m>=n:
return n
else:
for i in range(n, -1, -1):
for j in range(1, n):
if i == j*(m+1):
return n-i
def user_chooses_move(n, m):
while True:
global global_pieces_taken
global_pieces_taken = int(input("\nHow many pieces will you take? "))
if global_pieces_taken<=m and global_pieces_taken<=n and global_pieces_taken>0:
return global_pieces_taken
else:
print("\nOops! Unexpected move. Try again!")
def match():
print("\nWelcome to NIM game! Choose:")
game_mode = input("\n1 - Isolated match \n2 - Championship\n")
if game_mode == '1':
print("\n You chose an isolated match")
elif game_mode == '2':
print("\nYou chose a championship.")
computer = 0
user = 0
counter = 1
else:
print("\nUnexpected value. Please, choose from one of the two")
return False
while True:
if game_mode == '2' and counter == 4:
#checking if the championship is over
print("\n**** End of the championship! ****")
print("\nScoreboard: You", user, "X", computer,"Computer")
break
elif game_mode == '2':
#displaying the round number
print("\n**** Round",counter,"****")
n = int(input("\nHow many pieces? "))
m = int(input("Maximum of pieces per move? "))
if m<1 or n<1:
print("\nInvalid value. Choose a positive integer for both variables.")
continue
if n%(m+1) == 0:
#if n is multiple of (m+1), the user must begin so that the computer wins
c = 0
print("\nYou begin!")
while True:
c+=1
if c%2 ==0:
if computer_chooses_move(n, m) < 2:
print("\nThe computer took one piece.")
else:
print("\nThe computer took",computer_chooses_move(n, m),"pieces.")
n = n - computer_chooses_move(n, m)
if n == 0:
if game_mode == '1':
#if the game mode is an isolated match, there'll be one round only
print("\nEnd of the game. Computer wins!")
print("Thank you for playing!")
return True
elif game_mode == '2':
#if the game mode is a championship, it'll go to the next round
print("\nEnd of match. Computer wins!")
computer+=1
counter+=1
break
elif n < 2:
print("Now there is only one piece on the board.")
else:
print("Now there are",n,"pieces on the board.")
else:
if user_chooses_move(n, m) < 2:
print("\nYou took one piece.")
else:
print("\nYou took",global_pieces_taken, "pieces.")
n = n - global_pieces_taken
if n == 0:
return "how"
elif n < 2:
print("Now there is only one piece on the board.")
else:
print("Now there are",n,"pieces on the board.")
else:
#if n isn't multiple of (m+1), the computer must begin so that it wins
c = 0
print("\nThe computer begins!")
while True:
c+=1
if c%2 ==0:
if user_chooses_move(n, m) < 2:
print("\nYou took one piece.")
else:
print("\nYou took",global_pieces_taken, "pieces.")
n = n - global_pieces_taken
if n == 0:
return "how"
elif n < 2:
print("Now there is only one piece on the board.")
else:
print("Now there are",n, "pieces on the board.")
else:
if computer_chooses_move(n, m) < 2:
print("\nThe computer took one piece.")
else:
print("\nThe computer took",computer_chooses_move(n, m),"pieces.")
n = n - computer_chooses_move(n, m)
if n == 0:
if game_mode == '1':
print("\nEnd of the game. Computer wins!")
print("Thank you for playing!")
return True
elif game_mode == '2':
print("\nEnd of round. Computer wins!")
computer+=1
counter+=1
break
elif n < 2:
print("Now there is only one piece on the board.")
else:
print("Now there are",n, "pieces on the board.")
match()