cellular_automata 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/README.md +38 -0
- data/Rakefile +1 -0
- data/bin/cell +5 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/cellular_automata.gemspec +25 -0
- data/lib/cellular_automata/animator.rb +16 -0
- data/lib/cellular_automata/board.rb +76 -0
- data/lib/cellular_automata/cell.rb +92 -0
- data/lib/cellular_automata/version.rb +3 -0
- data/lib/cellular_automata.rb +7 -0
- metadata +96 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 39fe07d2e8c8d0a1108c4cdd4bafe42016fb1814
|
4
|
+
data.tar.gz: 775052c3229724fdc2aff5ec27c59c45d831c156
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bd59f7522b9cc0c6695dc6f04f3f67ad227e0e4c851a7c0c2a42d113249b7d7892c0d116d1bc6f9882ca6d72fb24c9bf16d6b54d86943ad943225d60aa5078b0
|
7
|
+
data.tar.gz: b9aed23d3eb5e69ad623000dfc7e04ce5722deb2ecc9c43b43a2af832f6e8a850833aafccb971f8a71b7994d01d5c524f42a998fb73c28ea9abe68b5adef4d81
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# CellularAutomata
|
2
|
+
|
3
|
+
This is yet another cellular automata implementation. First Conway's, and then
|
4
|
+
we'll see where it goes.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'cellular_automata'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install cellular_automata
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
At present, it can only play Conway's Game of Life. Run `cell` to watch.
|
25
|
+
|
26
|
+
## Development
|
27
|
+
|
28
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
29
|
+
|
30
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
31
|
+
|
32
|
+
## Contributing
|
33
|
+
|
34
|
+
1. Fork it ( https://github.com/ffleming/cellular_automata/fork )
|
35
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
36
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
37
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
38
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/cell
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "cellular_automata"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'cellular_automata/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "cellular_automata"
|
8
|
+
spec.version = CellularAutomata::VERSION
|
9
|
+
spec.authors = ["Forrest Fleming"]
|
10
|
+
spec.email = ["ffleming@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A simulation of cellular automata}
|
13
|
+
spec.description = %q{A superset of 0-player games, of which Conway's Game of Life is a member.}
|
14
|
+
spec.homepage = "https://github.com/ffleming/cellular_automata"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "bin"
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.license = 'MIT'
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1", ">= 1.8"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module CellularAutomata::Animator
|
2
|
+
class << self
|
3
|
+
attr_accessor :board
|
4
|
+
def board
|
5
|
+
@board ||= CellularAutomata::Board.new(width: 120, height: 30)
|
6
|
+
end
|
7
|
+
def animate(steps=1000, refresh=0.1)
|
8
|
+
(1..steps).each do
|
9
|
+
puts "\e[H\e[2J"
|
10
|
+
board.cycle!
|
11
|
+
puts board.to_s
|
12
|
+
sleep refresh
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
class CellularAutomata::Board
|
2
|
+
attr_reader :width, :height
|
3
|
+
def initialize(width: 80, height: 20)
|
4
|
+
@height = height
|
5
|
+
@width = width
|
6
|
+
@array = build_array
|
7
|
+
CellularAutomata::Cell::array = @array
|
8
|
+
seed!
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_s
|
12
|
+
line = '+' << ('-' * width) << "+\n"
|
13
|
+
ret = '' << line
|
14
|
+
@array.each do |row|
|
15
|
+
ret << "|"
|
16
|
+
row.each do |cell|
|
17
|
+
ret << cell.to_s
|
18
|
+
end
|
19
|
+
ret << "|\n"
|
20
|
+
end
|
21
|
+
ret << line
|
22
|
+
end
|
23
|
+
|
24
|
+
def cycle!
|
25
|
+
each_cell do |cell|
|
26
|
+
case cell.live_neighbors.length
|
27
|
+
when 0..1
|
28
|
+
cell.die!
|
29
|
+
when 3
|
30
|
+
cell.live!
|
31
|
+
when 4..8
|
32
|
+
cell.die!
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def animate(steps=1000, refresh=0.1)
|
38
|
+
(1..steps).each do |i|
|
39
|
+
puts "\e[H\e[2J"
|
40
|
+
cycle!
|
41
|
+
puts self.to_s
|
42
|
+
sleep refresh
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def each_cell
|
49
|
+
(0..height-1).each do |row|
|
50
|
+
(0..width-1).each do |col|
|
51
|
+
yield @array[row][col]
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
def seed!
|
58
|
+
(0..height-1).each do |row|
|
59
|
+
(0..width-1).each do |col|
|
60
|
+
@array[row][col].live! if rand < 0.2
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def build_array
|
66
|
+
arr = []
|
67
|
+
(0..height-1).each do |i|
|
68
|
+
arr[i] = []
|
69
|
+
(0..width-1).each do |j|
|
70
|
+
arr[i][j] = CellularAutomata::Cell.new(row: i, column: j)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
return arr
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
@@ -0,0 +1,92 @@
|
|
1
|
+
class CellularAutomata::Cell
|
2
|
+
class << self
|
3
|
+
attr_accessor :array
|
4
|
+
end
|
5
|
+
|
6
|
+
attr_accessor :row, :column
|
7
|
+
attr_reader :alive
|
8
|
+
alias x column
|
9
|
+
alias x= column=
|
10
|
+
alias y row
|
11
|
+
alias y= row=
|
12
|
+
alias alive? alive
|
13
|
+
def initialize(row: , column: )
|
14
|
+
@row = row
|
15
|
+
@column = column
|
16
|
+
@alive = false
|
17
|
+
end
|
18
|
+
|
19
|
+
def die!
|
20
|
+
@alive = false
|
21
|
+
end
|
22
|
+
|
23
|
+
def live!
|
24
|
+
@alive = true
|
25
|
+
end
|
26
|
+
|
27
|
+
def dead?
|
28
|
+
!alive?
|
29
|
+
end
|
30
|
+
|
31
|
+
def to_s
|
32
|
+
return ' ' if dead?
|
33
|
+
'*'
|
34
|
+
end
|
35
|
+
|
36
|
+
def live_neighbors
|
37
|
+
neighbors.select {|n| n.alive? }
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def neighbors
|
43
|
+
[
|
44
|
+
n_neighbor,
|
45
|
+
ne_neighbor,
|
46
|
+
e_neighbor,
|
47
|
+
se_neighbor,
|
48
|
+
s_neighbor,
|
49
|
+
sw_neighbor,
|
50
|
+
w_neighbor,
|
51
|
+
nw_neighbor
|
52
|
+
].compact
|
53
|
+
end
|
54
|
+
|
55
|
+
def n_neighbor
|
56
|
+
neighbor(y-1, x)
|
57
|
+
end
|
58
|
+
|
59
|
+
def ne_neighbor
|
60
|
+
neighbor(y-1, x+1)
|
61
|
+
end
|
62
|
+
|
63
|
+
def e_neighbor
|
64
|
+
neighbor(y, x+1)
|
65
|
+
end
|
66
|
+
|
67
|
+
def se_neighbor
|
68
|
+
neighbor(y+1, x+1)
|
69
|
+
end
|
70
|
+
|
71
|
+
def s_neighbor
|
72
|
+
neighbor(y+1, x)
|
73
|
+
end
|
74
|
+
|
75
|
+
def sw_neighbor
|
76
|
+
neighbor(y+1, x-1)
|
77
|
+
end
|
78
|
+
|
79
|
+
def w_neighbor
|
80
|
+
neighbor(y, x+1)
|
81
|
+
end
|
82
|
+
|
83
|
+
def nw_neighbor
|
84
|
+
neighbor(y-1, x-1)
|
85
|
+
end
|
86
|
+
|
87
|
+
def neighbor(r, c)
|
88
|
+
raise StandardError.new("Cell::array not initialized") if CellularAutomata::Cell::array.nil?
|
89
|
+
return nil if r < 0 || c < 0
|
90
|
+
return CellularAutomata::Cell::array[r][c] rescue nil
|
91
|
+
end
|
92
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cellular_automata
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Forrest Fleming
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '1.8'
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '1.8'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: rake
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '10.0'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '10.0'
|
47
|
+
description: A superset of 0-player games, of which Conway's Game of Life is a member.
|
48
|
+
email:
|
49
|
+
- ffleming@gmail.com
|
50
|
+
executables:
|
51
|
+
- cell
|
52
|
+
- console
|
53
|
+
- setup
|
54
|
+
extensions: []
|
55
|
+
extra_rdoc_files: []
|
56
|
+
files:
|
57
|
+
- ".gitignore"
|
58
|
+
- ".rspec"
|
59
|
+
- ".travis.yml"
|
60
|
+
- Gemfile
|
61
|
+
- README.md
|
62
|
+
- Rakefile
|
63
|
+
- bin/cell
|
64
|
+
- bin/console
|
65
|
+
- bin/setup
|
66
|
+
- cellular_automata.gemspec
|
67
|
+
- lib/cellular_automata.rb
|
68
|
+
- lib/cellular_automata/animator.rb
|
69
|
+
- lib/cellular_automata/board.rb
|
70
|
+
- lib/cellular_automata/cell.rb
|
71
|
+
- lib/cellular_automata/version.rb
|
72
|
+
homepage: https://github.com/ffleming/cellular_automata
|
73
|
+
licenses:
|
74
|
+
- MIT
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.2.2
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: A simulation of cellular automata
|
96
|
+
test_files: []
|