browser_conway_game_of_life 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/Gemfile +2 -0
- data/README.md +20 -3
- data/lib/browser_conway_game_of_life.rb +1 -1
- data/lib/browser_conway_game_of_life/cell.rb +37 -0
- data/lib/browser_conway_game_of_life/universe.rb +39 -0
- data/lib/browser_conway_game_of_life/version.rb +1 -1
- data/run.rb +16 -0
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5b20a90aab9b9d292e245f3221b7a23e68ec5490
|
4
|
+
data.tar.gz: 96cc4ed3e351878cd4b876c891bbd2b73bdf864b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4367bb7711a649cd367296441f4adfc629a4e3f1fdb81e93aea32cbf966bfe2ae1cbe6dbfa9bcaf0f61c92d6286050425ff91fd417df800136f804c281a53f42
|
7
|
+
data.tar.gz: ca9d71c638c1b9d4e42b1f459d5c860e5d68b155ee303320ae633d459fec3e40b5fa7217e57bdcb2b1adf99c320ab83fbfb2384c9a41b1598f9b9c980d847a1e
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# BrowserConwayGameOfLife
|
2
2
|
|
3
|
-
|
3
|
+
The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970. The "game" is a zero-player game, meaning that its evolution is determined by its initial state, requiring no further input. One interacts with the Game of Life by creating an initial configuration and observing how it evolves.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -20,11 +20,28 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
-
|
23
|
+
```bash
|
24
|
+
ruby run.rb # or shotgun run.rb # or rake
|
25
|
+
```
|
26
|
+
Type in your favourite browser
|
27
|
+
http://localhost:4567
|
28
|
+
|
29
|
+
### TODO
|
30
|
+
|
31
|
+
- [x] Create gem
|
32
|
+
- [x] Create Git repository
|
33
|
+
- [x] Create Conway's Game of Life with OOP principles
|
34
|
+
- [ ] Create tests (i.e. Rspec)
|
35
|
+
- [ ] Create proper structure application (Gemfile, Rakefile, README, directories lib/, public/, views/, test/)
|
36
|
+
- [ ] Add Sinatra & DataMapper gems for start web application
|
37
|
+
- [ ] Create Erb, Haml and Slim templates for views
|
38
|
+
- [ ] Create CSS files
|
39
|
+
- [ ] Create JavaScript files
|
40
|
+
|
24
41
|
|
25
42
|
## Contributing
|
26
43
|
|
27
|
-
1. Fork it ( https://github.com/
|
44
|
+
1. Fork it ( https://github.com/randsina/browser_conway_game_of_life/fork )
|
28
45
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
29
46
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
30
47
|
4. Push to the branch (`git push origin my-new-feature`)
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module BrowserConwayGameOfLife
|
2
|
+
class Cell
|
3
|
+
NEIGHBOURS = [
|
4
|
+
[-1, -1], [-1, 0], [-1, 1],
|
5
|
+
[0, -1], [0, 1],
|
6
|
+
[1, -1], [1, 0], [1, 1]
|
7
|
+
]
|
8
|
+
|
9
|
+
attr_accessor :value
|
10
|
+
|
11
|
+
def initialize(value = rand(0..1))
|
12
|
+
(0..1).include?(value) ? @value = value : @value = rand(0..1)
|
13
|
+
end
|
14
|
+
|
15
|
+
def live?
|
16
|
+
@value == 1
|
17
|
+
end
|
18
|
+
|
19
|
+
def dead?
|
20
|
+
!live?
|
21
|
+
end
|
22
|
+
|
23
|
+
def change!
|
24
|
+
self.value = self.value.send(:+, -1).abs
|
25
|
+
end
|
26
|
+
|
27
|
+
def to_s
|
28
|
+
live? ? 'O' : ' '
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.live_neighbours(universe, i, j)
|
32
|
+
NEIGHBOURS.inject(0) do |sum, pos|
|
33
|
+
sum + universe[(i + pos[0]) % universe.size][(j + pos[1]) % universe[0].size].value
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module BrowserConwayGameOfLife
|
2
|
+
class Universe
|
3
|
+
attr_reader :rows, :cols
|
4
|
+
|
5
|
+
def initialize(rows, cols)
|
6
|
+
srand 42
|
7
|
+
@field = Array.new(rows) { Array.new(cols) { Cell.new } }
|
8
|
+
@temp_field = @field.clone
|
9
|
+
end
|
10
|
+
|
11
|
+
def next
|
12
|
+
step
|
13
|
+
@temp_field = @field.clone
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_s
|
17
|
+
string = ''
|
18
|
+
@field.each do |vector|
|
19
|
+
vector.each do |el|
|
20
|
+
string << el.to_s
|
21
|
+
end
|
22
|
+
string << "\n"
|
23
|
+
end
|
24
|
+
string
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def step
|
30
|
+
@field.each_with_index do |vector, row|
|
31
|
+
vector.each_with_index do |el, col|
|
32
|
+
live = Cell.live_neighbours(@temp_field, row, col)
|
33
|
+
el.dead? && live == 3 && el.change!
|
34
|
+
el.live? && !(2..3).include?(live) && el.change!
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/run.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'pry'
|
3
|
+
require_relative 'lib/browser_conway_game_of_life'
|
4
|
+
Dir[File.expand_path("lib/browser_conway_game_of_life/*.rb")].each { |f| require f }
|
5
|
+
|
6
|
+
include BrowserConwayGameOfLife
|
7
|
+
|
8
|
+
# binding.pry
|
9
|
+
universe = Universe.new(20, 10)
|
10
|
+
puts universe
|
11
|
+
loop do
|
12
|
+
universe.next
|
13
|
+
p '*' * 40
|
14
|
+
puts universe
|
15
|
+
sleep 0.3
|
16
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: browser_conway_game_of_life
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Roman Vakulchik
|
@@ -52,7 +52,10 @@ files:
|
|
52
52
|
- Rakefile
|
53
53
|
- browser_conway_game_of_life.gemspec
|
54
54
|
- lib/browser_conway_game_of_life.rb
|
55
|
+
- lib/browser_conway_game_of_life/cell.rb
|
56
|
+
- lib/browser_conway_game_of_life/universe.rb
|
55
57
|
- lib/browser_conway_game_of_life/version.rb
|
58
|
+
- run.rb
|
56
59
|
homepage: https://github.com/randsina/browser_conway_game_of_life
|
57
60
|
licenses:
|
58
61
|
- MIT
|