自己拿ruby写的一个简单的生命游戏

朋友买书送了本Ruby完全自学手册,遂学之


第一次写ruby代码,感觉挺适合写算法的东西,使用了TK库,有兴趣的同学可以玩玩。另,Tk canvas界面

很卡(更新界面代码没有new新的控件),有人若是知道原因能否指明,先谢过

require 'tk'

$g_ground_width = 600
$g_ground_height = 600

root = TkRoot.new do
  title "LifeGame"
  geometry $g_ground_width.to_s  + "x" + $g_ground_height.to_s
end

$canvas = TkCanvas.new(root)
$canvas.grid :stick => 'news', :column => 0, :row => 0

TkGrid.columnconfigure( root, 0, :weight => 1)
TkGrid.rowconfigure( root, 0, :weight => 1)

class Cell
  @@size = 10
  
  def initialize
    #puts "come in Cell's init"
    @alive = false;
    @readyAlive = false;
    @x = 0
    @y = 0
    
    @tkRect
    #puts "end of Cell's init"
  end
  
  def self.size
    @@size
  end
  
  def setX(x)
    @x = x
  end
  
  def setY(y)
    @y = y
  end
  
  def getX
    @x
  end
  
  def getY
    @y
  end
  
  def wakeUp
    @alive = true
    @tkRect[:fill] = "black"
  end
  
  def kill
    @alive = false
    @tkRect[:fill] = "white"
  end
  
  def readyAlive(alive)
    @readyAlive = alive
    @readyAlive
  end
  
  def getReadyAlive
    @readyAlive
  end
  
  def isAlive
    @alive
  end
  
  def initTkRect
    @tkRect = TkcRectangle.new($canvas, @x, @y, @x + @@size, @y + @@size)
    @tkRect[:fill] = "white"

  end
  
end

class LifeGame
  def initialize
    @col = $g_ground_width / Cell.size
    @row = $g_ground_height / Cell.size
      
    @cellMap = Array.new(@col){ Array.new(@row) }
      
    #puts "come in LifeGame's init"
    num = 0
    
    for i in (0..@col-1)
      for j in (0..@row-1)
        @cellMap[i][j] = Cell.new
        @cellMap[i][j].setX(i * Cell.size)
        @cellMap[i][j].setY(j * Cell.size)
        @cellMap[i][j].initTkRect
        num += 1
      end
    end
    puts "total new "+num.to_s+"Cell"
      
=begin too mass
    @cellMap[20][20].wakeUp
    @cellMap[21][20].wakeUp
    @cellMap[23][20].wakeUp
    @cellMap[24][20].wakeUp
    
    @cellMap[20][22].wakeUp
    @cellMap[21][22].wakeUp
    @cellMap[23][22].wakeUp
    @cellMap[24][22].wakeUp
    
    @cellMap[22][18].wakeUp
    @cellMap[22][19].wakeUp
    @cellMap[22][20].wakeUp
    @cellMap[22][21].wakeUp
    @cellMap[22][22].wakeUp
    @cellMap[22][23].wakeUp
    @cellMap[22][24].wakeUp
=end
    
=begin
    @cellMap[29][29].wakeUp
    @cellMap[28][30].wakeUp
    @cellMap[29][30].wakeUp
    @cellMap[30][30].wakeUp
=end
    
    for i in (0..300)
      @cellMap[rand(40)+10][rand(40)+10].wakeUp
    end
    
    puts "end of LifeGame's init"
    
  end
  
  def findNeighbor(i, j)
    aliveNum = 0
    [-1, 0, 1].each do |dx|
      [-1, 0, 1].each do |dy|
        
        if (dx == 0 and dy == 0)
          next
        end
        
        if (i + dx < 0 or i + dx >= @col)
          next
        end
        
        if (j + dy < 0 or j + dy >= @row)
          next
        end
          
        if (@cellMap[i+dx][j+dy].isAlive)
          aliveNum += 1
        end
        
      end
    end
    aliveNum
  end
  
  def step
    for i in (0..@col-1)
      for j in (0..@row-1)
        #TODO
        num = findNeighbor(i, j)
        #puts "end findNeighbor"
        
        #puts "before change"
        if num == 3
          @cellMap[i][j].readyAlive(true)
        elsif num == 2
          @cellMap[i][j].readyAlive(@cellMap[i][j].isAlive)
        else
          #if num >0
          #  puts i.to_s+", "+j.to_s+": "+num.to_s
          #end
          @cellMap[i][j].readyAlive(false)
        end
        
        #puts "end step"
        
      end
    end
  
    for i in (0..@col-1)
      for j in (0..@row-1)
        
        if @cellMap[i][j].getReadyAlive
          @cellMap[i][j].wakeUp
        else
          @cellMap[i][j].kill
        end
        
        @cellMap[i][j].readyAlive(false)
      end
    end

  end
  
  def renderAll
    for i in (0..@col-1)
      for j in (0..@row-1)
        if @cellMap[i][j].isAlive()
          #puts i.to_s+","+j.to_s+" wakeUp !"
          #puts @cellMap[i][j].getX.to_s
          #puts @cellMap[i][j].getY.to_s
          #puts Cell.size().to_s
          TkcRectangle.new($canvas, @cellMap[i][j].getX, @cellMap[i][j].getY, @cellMap[i][j].getX + Cell.size, @cellMap[i][j].getY + Cell.size).fill 'black'
        end 
      end
    end
  end
  
end

$g_gameThread;

refreshThread = Thread.new do
  puts "start thread"
  puts $g_gameThread
  $g_gameThread = LifeGame.new
  puts "new LifeGame"
  puts $g_gameThread
  
  loop do
    #sleep(0.1)
    $g_gameThread.step()
    puts "======================================================"
    #$g_gameThread.renderAll()

    #x = rand(590)
    #y = rand(590)
    #TkcRectangle.new(@canvas, x,y,x+20,y+20).fill 'black'
  end
end

Tk.mainloop


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值