simple_life_game 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/cell.rb +16 -0
- data/lib/simple_life_game.rb +38 -0
- metadata +79 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d6ab44e55a890c93c8548b1a14ffb19328a2b521
|
4
|
+
data.tar.gz: e370a4df2dd5b1c903977a884b0786ea7d637f5d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e03c9c2ebcfb77e8fa86415346da748f2cfcb65030d33abe8dc02ffa62d77c2409bd0a573ee1aacca218146d02b9b7a99cb26983352da989101671be7d82aa48
|
7
|
+
data.tar.gz: fbaaeead616d91d5d44b193ca5b2c01f4e79d66f56540c973e37479b141b137a3887ab38b05cadcb23ca45ea88c94d900edc0a0a1a4878c3eaab41a037256743
|
data/lib/cell.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
class Cell
|
2
|
+
attr_accessor :neighbors
|
3
|
+
attr_reader :alive
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@alive = (rand(0..1) == 1) ? true : false
|
7
|
+
end
|
8
|
+
|
9
|
+
def next!
|
10
|
+
@alive = @alive ? (2..3) === @neighbors : 3 == @neighbors
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_i
|
14
|
+
@alive ? 1 : 0
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require "colorize"
|
2
|
+
require_relative "cell"
|
3
|
+
|
4
|
+
class LifeGame
|
5
|
+
def initialize(width = 60, heigth = 40)
|
6
|
+
@width = width
|
7
|
+
@heigth = heigth
|
8
|
+
@card = Array.new(heigth) { Array.new(width) { Cell.new } }
|
9
|
+
end
|
10
|
+
|
11
|
+
def next_gen!
|
12
|
+
@card.each_with_index do |line, y|
|
13
|
+
line.each_with_index do |cell, x|
|
14
|
+
cell.neighbors = alive_neighbors(y, x)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
@card.each { |line| line.each { |cell| cell.next! } }
|
18
|
+
end
|
19
|
+
|
20
|
+
def print_card
|
21
|
+
@card.each_with_index { |line, num| line.map { |cell| print cell.alive ? "■".light_blue : ' ' }; puts "\n" }
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
def alive_neighbors(y, x)
|
26
|
+
up = (y == 0) ? @heigth - 1 : y - 1
|
27
|
+
down = (y == @heigth - 1) ? 0 : y + 1
|
28
|
+
left = (x == 0) ? @width - 1 : x - 1
|
29
|
+
right = (x == @width - 1) ? 0 : x + 1
|
30
|
+
|
31
|
+
[[y, left], [y, right], # sides
|
32
|
+
[up, right], [up, x], [up, left], # over
|
33
|
+
[down, left], [down, x], [down, right] # under
|
34
|
+
].inject(0) do |sum, pos|
|
35
|
+
sum += @card[pos[0]][pos[1]].to_i
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_life_game
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Aleks Senkou
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-11-16 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.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: colorize
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.7'
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: 0.7.3
|
37
|
+
type: :development
|
38
|
+
prerelease: false
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - "~>"
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0.7'
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 0.7.3
|
47
|
+
description: Simple realization of Life game
|
48
|
+
email: aleksey.senkou@gmail.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- lib/cell.rb
|
54
|
+
- lib/simple_life_game.rb
|
55
|
+
homepage: http://rubygems.org/gems/simple_life_game
|
56
|
+
licenses:
|
57
|
+
- MIT
|
58
|
+
metadata: {}
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options: []
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: 2.1.3
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: 1.3.4
|
73
|
+
requirements: []
|
74
|
+
rubyforge_project:
|
75
|
+
rubygems_version: 2.2.2
|
76
|
+
signing_key:
|
77
|
+
specification_version: 4
|
78
|
+
summary: Conway's Game of Life
|
79
|
+
test_files: []
|