康威的生命游戏有逻辑问题

时间:2014-08-22 02:12:47

标签: ruby conways-game-of-life

我正在使用康威的生命游戏。我有一些逻辑问题,它总是以2或3个滴答结束(通常只有2个)。大多数情况下,所有细胞死亡,但偶尔会有1或2只活着。我无法找到导致第二个蜱的行为几乎全部(如果不是完全)死亡的部分。

您是否有人发现可能导致此行为的任何重大问题?

require 'pry'
class Game
  def initialize(size)
    @size = size
    @ticks = 1
    @current_board = Array.new(size) { Array.new(size) { (rand(99) % 5 == 0) ? false : true } }
    @future_board = Array.new(size) { Array.new(size) }
    # @future_board = @current_board
  end

  def is_alive?(y, x)
    if @current_board[y]
      @current_board[y][x]
    end
  end

  def check_neigbors(y, x)
    neighbors = {
      top:          [y-1, x],
      top_right:    [y-1, x+1],
      right:        [y, x+1],
      bottom_right: [y+1, x+1],
      bottom:       [y+1, x],
      bottom_left:  [y+1, x-1],
      left:         [y, x-1],
      top_left:     [y-1, x-1]
    }
    neighbor_state = {
      top:          false,
      top_right:    false,
      right:        false,
      bottom_right: false,
      bottom:       false,
      bottom_left:  false,
      left:         false,
      top_left:     false
    }
    # binding.pry
    neighbors.each { |k, v| neighbor_state[k] = true if is_alive?(v[0], v[1]) }
    live_neighbor_count = 0
    neighbor_state.each_value { |v| live_neighbor_count += 1 if v }
    live_neighbor_count
  end

  def cell_lives(y, x)
    @future_board[y][x] = true
  end

  def cell_dies(y, x)
    @future_board[y][x] = false
  end

  def display_board
    # need to display board here
    system("clear")
    # @current_board.each do
    # |r| puts r.map { |c| c ? 'O' : 'X' }.join(" ")
    # |r| puts r.map { |c| c }.join(" ")
    puts @current_board.map { |row| row.map { |cell| cell ? 'X' : ' ' }.inspect }
    # end
    puts "\nTicks: #{@ticks}"
  end

  def play
    loop do
      display_board
      @current_board.each do |r| # outer loop to iterate through rows
        row_index = @current_board.index(r).to_i
        r.each do |c| # inner loop to iterate through columns
          column_index = r.index(c).to_i
          live_neighbor_count = check_neigbors(row_index, column_index) # count the number of live neighbors
          cell_dies(row_index, column_index) if ( is_alive?(row_index, column_index) ) && live_neighbor_count < 2 # rule 1
          cell_lives(row_index, column_index) if ( is_alive?(row_index, column_index) ) && ( live_neighbor_count == 2 || live_neighbor_count == 3 ) # rule 2
          cell_dies(row_index, column_index) if ( is_alive?(row_index, column_index) ) && live_neighbor_count > 3 # rule 3
          cell_lives(row_index, column_index) if !( is_alive?(row_index, column_index) ) && live_neighbor_count == 3 # rule 4

        end
      end
      if @current_board == @future_board # board is gridlocked. Game over!
        puts "\nGAME OVER!"
        exit!
      else
        @current_board = @future_board # update the board
        @ticks += 1
        sleep(1)
      end
    end
  end
end

print "How large of a grid do you want: "
grid_size = gets.to_i
game = Game.new grid_size
game.play

2 个答案:

答案 0 :(得分:0)

尝试: -

 @current_board = @futureboard.dup

答案 1 :(得分:0)

在康威的生活游戏中,如果你从一个随机排列的细胞开始,大多数人都会因为游戏规则而在第二次打勾时死亡,所以我没有证据表明存在错误

你是如何制作滑翔机或其他东西并确保其表现符合预期?如果它不起作用,那么请发布程序的输出并指出输出错误的第一帧,并发布您期望输出的内容。