Size: 3778
Comment: created section interact/games with "queens on a chess board" and "nim"
|
Size: 5566
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 6: | Line 6: |
== Zeros == Have you ever wished you could count the number of zero digits in a long number like Rainman? by William Stein and Sequoia Lefthand {{attachment:zeros.png}} {{{#!sagecell import random def init(): global B,Br,n,round,tm,t,v B = 40 Br = 15 n = 1 round = 0 tm = 0 t = walltime() init() print "<html><h1 align=center>Zeros</h1></html>" print "<html><h2 align=center><font color='blue'>How many zeros?</font></h2></html>" @interact def zeros(a=("",selector(buttons=True, nrows=1, values=['Reset'] + [1..B], default=1))): if a == 'Reset': init() print "<html><center>" global B,Br,n,round,tm,t,v if a == n: if round > 0: print "<font size=+3 color='red'>RIGHT</font>" r = walltime() - t tm += r round += 1 t = walltime() while True: n2 = random.randrange(1,Br) if n2 != n: n = n2 break if Br < B: Br += 2 elif round > 0: print "<font size=+2 color='blue'>Wrong. Try again...</font>" print "</center>" print "<font size=+%s color='#333'>"%random.randrange(-2,5) print ' '*random.randrange(20) + '0'*n print "</font>" if round > 0: print "<br><br><center>Score: %s rounds, Average time: %.1f seconds</center>"%( round, float(tm)/round) print "</html>" }}} |
|
Line 9: | Line 68: |
{{{ | {{{#!sagecell |
Line 48: | Line 107: |
Play nim against a perfect oponent. This interaction exemplifies the use of persistent data, and the auto_update=False option coded by mhansen and included in sage 3.3. | Play nim against a perfect opponent. This interaction exemplifies the use of persistent data, and the auto_update=False option coded by mhansen and included in sage 3.3. |
Line 50: | Line 109: |
{{{ | {{{#!sagecell |
Line 76: | Line 135: |
{{{ nim=[1,3,5,7] |
{{{#!sagecell nim=[1,3,5,6] |
Line 79: | Line 138: |
def _(heap=input_box(default=1), amount=input_box(default=0), auto_update=False ): | def _(heap=selector(range(len(nim)), buttons=True), amount=selector(range(max(nim)+1), buttons=True), auto_update=False): |
Line 83: | Line 144: |
return | |
Line 92: | Line 154: |
if max(nim)==0: print 'You win' return |
|
Line 98: | Line 163: |
myheap=min(heap_number for heap_number, heap_size in enumerate(heap_size) if heap_size>0) | myheap=min(heap_number for heap_number, heap_size in enumerate(nim) if heap_size>0) myamount=1 |
Line 101: | Line 167: |
if max(nim)==0: print 'I win' else: print 'Please move again' |
|
Line 102: | Line 172: |
print 'Please choose a heap and the amount to substract from that heap' | print 'Choose a heap and the amount to substract from that heap' |
Sage Interactions - Games and Diversions
goto interact main page
Zeros
Have you ever wished you could count the number of zero digits in a long number like Rainman?
by William Stein and Sequoia Lefthand
Queens on board
An interaction to play with the problem of placing eight queens on a board so that they do not threaten each other. This interaction exemplifies the use of persistent data, and the auto_update=False option coded by mhansen and included in sage 3.3. by Pablo Angulo
Nim
Play nim against a perfect opponent. This interaction exemplifies the use of persistent data, and the auto_update=False option coded by mhansen and included in sage 3.3. by Pablo Angulo