rubylife 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 +9 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +27 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/rubylife.rb +59 -0
- data/lib/rubylife/board.rb +35 -0
- data/lib/rubylife/cell.rb +20 -0
- data/lib/rubylife/version.rb +3 -0
- data/rubylife.gemspec +28 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ab80f1fd5d69925d66658f99520ab6ca72cd5f03
|
4
|
+
data.tar.gz: 37303afcd9acf6c601534e87b1638c650c5db32a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9a52d7ea7846b9270fb97b8c9d4ff62de790b16fe0e6d6595e0d375987b2a02926ba218700eda062496c4abd908847234273af9fb1881912024094e64c0eb995
|
7
|
+
data.tar.gz: 64c2f72d178b8a62a7b26147d319bd66a026bc67d3df01021dc612e02e07962cbe32615053cd7151ae9630a129cc72e4ff467122ce42b2a93a363a2b9aeab138
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 scaryguy
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# Rubylife
|
2
|
+
|
3
|
+
An implementation of Conway's Game of Life in Ruby
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'rubylife'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install rubylife
|
20
|
+
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
game = Rubylife::Game.new(Rubylife::Board.new, [[2,0],[2,1],[2,2],[1,2],[0,1]])
|
26
|
+
game.tick!
|
27
|
+
```
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "rubylife"
|
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
data/lib/rubylife.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
require "rubylife/version"
|
2
|
+
require 'rubylife/board'
|
3
|
+
require 'rubylife/cell'
|
4
|
+
|
5
|
+
|
6
|
+
module Rubylife
|
7
|
+
class Game
|
8
|
+
attr_reader :board
|
9
|
+
|
10
|
+
def initialize(board=Board.new, seed=[])
|
11
|
+
|
12
|
+
@board = board
|
13
|
+
seed.each do |cell|
|
14
|
+
board.grid[cell[0]][cell[1]].alive=true
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
def tick!
|
20
|
+
|
21
|
+
live_cells = []
|
22
|
+
dead_cells = []
|
23
|
+
|
24
|
+
|
25
|
+
@board.cells.each do |cell|
|
26
|
+
|
27
|
+
# Rule #1:
|
28
|
+
# Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.
|
29
|
+
if (cell.alive? && @board.live_neighbours(cell).size < 2)
|
30
|
+
dead_cells << cell
|
31
|
+
end
|
32
|
+
|
33
|
+
# Rule #2:
|
34
|
+
# Any live cell with two or three live neighbours lives on to the next generation.
|
35
|
+
if (cell.alive? && (@board.live_neighbours(cell).size == 2 || @board.live_neighbours(cell) == 3))
|
36
|
+
live_cells << cell
|
37
|
+
end
|
38
|
+
|
39
|
+
# Rule #3:
|
40
|
+
# Any live cell with more than three live neighbours dies, as if by overcrowding..
|
41
|
+
if (cell.alive? && @board.live_neighbours(cell).size > 3)
|
42
|
+
dead_cells << cell
|
43
|
+
end
|
44
|
+
|
45
|
+
# Rule #4:
|
46
|
+
# Any dead cell with exactly three live neighbours becomes a live cell,as if by reproduction.
|
47
|
+
if (cell.dead? && @board.live_neighbours(cell).size == 3)
|
48
|
+
live_cells << cell
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
live_cells.each { |cell| cell.alive = true}
|
54
|
+
dead_cells.each { |cell| cell.alive = false}
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Rubylife
|
2
|
+
class Board
|
3
|
+
attr_accessor :rows, :cols, :grid, :cells
|
4
|
+
|
5
|
+
def initialize(rows=3, cols=3)
|
6
|
+
@rows = rows
|
7
|
+
@cols = cols
|
8
|
+
@cells = []
|
9
|
+
@grid = Array.new(rows) do |row|
|
10
|
+
Array.new(cols) do |col|
|
11
|
+
cell = Cell.new(row, col)
|
12
|
+
@cells.push(cell)
|
13
|
+
cell
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def live_neighbours(cell)
|
19
|
+
neighbours = []
|
20
|
+
|
21
|
+
@grid.each_with_index do |row, i|
|
22
|
+
row.each_with_index do |col,j|
|
23
|
+
if (grid[i][j] != cell)
|
24
|
+
if(grid[i][j].alive? && (((cell.x - i).abs < 2) && ((cell.y - j).abs < 2)))
|
25
|
+
neighbours.push(grid[i][j])
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
neighbours
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
data/rubylife.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rubylife/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "rubylife"
|
8
|
+
spec.version = Rubylife::VERSION
|
9
|
+
spec.authors = ["scaryguy"]
|
10
|
+
spec.email = ["soner2@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A Conway's Game of Life implementation written in Ruby.}
|
13
|
+
spec.description = %q{A Conway's Game of Life implementation written in Ruby.}
|
14
|
+
spec.homepage = "http://github.com/scaryguy/rubylife"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
19
|
+
f.match(%r{^(test|spec|features)/})
|
20
|
+
end
|
21
|
+
spec.bindir = "exe"
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rubylife
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- scaryguy
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-11-18 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.13'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: A Conway's Game of Life implementation written in Ruby.
|
56
|
+
email:
|
57
|
+
- soner2@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- lib/rubylife.rb
|
72
|
+
- lib/rubylife/board.rb
|
73
|
+
- lib/rubylife/cell.rb
|
74
|
+
- lib/rubylife/version.rb
|
75
|
+
- rubylife.gemspec
|
76
|
+
homepage: http://github.com/scaryguy/rubylife
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 2.5.1
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: A Conway's Game of Life implementation written in Ruby.
|
100
|
+
test_files: []
|