def PrintWinner(code):
if code == 1:
print "Sage wins"
if code == 2:
print "You won...this time"
def PrintPosition(matches):
print matches*'1'
def YourMove(matches,Choose):
matches -= Choose
print "You chose ", Choose, " matches"
PrintPosition(matches)
if matches <= 0:
PrintWinner(1)
if matches == 1:
PrintWinner(2)
return matches
def SageMove(matches):
d = randrange(1,3,1)
print "I chose ", d, " matches"
matches -= d
PrintPosition(matches)
if matches <= 0:
PrintWinner(2)
if matches == 1:
PrintWinner(1)
return matches
@interact
def Nim(N=slider([5..100], label='Number of matches',default=11),Choose=selector(buttons=True, nrows=1, values=[0]+[1]+[2], default=0),auto_update=True):
print "<html><h1 align=center>Nim</h1></html>"
print "<html><h4 align=center>The person who takes the last match loses</h4></html>"
matches = N
PrintPosition(matches)
while Choose != 0 and matches>=2:
matches=YourMove(matches,Choose)
matches=SageMove(matches)
#Choose = 0
N=matches
|
|
Click to the left again to hide and once more to show the dynamic interactive window
|