golife-cli 1.0.1

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: ebcb9bf0646c66b641818c73e94eccc16b7008c9
4
+ data.tar.gz: 8c0ab0afe52210717821876754ebabf9982db836
5
+ SHA512:
6
+ metadata.gz: eaac054645d300c9f4c0b1d2e9a9b55455b5584edf5a21ff10384c007c7521748b520a6d7e08a8b4444981425cf36db6aa043c708450f6291d5d8158baab046a
7
+ data.tar.gz: 825bbb8c9a767fddb74aec94ad3827827d314195fd0d6598df94c58b7301d1730bd9a60475af23a8ce15a6d72feec492323fa0d546154ecdf9c5fe339d62de6d
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/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.2
5
+ before_install: gem install bundler -v 1.13.0
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at vs4vijay@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in golife.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 vs4vijay
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,63 @@
1
+ # Golife
2
+ Conway's Game of Life Implementation in Ruby
3
+
4
+
5
+ ## Installation
6
+ ```
7
+ gem install golife-cli
8
+ ```
9
+
10
+ ## Usage
11
+ Just run the `golife` command as follows
12
+ ```
13
+ golife
14
+ ```
15
+
16
+ ## Development
17
+ To run in Development mode:
18
+ ```
19
+ bundle exec ./exe/golife
20
+ ```
21
+
22
+ Building gem:
23
+ ```
24
+ gem build golife-cli.gemspec
25
+ ```
26
+
27
+ Publishing to RubyGems:
28
+ ```
29
+ gem push golife-cli-<version>.gem
30
+ ```
31
+
32
+ ~~After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
33
+
34
+ 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).~~
35
+
36
+
37
+ ## To Do
38
+ - [x] Refactoring code and Optimize logic
39
+ - [ ] Add Ncurses
40
+ - [x] Create an executable gem
41
+ - [ ] Take input from user instead of random cells
42
+ - [ ] Add predefined cells like Glider, Gun, Blink, Box
43
+ - [ ] Command line arguments
44
+ - [ ] Unit tests
45
+ - [x] Zero player (random)
46
+ - [ ] Multiplayer support(LAN, Internet)
47
+ - [ ] Different colors of cells
48
+
49
+
50
+ ## Rules
51
+ - Any live cell with fewer than two live neighbours dies, as if by needs caused by underpopulation.
52
+ - Any live cell with more than three live neighbours dies, as if by overcrowding.
53
+ - Any live cell with two or three live neighbours lives, unchanged, to the next generation.
54
+ - Any dead cell with exactly three live neighbours becomes a live cell.
55
+
56
+
57
+ ## Contributing
58
+ Bug reports and pull requests are welcome on GitHub at https://github.com/vs4vijay/golife. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
59
+
60
+
61
+ ## License
62
+
63
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "golife"
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/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/golife ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "golife"
4
+
5
+ Golife::Game.new.play
data/golife.gemspec ADDED
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'golife/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "golife-cli"
8
+ spec.version = Golife::VERSION
9
+ spec.authors = ["vs4vijay"]
10
+ spec.email = ["vs4vijay@gmail.com"]
11
+
12
+ spec.summary = %q{Conway's Game of Life}
13
+ spec.homepage = "http://vs4vijay.github.io/"
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
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 do |f|
25
+ f.match(%r{^(test|spec|features)/})
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_development_dependency "bundler", "~> 1.13"
32
+ spec.add_development_dependency "rake", "~> 10.0"
33
+ # spec.add_development_dependency "minitest", "~> 5.0"
34
+ end
data/lib/golife.rb ADDED
@@ -0,0 +1,111 @@
1
+ # Conway's Game of Life Implementation in Ruby
2
+ # Author: Vijay Soni(vs4vijay@gmail.com)
3
+ require "golife/version"
4
+
5
+ SLEEP_INTERVAL = 0.2
6
+ GAME_HEADING = "Game of Life"
7
+
8
+ class Golife::Game
9
+ attr_accessor :playground, :width, :height, :generation
10
+
11
+ def initialize
12
+ self.generation = 0
13
+ self.width = 10
14
+ self.height = 10
15
+ self.playground = self.make_playground(self.height, self.width)
16
+ end
17
+
18
+ def play
19
+ loop do
20
+ system "clear"
21
+ show_playground
22
+ sleep SLEEP_INTERVAL
23
+ next_generation
24
+ end
25
+ end
26
+
27
+ def make_playground(rows, cols, type = "random")
28
+ (0..rows).map do |i|
29
+ (0..cols).map do |j|
30
+ if type == "empty"
31
+ cell_alive = false
32
+ elsif type == "random"
33
+ cell_alive = Random.new.rand(0..1) == 1
34
+ end
35
+ Cell.new(i, j, cell_alive)
36
+ end
37
+ end
38
+ end
39
+
40
+ def show_playground
41
+ puts "\t#{GAME_HEADING} [Generation: #{@generation}]"
42
+ @playground.each_with_index do |row, i|
43
+ print "|"
44
+ row.each_with_index do |cell, j|
45
+ print " #{cell.alive ? '*' : ' '} |"
46
+ end
47
+ print "\n\n"
48
+ end
49
+ end
50
+
51
+ def next_generation
52
+ @generation += 1
53
+ @playground = @playground.map do |row|
54
+ row.map do |cell|
55
+ # cell.alive = Random.new.rand(0..1) == 1
56
+ live_neighbours = neighbours_of(cell).select{|c| c.alive?}
57
+ if(cell.alive? && (live_neighbours.size == 2 || live_neighbours.size == 3))
58
+ cell.live!
59
+ elsif(cell.dead? && live_neighbours.size == 3)
60
+ cell.live!
61
+ else
62
+ cell.dead!
63
+ end
64
+ end
65
+ end
66
+ end
67
+
68
+ private
69
+
70
+ def neighbours_of(cell)
71
+ @playground.flatten.map do |n_cell|
72
+ n_cell if [cell.x + 1, cell.x, cell.x - 1].include?(n_cell.x) && [cell.y + 1, cell.y, cell.y - 1].include?(n_cell.y)
73
+ end.compact.reject{|c| c.x == cell.x && c.y == cell.y}
74
+ end
75
+
76
+ end
77
+
78
+ class Cell
79
+ attr_accessor :x, :y, :alive
80
+
81
+ def initialize(x, y, alive = false)
82
+ self.x = x
83
+ self.y = y
84
+ self.alive = alive
85
+ end
86
+
87
+ def alive?
88
+ @alive
89
+ end
90
+
91
+ def dead?
92
+ !@alive
93
+ end
94
+
95
+ def dead!
96
+ @alive = false
97
+ self
98
+ end
99
+
100
+ def live!
101
+ @alive = true
102
+ self
103
+ end
104
+
105
+ def to_s
106
+ "x: #{@x}, y: #{@y}"
107
+ end
108
+
109
+ end
110
+
111
+ Golife::Game.new.play if __FILE__ == $0
@@ -0,0 +1,3 @@
1
+ module Golife
2
+ VERSION = "1.0.1"
3
+ end
@@ -0,0 +1,108 @@
1
+ #!/usr/bin/env ruby
2
+ # Conway's Game of Life Implementation in Ruby
3
+ # Author: Vijay Soni(vs4vijay@gmail.com)
4
+ class Golife
5
+ attr_accessor :playground, :width, :height, :iteration
6
+
7
+ def initialize
8
+ self.iteration = 0
9
+ self.width = 10
10
+ self.height = 10
11
+ self.playground = self.make_playground(self.width, self.height)
12
+ end
13
+
14
+ def play
15
+ puts "Play"
16
+ loop do
17
+ system "clear"
18
+ show_playground
19
+ sleep 0.2
20
+ next_generation
21
+ end
22
+ end
23
+
24
+ def make_playground(rows, cols, type = "random")
25
+ (0..rows).map do |i|
26
+ (0..cols).map do |j|
27
+ if type == "empty"
28
+ cell_alive = false
29
+ elsif type == "random"
30
+ cell_alive = Random.new.rand(0..1) == 1
31
+ end
32
+ Cell.new(i, j, cell_alive)
33
+ end
34
+ end
35
+ end
36
+
37
+ def show_playground
38
+ puts "\tGame of Life [Iteration: #{@iteration}]"
39
+ @playground.each_with_index do |row, i|
40
+ print "|"
41
+ row.each_with_index do |cell, j|
42
+ print " #{cell.alive ? '*' : ' '} |"
43
+ end
44
+ print "\n\n"
45
+ end
46
+ end
47
+
48
+ def next_generation
49
+ @iteration += 1
50
+ @playground = @playground.map do |row|
51
+ row.map do |cell|
52
+ # cell.alive = Random.new.rand(0..1) == 1
53
+ live_neighbours = neighbours_of(cell).select{|c| c.alive?}
54
+ if(cell.alive? && (live_neighbours.size == 2 || live_neighbours.size == 3))
55
+ cell.dup.live!
56
+ elsif(cell.dead? && live_neighbours.size == 3)
57
+ cell.dup.live!
58
+ else
59
+ cell.dup.dead!
60
+ end
61
+ end
62
+ end
63
+ end
64
+
65
+ private
66
+
67
+ def neighbours_of(cell)
68
+ @playground.flatten.map do |n_cell|
69
+ n_cell if [cell.x + 1, cell.x, cell.x - 1].include?(n_cell.x) && [cell.y + 1, cell.y, cell.y - 1].include?(n_cell.y)
70
+ end.compact.reject{|c| c.x == cell.x && c.y == cell.y}
71
+ end
72
+
73
+ end
74
+
75
+ class Cell
76
+ attr_accessor :x, :y, :alive
77
+
78
+ def initialize(x, y, alive = false)
79
+ self.x = x
80
+ self.y = y
81
+ self.alive = alive
82
+ end
83
+
84
+ def alive?
85
+ @alive
86
+ end
87
+
88
+ def dead?
89
+ !@alive
90
+ end
91
+
92
+ def dead!
93
+ @alive = false
94
+ self
95
+ end
96
+
97
+ def live!
98
+ @alive = true
99
+ self
100
+ end
101
+
102
+ def to_s
103
+ "x: #{@x}, y: #{@y}"
104
+ end
105
+
106
+ end
107
+
108
+ Golife.new.play if __FILE__ == $0
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: golife-cli
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - vs4vijay
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-09-24 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.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
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
+ - vs4vijay@gmail.com
44
+ executables:
45
+ - golife
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - ".travis.yml"
51
+ - CODE_OF_CONDUCT.md
52
+ - Gemfile
53
+ - LICENSE.txt
54
+ - README.md
55
+ - Rakefile
56
+ - bin/console
57
+ - bin/setup
58
+ - exe/golife
59
+ - golife.gemspec
60
+ - lib/golife.rb
61
+ - lib/golife/version.rb
62
+ - original-golife.rb
63
+ homepage: http://vs4vijay.github.io/
64
+ licenses:
65
+ - MIT
66
+ metadata:
67
+ allowed_push_host: https://rubygems.org
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 2.4.5
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: Conway's Game of Life
88
+ test_files: []