[#6219] Ruby連載 第4話 — matz@... (Yukihiro Matsumoto)

まつもと ゆきひろです

32 messages 1998/02/04
[#6221] Re: Ruby連載 第4話 — Shin-ichiro HARA <sinara@...> 1998/02/04

原です。

[#6224] Re: Ruby連載 第4話 — Yasunari Momoi <conan@...> 1998/02/04

ももちゃん@あるもにこすです.

[#6225] Re: Ruby連載 第4話 — matz@... (Yukihiro Matsumoto) 1998/02/04

まつもと ゆきひろです

[#6249] Re: i++ — 助田 雅紀 <masaki.suketa@...>

助田です。

33 messages 1998/02/04
[#6252] Re: i++ — gotoken@... (GOTO Kentaro) 1998/02/05

ことけんです

[#6255] Re: i++ — matz@... (Yukihiro Matsumoto) 1998/02/05

まつもと ゆきひろです

[#6260] Re: i++ — Yuji Shigehiro <sigehiro@...> 1998/02/05

しげひろです.

[#6314] RE: ruby's design policy (Re: I'd like to subscr ibe this ML) — 助田 雅紀 <masaki.suketa@...>

オブジェクト指向周辺をぶらぶらしている助田です。

11 messages 1998/02/06

[#6333] ruby 流に添削して下さい — nkon@...2.3web.ne.jp

13 messages 1998/02/07
[#6335] Re: ruby 流に添削して下さい — OZAWA Sakuro <crouton@...> 1998/02/07

さくです.

[#6372] ruby 1.1b7 released — matz@... (Yukihiro Matsumoto)

まつもと ゆきひろです

24 messages 1998/02/10
[#6402] parsedate (Re: ruby 1.1b7 released) — WATANABE Hirofumi <watanabe@...> 1998/02/13

わたなべです.

[#6405] Re: parsedate (Re: ruby 1.1b7 released) — Tadayoshi Funaba <tadf@...> 1998/02/13

ふなばです。

[#6407] Re: parsedate (Re: ruby 1.1b7 released) — matz@... (Yukihiro Matsumoto) 1998/02/13

まつもと ゆきひろです

[#6373] call for scripts — matz@... (Yukihiro Matsumoto)

まつもと ゆきひろです

133 messages 1998/02/10
[#6414] Re: call for scripts — Terutuoshi Kaneshiro <k-teru@...06.odn.ne.jp> 1998/02/14

金城です.

[#6428] Re: call for scripts — matz@... (Yukihiro Matsumoto) 1998/02/16

まつもと ゆきひろです

[#6448] Re: call for scripts — Terutuoshi Kaneshiro <k-teru@...06.odn.ne.jp> 1998/02/16

金城です.

[#6452] Re: call for scripts — matz@... (Yukihiro Matsumoto) 1998/02/17

まつもと ゆきひろです

[#6481] Re: call for scripts — Kikutani Makoto <kikutani@...> 1998/02/17

きくたに@ぷ〜たろ〜です。

[#6483] Re: call for scripts — OZAWA Sakuro <crouton@...> 1998/02/17

さくです.

[#6495] Re: call for scripts — WATANABE Hirofumi <watanabe@...> 1998/02/18

わたなべです.

[#6416] Re: call for scripts — shugo@... (Shugo Maeda) 1998/02/14

前田です。

[#6417] rgrep (Re: call for scripts) — shugo@... (Shugo Maeda) 1998/02/15

前田です。

[#6423] Re: rgrep (Re: call for scripts) — matz@... (Yukihiro Matsumoto) 1998/02/16

まつもと ゆきひろです

[#6612] Re: call for scripts — Tadayoshi Funaba <tadf@...> 1998/02/21

ふなばです。

[#6582] File::Separator on cygwin — OZAWA Sakuro <ozawa@...>

小澤さくです。

19 messages 1998/02/20
[#6583] Re: File::Separator on cygwin — WATANABE Hirofumi <watanabe@...> 1998/02/20

わたなべです.

[#6586] Re: File::Separator on cygwin — OZAWA Sakuro <ozawa@...> 1998/02/20

小澤さくです。

[#6591] ruby 1.1b8 released — matz@... (Yukihiro Matsumoto)

まつもと ゆきひろです

32 messages 1998/02/20
[#6620] rbc.rb and binding — shugo@... (Shugo Maeda) 1998/02/22

前田です。

[#6629] Re: rbc.rb and binding — keiju@... (石塚圭樹 ) 1998/02/23

けいじゅ@日本ラショナルソフトウェアです.

[#6643] Re: rbc.rb and binding — shugo@... (Shugo Maeda) 1998/02/23

前田です。

[#6649] Re: rbc.rb and binding — keiju@... (石塚圭樹 ) 1998/02/23

けいじゅ@日本ラショナルソフトウェアです.

[#6650] Re: rbc.rb and binding — matz@... (Yukihiro Matsumoto) 1998/02/23

まつもと ゆきひろです

[#6667] JRI 0.5 worked (was Re: rbc.rb and binding) — OZAWA Sakuro <crouton@...> 1998/02/23

さくです.

[#6687] Re: JRI 0.5 worked (was Re: rbc.rb and binding) — shugo@... (Shugo Maeda) 1998/02/24

前田です。

[#6712] JRI and JDBC — OZAWA Sakuro <ozawa@...> 1998/02/24

小澤さくです。

[#6635] hello — WATANABE Tetsuya <tetsu@...>

渡辺哲也と申します。

15 messages 1998/02/23

[#6706] Re: Counter class — toyofuku@...

豊福@パパイヤです。

12 messages 1998/02/24

[#6735] Mutex/ConditionVariable/Queue — shugo@... (Shugo Maeda)

前田です。

27 messages 1998/02/24
[#6746] Re: Mutex/ConditionVariable/Queue — keiju@... (石塚圭樹 ) 1998/02/25

けいじゅ@日本ラショナルソフトウェアです.

[#6747] Re: Mutex/ConditionVariable/Queue — shugo@... (Shugo Maeda) 1998/02/25

前田です。

[#6752] Re: Mutex/ConditionVariable/Queue — senda@... 1998/02/25

From: shugo@po.aianet.ne.jp (Shugo Maeda)

[#6754] Re: Mutex/ConditionVariable/Queue — shugo@... (Shugo Maeda) 1998/02/25

前田です。

[#6756] Re: Mutex/ConditionVariable/Queue — senda@... 1998/02/25

From: shugo@po.aianet.ne.jp (Shugo Maeda)

[#6786] Re: Mutex/ConditionVariable/Queue — senda@...

From: shugo@po.aianet.ne.jp (Shugo Maeda)

33 messages 1998/02/26
[#6791] Re: Mutex/ConditionVariable/Queue — shugo@... (Shugo Maeda) 1998/02/26

前田です。

[#6794] Re: Mutex/ConditionVariable/Queue — shugo@... (Shugo Maeda) 1998/02/26

前田です。

[#6796] Re: Mutex/ConditionVariable/Queue — keiju@... (石塚圭樹 ) 1998/02/26

けいじゅ@日本ラショナルソフトウェアです.

[ruby-list:6611] Game of Life (Re: call for scripts)

From: Takahiro Maebashi <maebashi@...>
Date: 1998-02-21 15:20:03 UTC
List: ruby-list #6611
前橋です。サイン本コンテスト(?)に応募します。

tk によるライフゲームです。
有名なので知っていると思いますが、碁盤の目上に配置された生命体が、一定
のルールにしたがって生成/消滅を繰り返していく様を見て楽しむというもの
です。ルールは、
・ある生命体の周囲 8つのマス目のうち 2つあるいは 3つに生命体が存在すれ
  ば生き残るがそれ以外では過疎あるいは過密で死んでしまう
・空いている場所の周りにちょうど 3つの生命体がいれば、そこに新しい生命
  体が生まれる
です。

使い方は起動すればわかるでしょう。マウスで盤面をクリックすることにより
配置/除去ができます。

# サーチエンジンでライフゲームを調べたら、Java アプレットのライフゲー
# ムが山のようにでてきた。。。

----------------------------------------------------------------------
#! /usr/local/bin/ruby

# Game of Life

require "tk"

class LifeGame
  def initialize(width=80, height=23)
    @width = width
    @height = height
    default = [-width-1, -width, -width+1, -1, 1, width-1, width, width+1]
    @neighbors = []
    @neighbors.fill(default, 0, width*height)
    (0..width*height-1).each {|i|
      x = i % width
      y = i / width
      if x == 0
	@neighbors[i] = [-width, -width+1, 1, width, width+1]
      elsif x == @width-1
	@neighbors[i] = [-width-1, -width, -1, width-1, width]
      end
      if y == 0
	@neighbors[i] = @neighbors[i].clone
	@neighbors[i].delete_if {|v| v < -1}
      elsif y == @height-1
	@neighbors[i] = @neighbors[i].clone
	@neighbors[i].delete_if {|v| v > 1}
      end
    }
    @grid = {}
    center = height / 2 * width + width / 2
    @grid[center] = true
    @grid[center+1] = true
    @grid[center+width] = true
    @grid[center+width-1] = true
    @grid[center+width+width] = true
  end

  def nextgen
    n = {}
    @grid.each_key {|i|
      n[i] += 0
      @neighbors[i].each {|pos| n[i+pos] += 1}
    }
    n.each {|i, n|
      @grid[i] = if (n == 3 || @grid[i] && n == 2) then true else nil end
    }
  end

  def run
    loop {
      display
      nextgen
      break unless gets
    }
  end

  def display
    s = ' ' * (@width * @height)
    @grid.each_key {|i| s[i,1]='*'}
    print s, "\n"
  end
end

class TkLifeGame < LifeGame
  include Tk
  def initialize(width=80, height=80, rectsize=6)
    super(width, height)
    @rectsize = rectsize
    @goflag = false

    @canvas = TkCanvas.new(nil,
			   'width'=>width * rectsize,
			   'height'=>height * rectsize,
			   'borderwidth'=>1,
			   'relief'=>'sunken')
    @nextbutton = TkButton.new(nil,
			       'text'=>'next',
			       'command'=>proc {nextgen; display})
    @gobutton = TkButton.new(nil,
			     'text'=>'go',
			     'command'=>proc {
			       @goflag = !@goflag
			       if @goflag
				 @gobutton.text 'stop'
				 go
			       else
				 @gobutton.text 'go'
			       end
			     })
    @quitbutton = TkButton.new(nil,
			       'text'=>'quit',
			       'command'=>proc {exit})
    @canvas.pack
    @nextbutton.pack('side'=>'left')
    @gobutton.pack('side'=>'left')
    @quitbutton.pack('side'=>'right')

    @prevgrid = {}
    @rectangles = {}
    @canvas.bind '1', proc {|x, y|
      i = x / @rectsize + y / @rectsize * @width
      if @grid[i]
	@grid[i] = nil
      else
	@grid[i] = true
      end
      display
      update
    }, '%x %y'
  end

  def go
    nextgen
    display
    update
    if @goflag
      after 0, proc {go}
    end
  end

  def run
    display
    mainloop
  end

  def display
    @grid.each_key {|i|
      if @prevgrid[i]
	@prevgrid[i] = nil
      else
	setrect(i)
      end
    }
    @prevgrid.each_key {|i|
      resetrect(i)
    }
    @prevgrid = @grid.dup
  end

  def setrect(i)
    x = i % @width
    y = i / @width
    @rectangles[i] = TkcRectangle.new(@canvas,
				      x * @rectsize,
				      y * @rectsize,
				      x * @rectsize + @rectsize - 2,
				      y * @rectsize + @rectsize - 2,
				      'fill'=>'black')
  end

  def resetrect(i)
    @rectangles[i].destroy
    @rectangles[i] = nil
  end

end

#g = LifeGame.new
g = TkLifeGame.new
g.run

In This Thread