-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLP2.py
32 lines (28 loc) · 924 Bytes
/
LP2.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
import random
def nim_game():
heap_size = 13 # Number of matchsticks in the heap
current_player = "Max"
while heap_size > 0:
print(f"\nCurrent heap size: {heap_size}")
if current_player == "Max":
move = max_heap_move(heap_size)
else:
move = min_heap_move(heap_size)
print(f"{current_player} takes {move} matchsticks.")
heap_size -= move
current_player = "Min" if current_player == "Max" else "Max"
print(f"\n{current_player} wins!")
def max_heap_move(heap_size):
first_bit = heap_size & 1
heap_size_without_first_bit = heap_size >> 1
result = heap_size_without_first_bit + first_bit
return heap_size-result
def min_heap_move(heap_size):
b=int(heap_size/2)
if b>1:
a= random.randint(1,b)
else :
a= 1
return a
if __name__ == "__main__":
nim_game()