Differences between revisions 1 and 9 (spanning 8 versions)
Revision 1 as of 2009-05-22 10:18:16
Size: 3778
Editor: pang
Comment: created section interact/games with "queens on a chess board" and "nim"
Revision 9 as of 2019-04-07 10:08:28
Size: 5539
Editor: chapoton
Comment: py3 print
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()

html("<h1 align=center>Zeros</h1>")
html("<h2 align=center><font color='blue'>How many zeros?</font></h2>")
    
@interact
def zeros(a=selector(buttons=True, nrows=1, values=['Reset'] + [1..B], default=1)):
    if a == 'Reset':
        init()
    html("<center>")
    global B,Br,n,round,tm,t,v
    if a == n:
        if round > 0:
           html("<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:
        html("<font size=+2 color='blue'>Wrong. Try again...</font>")
    html("</center>")
    html("<font size=+%s color='#333'>"%random.randrange(-2,5))
    print ' '*random.randrange(20) + '0'*n
    html("</font>")
    if round > 0:
        html("<br><br><center>Score: %s rounds, Average time: %.1f seconds</center>"%(
                 round, float(tm)/round))
}}}
Line 9: Line 67:
{{{ {{{#!sagecell
Line 22: Line 80:
        queens.remove(queen)         if queen in queens:
    
queens.remove(queen)
Line 38: Line 97:
            print 'Queen at (%d,%d) is threatened by another queen'%(x,y)             print('Queen at (%d,%d) is threatened by another queen' % (x, y))
Line 42: Line 101:
    show(matrix_plot(board, cmap='Oranges' ))
    show(matrix_plot(board, cmap='Oranges' ))
Line 48: Line 106:
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 108:
{{{ {{{#!sagecell
Line 75: Line 133:
}}}
{{{

nim=[1,3,5,7]

nim=[1,3,5,6]
Line 79: Line 136:
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 82: Line 141:
        print 'You have lost'         print('You have lost')
        return
Line 84: Line 144:
        print 'Try to take the last item'         print('Try to take the last item')
Line 86: Line 146:
        print 'Previous situation:'
        print game_repr(nim)
        print('Previous situation:')
        print(game_repr(nim))
Line 90: Line 150:
        print 'Your move:'
        print game_repr(nim)
        print('Your move:')
        print(game_repr(nim))
        if max(nim)==0:
            print('You win')
            return
Line 93: Line 156:
        print 'My move:'         print('My move:')
Line 95: Line 158:
            myheap,myamount=move_nim(nim)             myheap, myamount = move_nim(nim)
Line 98: Line 161:
            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 100: Line 164:
        print game_repr(nim)         print(game_repr(nim))
        if max(nim)==0:
            print('I win')
        else:
            print('Please move again')
Line 102: Line 170:
        print 'Please choose a heap and the amount to substract from that heap'
        print game_repr(nim)
        print('Choose a heap and the amount to substract from that heap')
        print(game_repr(nim))

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

zeros.png

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

queens.png

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

nim.png

interact/games (last edited 2020-06-02 22:07:41 by kcrisman)