Repository navigation
Expand file tree
/
Copy pathdiscreteProject2.py
More file actions
77 lines (66 loc) · 1.77 KB
/
Copy pathdiscreteProject2.py
File metadata and controls
77 lines (66 loc) · 1.77 KB
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
import time
game = []
playerTurn = True
def sumXor():
s = 0
for elem in game:
s^=elem
return s
def isOver():
for elem in game:
if elem!=0:
return False
return True
def printBoard():
for i in range(len(game)):
for j in range(game[i]):
print("X", end=' ')
print()
def whereToEat():
s = sumXor()
if s==0: # losing
for i in range(len(game)):
if game[i]!=0:
return [i, 1]
s_copy = s
msb = 1
while s_copy!=1:
msb<<=1
s_copy>>=1
for i in range(len(game)):
if game[i]&msb:
return [i, game[i]-(game[i]^s)]
return IndexError
print("Give each row's # of elements, each separated by a space")
game = list(map(int, input().split()))
while not isOver():
print()
idx = float("INF")
amt = float("INF")
if playerTurn:
print("Player turn!")
printBoard()
# print(idx>0)
while (idx<0) or (idx>=len(game)) or (idx!=float("INF") and game[idx]==0):
if idx!=float("INF"):
print("Out of bounds!\n")
idx = int(input("What row would you like to take from?\n"))
while amt<=0 or amt>len(game):
if amt!=float("INF"):
print("Invalid amount!\n")
amt = int(input("How many would you like to take from this row?\n"))
else:
print("Computer turn!")
printBoard()
print("Thinking...")
# print(whereToEat())
idx, amt = whereToEat()
print("Computer chose to take " + str(amt) + " from index " + str(idx) + "!")
time.sleep(2)
game[idx]-=amt
playerTurn = not playerTurn
print("Game over!")
if playerTurn:
print("You lose...")
else:
print("You win!!")