golr 0.5.1 → 0.5.2
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/bin/golr +2 -1
- data/lib/golr/game.rb +13 -17
- data/lib/golr/version.rb +1 -1
- metadata +2 -2
data/bin/golr
CHANGED
@@ -26,10 +26,11 @@ optparse.parse!
|
|
26
26
|
# load game from file
|
27
27
|
game_file = ARGV[0]
|
28
28
|
begin
|
29
|
+
game_file = "nil" if game_file.nil?
|
29
30
|
game_string = File.read(game_file)
|
30
31
|
game = Golr::GameReader.from_string(game_string)
|
31
32
|
rescue Errno::ENOENT, Errno::EACCES => e
|
32
|
-
puts "Cannot read #{game_file} - Does it exist and is it readable?"
|
33
|
+
puts "Cannot read file \"#{game_file}\" - Does it exist and is it readable?"
|
33
34
|
exit 1
|
34
35
|
end
|
35
36
|
|
data/lib/golr/game.rb
CHANGED
@@ -2,10 +2,9 @@ require 'golr/key'
|
|
2
2
|
require 'golr/rules'
|
3
3
|
|
4
4
|
module Golr
|
5
|
-
|
6
5
|
class Game
|
7
6
|
|
8
|
-
attr_reader :grid, :columns, :rows
|
7
|
+
attr_reader :grid, :columns, :rows
|
9
8
|
|
10
9
|
def initialize(columns, rows, living_cells = [])
|
11
10
|
@columns = columns
|
@@ -24,7 +23,7 @@ module Golr
|
|
24
23
|
end
|
25
24
|
new_grid
|
26
25
|
end
|
27
|
-
|
26
|
+
|
28
27
|
|
29
28
|
def evolve
|
30
29
|
next_grid = init_grid
|
@@ -40,13 +39,11 @@ module Golr
|
|
40
39
|
end
|
41
40
|
|
42
41
|
def living_neighbors(key)
|
43
|
-
living_neighbors = neighboring_keys(key).inject(0) do |result, _key|
|
44
|
-
|
45
|
-
result += 1 if alive?(folded_key)
|
42
|
+
living_neighbors = neighboring_keys(key).inject(0) do |result, _key|
|
43
|
+
result += 1 if alive?(wrap_key_around_board_edges(_key))
|
46
44
|
result
|
47
45
|
end
|
48
46
|
end
|
49
|
-
private :living_neighbors
|
50
47
|
|
51
48
|
def neighboring_keys(key)
|
52
49
|
neighbor_keys = []
|
@@ -58,19 +55,18 @@ module Golr
|
|
58
55
|
end
|
59
56
|
neighbor_keys
|
60
57
|
end
|
61
|
-
private :neighboring_keys
|
62
58
|
|
63
|
-
|
64
|
-
def fold_key_if_required(key)
|
59
|
+
def wrap_key_around_board_edges(key)
|
65
60
|
x,y = Key.coordinates(key)
|
66
|
-
|
67
|
-
_x = _x > @columns ? 1 : _x
|
68
|
-
_y = y < 1 ? @rows : y
|
69
|
-
_y = _y > @rows ? 1 : _y
|
70
|
-
Key.key(_x,_y)
|
61
|
+
Key.key(wrap_coordinate(x, @columns), wrap_coordinate(y, @rows))
|
71
62
|
end
|
72
|
-
private :fold_key_if_required
|
73
63
|
|
74
|
-
|
64
|
+
def wrap_coordinate(value, limit)
|
65
|
+
wrapped = value < 1 ? limit : value
|
66
|
+
wrapped = wrapped > limit ? 1 : wrapped
|
67
|
+
end
|
75
68
|
|
69
|
+
private :init_grid, :living_neighbors, :neighboring_keys, :wrap_key_around_board_edges, :wrap_coordinate
|
70
|
+
|
71
|
+
end
|
76
72
|
end
|
data/lib/golr/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: golr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-04-
|
12
|
+
date: 2013-04-15 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|