screen_of_life 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.
- data/lib/screen_of_life.rb +96 -0
- metadata +45 -0
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
class Game_of_life
|
|
4
|
+
|
|
5
|
+
def initialize(n,m)
|
|
6
|
+
@n = n
|
|
7
|
+
@m = m
|
|
8
|
+
@empty = "."
|
|
9
|
+
@live = "X"
|
|
10
|
+
@gen = 0
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def origin_of_life
|
|
14
|
+
@amend = Array.new(@n){Array.new(@m,@empty)}
|
|
15
|
+
@board = Array.new(@n){Array.new(@m){rand(2) == 0 ? @live : @empty}}
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def limit?(index_x,index_y)
|
|
19
|
+
return false if index_x<0
|
|
20
|
+
return false if index_y<0
|
|
21
|
+
return false if index_x>=@n
|
|
22
|
+
return false if index_y>=@m
|
|
23
|
+
return true
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def neighborhood(row,col)
|
|
27
|
+
count = 0
|
|
28
|
+
(-1..1).each do |r|
|
|
29
|
+
(-1..1).each do |c|
|
|
30
|
+
if limit?(row+r,col+c)
|
|
31
|
+
count+=1 if @board[row+r][col+c] == @live
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
count-=1 if @board[row][col] == @live
|
|
36
|
+
return count
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def rules
|
|
40
|
+
(0...@n).each do |i|
|
|
41
|
+
(0...@m).each do |j|
|
|
42
|
+
num = neighborhood(i,j)
|
|
43
|
+
if num == 3 && @board[i][j] == @empty
|
|
44
|
+
@amend[i][j] = @live
|
|
45
|
+
elsif (num < 2 || num > 3) && @board[i][j] == @live
|
|
46
|
+
@amend[i][j] = @empty
|
|
47
|
+
else
|
|
48
|
+
@amend[i][j] = @board[i][j]
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
return @amend
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def the_end?
|
|
56
|
+
if @board == @amend
|
|
57
|
+
return true
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def step
|
|
62
|
+
(0...@n).each do|i|
|
|
63
|
+
(0...@m).each do|j|
|
|
64
|
+
@board[i][j] = @amend[i][j]
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
return @board
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def show
|
|
71
|
+
(0...@n).each do |i|
|
|
72
|
+
(0...@m).each do |j|
|
|
73
|
+
print @board[i][j]
|
|
74
|
+
end
|
|
75
|
+
puts
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def tick
|
|
80
|
+
@gen+=1
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def play
|
|
84
|
+
origin_of_life
|
|
85
|
+
loop do
|
|
86
|
+
puts "\e[H\e[2J"
|
|
87
|
+
rules
|
|
88
|
+
break if the_end?
|
|
89
|
+
step
|
|
90
|
+
show
|
|
91
|
+
tick
|
|
92
|
+
sleep 0.7
|
|
93
|
+
end
|
|
94
|
+
puts "There was #{@gen-=1} generations"
|
|
95
|
+
end
|
|
96
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: screen_of_life
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- Ana Booster
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2014-11-19 00:00:00.000000000 Z
|
|
13
|
+
dependencies: []
|
|
14
|
+
description: Game of life
|
|
15
|
+
email: artsiukhevicheugene@gmail.com
|
|
16
|
+
executables: []
|
|
17
|
+
extensions: []
|
|
18
|
+
extra_rdoc_files: []
|
|
19
|
+
files:
|
|
20
|
+
- lib/screen_of_life.rb
|
|
21
|
+
homepage: http://github.com/EugeneArt/Game_of_Life
|
|
22
|
+
licenses: []
|
|
23
|
+
post_install_message:
|
|
24
|
+
rdoc_options: []
|
|
25
|
+
require_paths:
|
|
26
|
+
- lib
|
|
27
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
28
|
+
none: false
|
|
29
|
+
requirements:
|
|
30
|
+
- - ! '>='
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '0'
|
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
34
|
+
none: false
|
|
35
|
+
requirements:
|
|
36
|
+
- - ! '>='
|
|
37
|
+
- !ruby/object:Gem::Version
|
|
38
|
+
version: '0'
|
|
39
|
+
requirements: []
|
|
40
|
+
rubyforge_project:
|
|
41
|
+
rubygems_version: 1.8.23
|
|
42
|
+
signing_key:
|
|
43
|
+
specification_version: 3
|
|
44
|
+
summary: Game of life
|
|
45
|
+
test_files: []
|