golife 0.0.0

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/golife.rb +101 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7df70eb0da0b5af657e57c1f118072d4d4c07580
4
+ data.tar.gz: c935d04f79d6bfe08d18050b5e53c28866a7baf4
5
+ SHA512:
6
+ metadata.gz: 4a75863c45ad9a532a17f655edb21c283fd237dbe0d2287b72c75390e90e9427ab2ae35b67d9ebbd6e531b64e499e333792a82e25bbd09f081496c22d8259301
7
+ data.tar.gz: 1f78f7d25d19e82a98d49e1e120d5d6808549b77f144824eba12f22358d15f8520095cee93e0aa3a5df730b3cf9ee1b3a5958ea15f3989b586f6a87cbc7c271c
data/lib/golife.rb ADDED
@@ -0,0 +1,101 @@
1
+ require 'terminfo'
2
+ require 'curses'
3
+
4
+ class GOLife
5
+ HEIGHT, WIDTH = TermInfo.screen_size
6
+ SELF_FILLED = 10
7
+ SLEEP_TIME = 0.01
8
+ MAX_COUNTER = 150
9
+
10
+ attr_reader :field, :temp
11
+
12
+ def initialize(width=(WIDTH-2), height=(HEIGHT-2))
13
+ @width = width
14
+ @height = height
15
+ p @width, @height
16
+ @field = Array.new(@height) { Array.new(@width, 0) }
17
+ @temp = Array.new(@height) {Array.new(@width, 0) }
18
+ @counter = 0
19
+ end
20
+
21
+ def generate
22
+ @height
23
+ 100.times do
24
+ @temp[@height / 3 + rand(@height / 4)][@width / 3 + rand(@width / 4)] = SELF_FILLED
25
+ end
26
+ end
27
+
28
+ def cell_changed?(i, j)
29
+ @field[i][j] != @temp[i][j]
30
+ end
31
+
32
+ def changed?
33
+ HEIGHT.times do |i|
34
+ WIDTH.times do |j|
35
+ return true if cell_changed?(i, j)
36
+ end
37
+ end
38
+ false
39
+ end
40
+
41
+ def finished?
42
+ @counter > MAX_COUNTER
43
+ end
44
+
45
+ def update_cell(i, j)
46
+ ans = 0
47
+ (-1..1).each do |dx|
48
+ (-1..1).each do |dy|
49
+ ans += \
50
+ @field[(i + dx + @height) % @height][(j + dy + @width) % @width] / 10
51
+ end
52
+ end
53
+ ans
54
+ end
55
+
56
+ def recalc
57
+ @counter += 1
58
+ (@height).times do |i|
59
+ (@width).times do |j|
60
+ @temp[i][j] = update_cell(i, j)
61
+ if @temp[i][j] == 3 || @temp[i][j] == 2
62
+ if @field[i][j] / 10 != 0
63
+ @temp[i][j] += 10
64
+ elsif @temp[i][j] == 3
65
+ @temp[i][j] += 10
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
71
+
72
+ def paint
73
+ print '#' * (@width + 2), "\n"
74
+ @height.times do |i|
75
+ print '#'
76
+ @width.times do |j|
77
+ @field[i][j] = @temp[i][j]
78
+ if @field[i][j] / 10 != 0
79
+ print '*'
80
+ else
81
+ print ' '
82
+ end
83
+ end
84
+ print "#\n"
85
+ end
86
+ print '#' * (@width + 2)
87
+ end
88
+
89
+ def run
90
+ #Curses.init_screen
91
+ generate
92
+ until finished?
93
+ puts "\e[H\e[2J\n"
94
+ #Curses.refresh
95
+ paint
96
+ recalc
97
+ sleep(SLEEP_TIME)
98
+ end
99
+ #Curses.close_screen
100
+ end
101
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: golife
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Vlad Shablinsky
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-24 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Game of life gem
14
+ email: vladshablinsky@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/golife.rb
20
+ homepage: http://rubygems.org/gems/golife
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.5
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: GOLife
44
+ test_files: []