conway 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,15 +1,17 @@
1
1
  ## Conway
2
- A simple Game of Live implementation with formost focus on object design.
2
+ A simple Game of Life implementation with foremost focus on object design.
3
3
 
4
- Currently very much leaky of object references.
4
+ It's currently very much leaky of object references and has no mind for
5
+ efficiency.
5
6
 
6
- Conway comes with a very simple ASCII visualize. It can be invoked like so,
7
- where the size argument determines the dimensions of the square cell grid:
7
+ Conway comes with a very simple ASCII visualizer. It can be invoked as
8
+ below, where the size argument determines the dimensions of the cell grid:
8
9
 
9
10
  conway --size 25 --cells="2,3 3,3 2,2 5,4 5,8 9,8, 7,6"
10
11
  conway -s 30 -c 2,3:3,3:3,2:2,2:3,4
11
12
 
12
- Please feel free to send feedback via the GitHub project or via email at
13
- mby _AT_ mattyoho _dot_ com.
13
+ Please feel free to send feedback via the
14
+ [GitHub project](https://github.com/mattyoho/conway) or via email at
15
+ mby _AT_ mattyoho _DOT_ com.
16
+
14
17
 
15
- https://github.com/mattyoho/conway
data/bin/conway CHANGED
@@ -9,7 +9,7 @@ require 'conway/visualizer/ascii'
9
9
 
10
10
  PROGRAM_VERSION = 1
11
11
 
12
- module ConwayAscii
12
+ module ConwayCli
13
13
  def self.suppress_warnings
14
14
  original_verbosity = $VERBOSE
15
15
  begin
@@ -33,7 +33,7 @@ module ConwayAscii
33
33
  end
34
34
  end
35
35
 
36
- ConwayAscii.suppress_warnings do
36
+ ConwayCli.suppress_warnings do
37
37
  Choice.options do
38
38
  header 'Conway Game of Life ASCII visualizer'
39
39
  header 'Available options:'
@@ -61,14 +61,14 @@ ConwayAscii.suppress_warnings do
61
61
  end
62
62
 
63
63
  Signal.trap("TERM") do
64
- ConwayAscii.exit
64
+ ConwayCli.exit
65
65
  end
66
66
 
67
67
  Signal.trap("INT") do
68
- ConwayAscii.exit
68
+ ConwayCli.exit
69
69
  end
70
70
 
71
- live_points = ConwayAscii.parse_points Choice[:cells]
71
+ live_points = ConwayCli.parse_points Choice[:cells]
72
72
  grid = Conway::Visualizer::Ascii.new(Choice[:size], live_points)
73
73
 
74
74
  grid.loop do |step|
data/lib/conway/point.rb CHANGED
@@ -3,7 +3,7 @@ module Conway
3
3
  attr_reader :x, :y
4
4
 
5
5
  def initialize(x=0,y=0)
6
- @x,@y = x,y
6
+ self.x, self.y = x,y
7
7
  end
8
8
 
9
9
  def eql?(other)
@@ -18,12 +18,20 @@ module Conway
18
18
  :"#{x}-#{y}".object_id
19
19
  end
20
20
 
21
+ def update(x, y)
22
+ self.x = x
23
+ self.y = y
24
+ end
25
+
21
26
  def adjacents
22
- grid = (-1..1).map do |j|
27
+ (-1..1).map do |j|
23
28
  (-1..1).map do |i|
24
29
  Point.new(x+i, y+j) unless i == 0 && j == 0
25
30
  end
26
31
  end.flatten.compact
27
32
  end
33
+
34
+ protected
35
+ attr_writer :x, :y
28
36
  end
29
37
  end
@@ -16,9 +16,9 @@ module Conway
16
16
  puts "They live!!\n\n"
17
17
 
18
18
  begin
19
- new_points = generation.cell_coordinates
19
+ live_cells = generation.cell_coordinates
20
20
 
21
- if new_points.count == 0
21
+ if live_cells.count == 0
22
22
  puts "\nThey have all perished! D-:"
23
23
  break
24
24
  end
@@ -26,7 +26,7 @@ module Conway
26
26
  grid = ""
27
27
  (1..max_y).each do |y|
28
28
  (1..max_x).each do |x|
29
- cell_char = cell_content_for(new_points, x,y)
29
+ cell_char = cell_content_for(live_cells, x,y)
30
30
  grid << "|#{cell_char}"
31
31
  end
32
32
  grid << "|\n"
@@ -34,8 +34,8 @@ module Conway
34
34
 
35
35
  grid << "\n"
36
36
 
37
- grid << "Total objects: #{ObjectSpace.count_objects[:TOTAL]} "
38
- grid << "Total LiveCells: #{new_points.count}\n"
37
+ grid << "Total objects: #{live_object_count} "
38
+ grid << "Total living cells: #{live_cells.count}\n"
39
39
 
40
40
  elapsed_minutes, elapsed_seconds = ((Time.now - start).to_i).divmod 60
41
41
  grid << "Elapsed time: #{elapsed_minutes} min, #{elapsed_seconds} secs\n"
@@ -50,7 +50,19 @@ module Conway
50
50
  attr_accessor :max_x, :max_y, :starting_cells, :loop_interval
51
51
 
52
52
  def cell_content_for(points, x,y)
53
- points.detect {|p| p.x == x && p.y == y } ? "X" : " "
53
+ @comparison_point ||= Point.new(x,y)
54
+ @comparison_point.update(x,y)
55
+ points.detect {|p| p == @comparison_point } ? "X" : " "
56
+ end
57
+
58
+ def live_object_count
59
+ if ObjectSpace.respond_to?(:count_objects)
60
+ # Ruby 1.9.2
61
+ ObjectSpace.count_objects[:TOTAL]
62
+ else
63
+ # Ruby 1.8.7
64
+ ObjectSpace.live_objects
65
+ end
54
66
  end
55
67
  end
56
68
  end
@@ -34,5 +34,14 @@ describe Point do
34
34
  point.adjacents.should == neighbors
35
35
  end
36
36
  end
37
+
38
+ describe "#update" do
39
+ let(:point) { Point.new(2,2) }
40
+ it "updates the x and y points" do
41
+ point.update(1,3)
42
+ point.x.should eql(1)
43
+ point.y.should eql(3)
44
+ end
45
+ end
37
46
  end
38
47
 
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: conway
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 25
4
5
  prerelease: false
5
6
  segments:
6
7
  - 0
7
8
  - 1
8
- - 0
9
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
10
11
  platform: ruby
11
12
  authors:
12
13
  - Matt Yoho
@@ -14,7 +15,7 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2011-01-19 00:00:00 -05:00
18
+ date: 2011-01-20 00:00:00 -05:00
18
19
  default_executable: conway
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
@@ -25,6 +26,7 @@ dependencies:
25
26
  requirements:
26
27
  - - "="
27
28
  - !ruby/object:Gem::Version
29
+ hash: 19
28
30
  segments:
29
31
  - 0
30
32
  - 1
@@ -78,6 +80,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
78
80
  requirements:
79
81
  - - ">="
80
82
  - !ruby/object:Gem::Version
83
+ hash: 3
81
84
  segments:
82
85
  - 0
83
86
  version: "0"
@@ -86,6 +89,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
86
89
  requirements:
87
90
  - - ">="
88
91
  - !ruby/object:Gem::Version
92
+ hash: 3
89
93
  segments:
90
94
  - 0
91
95
  version: "0"