ruby 康威的生命游戏

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 康威的生命游戏相关的知识,希望对你有一定的参考价值。

require 'rspec'
require 'space'

describe Space do
  it 'should be an instance of Space' do
    test_space = Space.new(2,3)
    test_space.should be_an_instance_of Space
  end

  it 'should initialize with coordinates and "false" for dead state' do
    test_space = Space.new(2,3)
    test_space.x.should eq 2
    test_space.y.should eq 3
    test_space.state.should eq false
  end

  describe '.all' do
    it 'starts empty' do
      Space.all.should eq []
    end

    it 'is filled with all spaces' do
      test_board = Board.new(10,10)
      test_space1 = Space.create(0,0)
      test_space2 = Space.create(1,0)
      Space.all.should eq [test_space1, test_space2]
    end
  end

  describe '.create' do
    it 'creates a space object and pushes into all_spaces array' do
      test_space = Space.create(0,0)
      Space.all.should eq [test_space]
    end
  end
end
require 'rspec'
require 'space'

describe Space do
  it 'should be an instance of Space' do
    test_space = Space.new(2,3)
    test_space.should be_an_instance_of Space
  end

  it 'should initialize with coordinates and "false" for dead state' do
    test_space = Space.new(2,3)
    test_space.x.should eq 2
    test_space.y.should eq 3
    test_space.state.should eq false
  end

  describe '.all' do
    it 'starts empty' do
      Space.all.should eq []
    end

    it 'is filled with all spaces' do
      test_board = Board.new(10,10)
      test_space1 = Space.create(0,0)
      test_space2 = Space.create(1,0)
      Space.all.should eq [test_space1, test_space2]
    end
  end

  describe '.create' do
    it 'creates a space object and pushes into all_spaces array' do
      test_space = Space.create(0,0)
      Space.all.should eq [test_space]
    end
  end
end
require 'rspec'
require 'board'

describe Board do
  it 'initializes a board instance' do
    test_board = Board.new(10, 10)
    test_board.should be_an_instance_of Board
  end

  it 'initializes with correct number of spaces' do
    test_board = Board.new(10, 10)
    test_board.height.should eq 10
    test_board.width.should eq 10
    test_board.number_spaces.should eq 100
  end




  # describe '.create' do
  #   it 'creates a board object and fills it with space objects' do
  #     test_board = Board.create(10, 10)
  #     test_space = Space.new(2,3,false)
  #     test_board
  #   end
  # end
end
class Board

  def initialize(height, width)
    @height = height
    @width = width
    @number_spaces = height * width
  end

  def height
    @height
  end

  def width
    @width
  end

  def number_spaces
    @number_spaces
  end

end

以上是关于ruby 康威的生命游戏的主要内容,如果未能解决你的问题,请参考以下文章

康威生命游戏_学习记录

为啥康威的生命游戏可以归类为万能机?

康威的生命游戏:为啥模式行为不正确?

康威生命游戏的简单动画与 FuncAnimation

优化康威的“生命游戏”

Python案例:协程实现康威生命游戏,元胞自动机