game_life 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/game_life.rb +40 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6d03a2ced5d851db39960dc51530305430435026
|
4
|
+
data.tar.gz: 62527b02cb0a6654bedaecfa6630ac0c1f9cd323
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b9c82476bbf2be651dbb7fbb151fc8b83cb03e74baa03fab940689616047c87ea9f74b2b824173bfca539986363b3a08f3b1d4a77af7c047445f859b9155aec1
|
7
|
+
data.tar.gz: 3ad4850a2f69947b1c41164c2b02a172c80a7247eff7d9876975c393e5f5eb9159ba6080a3963ddaf5673ec9f7d4e14ddd0c7b7db6f852f8c293f0cbe94bd192
|
data/lib/game_life.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
class GameOfLife
|
2
|
+
|
3
|
+
attr_accessor :field
|
4
|
+
|
5
|
+
def initialize (m, n)
|
6
|
+
@m, @n = m, n
|
7
|
+
@field = Array.new(m) { Array.new(n) { rand(2) } }
|
8
|
+
end
|
9
|
+
|
10
|
+
def neighbors (x, y)
|
11
|
+
s=0
|
12
|
+
(-1..1).to_a.each do |i|
|
13
|
+
(-1..1).to_a.each { |j| s += @field[(x+i) % @m][(y+j) % @n] }
|
14
|
+
end
|
15
|
+
s-@field[x][y]
|
16
|
+
end
|
17
|
+
|
18
|
+
def rules (x, y)
|
19
|
+
if (neighbors(x ,y) == 3)
|
20
|
+
return 1
|
21
|
+
elsif (@field[x][y] == 1 && neighbors(x, y) == 2)
|
22
|
+
return 1
|
23
|
+
else
|
24
|
+
return 0
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def step
|
29
|
+
field_copy = Array.new(@m) { Array.new(@n) { 0 } }
|
30
|
+
@field.each_with_index do |row, i|
|
31
|
+
row.each_with_index do |elem, j|
|
32
|
+
field_copy[i][j] = rules(i, j)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
@field = field_copy
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: game_life
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Konstatntin Kanyuka
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-07-12 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Implements logic of Conway's Game of Life
|
14
|
+
email: kkan.web@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/game_life.rb
|
20
|
+
homepage: http://github.com/kkan
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubyforge_project:
|
40
|
+
rubygems_version: 2.4.6
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: Conway's Game of Life
|
44
|
+
test_files: []
|