tk_game_of_life 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/lib/cell.rb +59 -0
  3. data/lib/game_of_life.rb +26 -0
  4. metadata +45 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 02bd98ad686a35f163177465375e4ad9c191342b84d25434020cef39e3e6ba6f
4
+ data.tar.gz: 94545d80acdaaabff692044a81a80cc7f4c9495f42d7ebf948b76dd52a588c60
5
+ SHA512:
6
+ metadata.gz: 310237dcb28441bda29931030fa3d14f8b025e58ec0ff803618cc3fd7437ff91eb4ce5197c964c9a3a3acac2baa6ff8e44de8bc445b05d6ec367381ce973f6e0
7
+ data.tar.gz: 85b91839aca6284078f11a2a460185ba19bc847ecdf13a91330e632ed6cd3f484a419d8594596639ebb7f86d347f7a30a059725a69139fa98c3d3199ed16249a
data/lib/cell.rb ADDED
@@ -0,0 +1,59 @@
1
+ OFFSET = 1
2
+
3
+ class Cell
4
+ def self.alive
5
+ 1
6
+ end
7
+
8
+ def self.dead
9
+ 0
10
+ end
11
+
12
+ def self.neighbor_count(universe, row, spot)
13
+ [ n_neighbor?(universe, row, spot), s_neighbor?(universe, row, spot),
14
+ e_neighbor?(universe, row, spot), w_neighbor?(universe, row, spot),
15
+ ne_neighbor?(universe, row, spot), nw_neighbor?(universe, row, spot),
16
+ se_neighbor?(universe, row, spot), sw_neighbor?(universe, row, spot)
17
+ ].count(true)
18
+ end
19
+
20
+ def self.n_neighbor?(universe, row, spot)
21
+ return false if row - OFFSET < 0
22
+ return universe[row - OFFSET][spot] == alive
23
+ end
24
+
25
+ def self.s_neighbor?(universe, row, spot)
26
+ return false if row + OFFSET == universe.size
27
+ return universe[row + OFFSET][spot] == alive
28
+ end
29
+
30
+ def self.e_neighbor?(universe, row, spot)
31
+ return false if spot + OFFSET == universe[row].size
32
+ return universe[row][spot + OFFSET] == alive
33
+ end
34
+
35
+ def self.w_neighbor?(universe, row, spot)
36
+ return false if spot - OFFSET < 0
37
+ return universe[row][spot - OFFSET] == alive
38
+ end
39
+
40
+ def self.nw_neighbor?(universe, row, spot)
41
+ return false if (row - OFFSET < 0) || (spot - OFFSET < 0)
42
+ return universe[row - OFFSET][spot - OFFSET] == alive
43
+ end
44
+
45
+ def self.ne_neighbor?(universe, row, spot)
46
+ return false if (row - OFFSET < 0) || (spot + OFFSET == universe[row].size)
47
+ return universe[row - OFFSET][spot + OFFSET] == alive
48
+ end
49
+
50
+ def self.se_neighbor?(universe, row, spot)
51
+ return false if (row + OFFSET == universe.size) || (spot + OFFSET == universe[row].size)
52
+ return universe[row + OFFSET][spot + OFFSET] == alive
53
+ end
54
+
55
+ def self.sw_neighbor?(universe, row, spot)
56
+ return false if (row + OFFSET == universe.size) || (spot - OFFSET < 0)
57
+ return universe[row + OFFSET][spot - OFFSET] == alive
58
+ end
59
+ end
@@ -0,0 +1,26 @@
1
+ require_relative '../lib/cell.rb'
2
+
3
+ class GameOfLife
4
+ def self.tick(universe)
5
+ new_universe = []
6
+
7
+ width = universe.size - 1
8
+ height = universe[0].size - 1
9
+ (0..width).each do |row|
10
+ new_row = []
11
+ (0..height).each do |spot|
12
+ neighbor_count = Cell.neighbor_count(universe, row, spot)
13
+ if neighbor_count == 3
14
+ new_row << Cell.alive
15
+ elsif neighbor_count == 2 and (universe[row][spot] == Cell.alive)
16
+ new_row << Cell.alive
17
+ else
18
+ new_row << Cell.dead
19
+ end
20
+ end
21
+ new_universe << new_row
22
+ end
23
+
24
+ return new_universe
25
+ end
26
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tk_game_of_life
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Taylor Keazirian
8
+ - Ian Gagorik
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2022-07-05 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A simple Game of Life gem
15
+ email: tkeazirian@8thlight.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/cell.rb
21
+ - lib/game_of_life.rb
22
+ homepage: https://github.com/t-keazirian/game_of_life
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubygems_version: 3.3.7
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Game of Life
45
+ test_files: []