中村です。 In article <199802211722.CAA01450 / niagara.shiojiri.ne.jp> OZAWA Sakuro <crouton / po.shiojiri.ne.jp> wrote: > 8-Queensはもう出てますし,あとは騎士巡回とハノイの塔で主要なパズルもの > は網羅したことになりますかね. Python 1.4 の Demo の下にあった hanoi.py を コンバートしてみました。(オリジナリティー 0 ) だいたいは動いているように見えます。 width, height = tk.getint(c['width']), tk.getint(c['height']) をどうやったら良いのか分からなかったので、 値を直接入れてしまいました。 ---------------------------------------------------------------------- #!/usr/local/bin/ruby # # Usage: tkhanoi [n [bitmapfile]] # # from 1998.02.22 # converted by Noritsugu Nakamura require "tkclass" class Tkhanoi def initialize(n, bitmap = nil) @n = n @c = Canvas.new @c.pack width = 300 height = 211 if bitmap Bitmap.new(@c, width/2, height/2, 'bitmap' => bitmap, 'foreground' => 'blue') end pegwidth = 10 pegheight = height / 2 pegdist = width / 3 x1, y1 = (pegdist - pegwidth) / 2, height * 1 / 3 x2, y2 = x1 + pegwidth, y1 + pegheight @pegs = [] for i in 0..2 p = Rectangle.new(@c, x1, y1, x2, y2, 'fill' => 'black') @pegs.push(p) x1, x2 = x1 + pegdist, x2 + pegdist end pieceheight = pegheight / 16 maxpiecewidth = pegdist * 2 / 3 minpiecewidth = 2 * pegwidth @pegstate = [[], [], []] @pieces = {} x1, y1 = (pegdist - maxpiecewidth) / 2, y2 - pieceheight - 2 x2, y2 = x1 + maxpiecewidth, y1 + pieceheight dx = (maxpiecewidth - minpiecewidth) / (2 * [1 , @n -1].max) for i in 0..@n-1 p = Rectangle.new(@c, x1, y1, x2, y2, 'fill' => 'red') @pieces[@n - i] = p @pegstate[0].push(@n - i) x1, x2 = x1 + dx, x2-dx y1, y2 = y1 - pieceheight - 2, y2 - pieceheight - 2 Tk.root.update # Tk.root.after(25) end run Tk.root.title("Hanoi") Tk.root.iconname("Hanoi") Tk.mainloop end def run while 1 hanoi(@n, 0, 1, 2) hanoi(@n, 1, 2, 0) hanoi(@n, 2, 0, 1) hanoi(@n, 0, 2, 1) hanoi(@n, 2, 1, 0) hanoi(@n, 1, 0, 2) end end def report(i, a, b) @pegstate[a].pop p = @pieces[i] ax1, ay1, ax2, ay2 = @c.bbox(@pegs[a]) while 1 x1, y1, x2, y2 = @c.bbox(p) break if y2 < ay1 @c.move(p, 0, -1) Tk.root.update end bx1, by1, bx2, by2 = @c.bbox(@pegs[b]) newcenter = (bx1 + bx2) / 2 while 1 x1, y1, x2, y2 = @c.bbox(p) center = (x1 + x2) / 2 break if center == newcenter if center > newcenter @c.move(p, -1, 0) else @c.move(p, 1, 0) end Tk.root.update end pieceheight = y2 - y1 - 2 newbottom = by2 - pieceheight * @pegstate[b].length - 2 while 1 x1, y1, x2, y2 = @c.bbox(p) break if y2 >= newbottom @c.move(p, 0, 1) Tk.root.update end @pegstate[b].push(i) end def hanoi(n, a, b, c) return if n <= 0 hanoi(n-1, a, c, b) report(n, a, b) hanoi(n-1, c, b, a) end end if ARGV[0] n = ARGV[0].to_i else n = 4 end if ARGV[1] bitmap = ARGV[1] if bitmap[0,1] != '@' bitmap = '@' + bitmap end else bitmap = nil end Tkhanoi.new(n, bitmap) 中村典嗣 E-mail: nnakamur / mxq.meshnet.or.jp