ruby_game_of_life 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6c30dfa41acd01c221f77b2c00435062d1561aec
4
+ data.tar.gz: ff34e904fac6176347408cfbc927db4c1bf52a52
5
+ SHA512:
6
+ metadata.gz: 1a6eb2f21b1c9a5894a425ff209534174857a0f8847c8645cc326f3221ad708914d6d80eef8803c79c4656f0be439ef23c8cd3232b1ce004e10ab91cad40d00b
7
+ data.tar.gz: e6a8ecb9479ad19f5fcd50ef20d19f40c73421be3efe4d9f29a770679129a8d5fb71a689e792929cd5089a7f4a50c5c61e8ec6d2ef570b0a5cd25e717ac7b33d
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in game_of_life.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # GameOfLife
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/game_of_life`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'game_of_life'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install game_of_life
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake false` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/game_of_life.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
Binary file
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "game_of_life"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/gameoflife ADDED
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'game_of_life'
4
+
5
+ if ARGV[0] == '-h' then
6
+ puts "Usage: gameoflife <filename> <num_generations>"
7
+ return
8
+ end
9
+
10
+ f = File.new(ARGV[0], "r")
11
+ map = GameOfLife.new
12
+ map.from_s f.read
13
+
14
+
15
+
16
+ ARGV[1].to_i.times do |i|
17
+ puts "Generation #{i}"
18
+ puts "-" * map.width
19
+ puts ""
20
+ puts map.to_s
21
+ puts ""
22
+ puts "-" * map.width
23
+ map.next_generation!
24
+ end
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,36 @@
1
+ module GameOfLife
2
+
3
+ #Represents a single cell in a Game of Life
4
+ class Cell
5
+
6
+ attr_accessor :state
7
+
8
+ def initialize(state = :dead)
9
+ @state = state
10
+ end
11
+
12
+ def update_state(neighbors)
13
+ live_neighbors = neighbors.select { |cell| cell.state == :live }
14
+ case live_neighbors.size
15
+ when 0..1
16
+ @state = :dead
17
+ when 3
18
+ @state = :live
19
+ when 4..8
20
+ @state = :dead
21
+ end
22
+ self
23
+ end
24
+
25
+ def to_s
26
+ case @state
27
+ when :live
28
+ "#"
29
+ when :dead
30
+ "."
31
+ end
32
+ end
33
+
34
+ end
35
+
36
+ end
@@ -0,0 +1,97 @@
1
+ require_relative './cell'
2
+
3
+ module GameOfLife
4
+
5
+ # Represents a 2-dimensional collection of Cells
6
+ class Map
7
+
8
+ include Enumerable
9
+
10
+ attr_reader :width, :height
11
+ attr_accessor :cells
12
+
13
+ def initialize(width=10, height=10)
14
+ @width, @height = width, height
15
+ @cells = Array.new(@width) { Array.new(@height) { GameOfLife::Cell.new } }
16
+ end
17
+
18
+ def [](x,y)
19
+ @cells[y].nil? || y < 0 || x < 0 ? [] : @cells[y][x]
20
+ end
21
+
22
+ def []=(x,y, value)
23
+ @cells[y][x] = value
24
+ end
25
+
26
+
27
+ def each &block
28
+ @cells.each do |x|
29
+ x.each do |cell|
30
+ yield cell
31
+ end
32
+ end
33
+ end
34
+
35
+ def to_s
36
+ result_string = ""
37
+ @cells.each do |x|
38
+ x.each do |cell|
39
+ result_string += cell.to_s
40
+ end
41
+ result_string += "\n"
42
+ end
43
+ result_string
44
+ end
45
+
46
+ def from_s(map)
47
+ return if map.empty?
48
+ new_cells = []
49
+ new_row = []
50
+ map.each_char do |c|
51
+ case c
52
+ when "."
53
+ new_row << GameOfLife::Cell.new(:dead)
54
+ when "#"
55
+ new_row << GameOfLife::Cell.new(:live)
56
+ when "\n"
57
+ new_cells << new_row.dup
58
+ new_row = []
59
+ end
60
+ end
61
+ @cells = new_cells
62
+ @width = new_cells[0].length
63
+ @height = new_cells.size
64
+ end
65
+
66
+ def next_generation
67
+ next_map = deep_copy
68
+ for y in 0..@height-1 do
69
+ for x in 0..@width-1 do
70
+ neighbors = [self[x-1,y-1],
71
+ self[x,y-1],
72
+ self[x+1,y-1],
73
+ self[x-1,y],
74
+ self[x+1,y],
75
+ self[x-1,y+1],
76
+ self[x,y+1],
77
+ self[x+1,y+1]].flatten.keep_if { |n| !n.nil? }
78
+ next_map[x,y].update_state neighbors
79
+ end
80
+ end
81
+ next_map
82
+ end
83
+
84
+ def next_generation!
85
+ next_map = next_generation
86
+ @cells = next_map.cells
87
+ end
88
+
89
+ def deep_copy
90
+ copy_map = self.dup
91
+ copy_map.cells = Marshal.load(Marshal.dump(@cells))
92
+ copy_map
93
+ end
94
+
95
+ end
96
+
97
+ end
@@ -0,0 +1,3 @@
1
+ module GameOfLife
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,7 @@
1
+ require "game_of_life/version"
2
+ require "game_of_life/cell"
3
+ require "game_of_life/map"
4
+
5
+ module GameOfLife
6
+
7
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'game_of_life/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ruby_game_of_life"
8
+ spec.version = GameOfLife::VERSION
9
+ spec.authors = ["Conrad King (RainbowReich)"]
10
+ spec.email = ["rainbowryke@gmail.com"]
11
+
12
+ spec.summary = %q{Simulates Conway's Game of Life.}
13
+ spec.description = %q{}
14
+ spec.homepage = "https://github.com/RainbowReich/ruby_game_of_life"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
22
+ end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.10"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_game_of_life
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Conrad King (RainbowReich)
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-10-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: ''
42
+ email:
43
+ - rainbowryke@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".rspec"
50
+ - ".travis.yml"
51
+ - Gemfile
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - bin/.gameoflife.swp
56
+ - bin/console
57
+ - bin/gameoflife
58
+ - bin/setup
59
+ - lib/.game_of_life.rb.swp
60
+ - lib/game_of_life.rb
61
+ - lib/game_of_life/.cell.rb.swp
62
+ - lib/game_of_life/.map.rb.swo
63
+ - lib/game_of_life/.map.rb.swp
64
+ - lib/game_of_life/cell.rb
65
+ - lib/game_of_life/map.rb
66
+ - lib/game_of_life/version.rb
67
+ - ruby_game_of_life.gemspec
68
+ homepage: https://github.com/RainbowReich/ruby_game_of_life
69
+ licenses:
70
+ - MIT
71
+ metadata: {}
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 2.4.8
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: Simulates Conway's Game of Life.
92
+ test_files: []