game_of_life_gto 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ae414108485b0317e78332d69763a11b3689b4585688f9a7bd89c70f84b0c050
4
+ data.tar.gz: 5288cb74a865a41160a57c8db5ea631e7e90e6d0e6b5074c6f92e2076d91949e
5
+ SHA512:
6
+ metadata.gz: dcc0c002423ba3bea0b16f2632b5bccfe99e65262ff2cf20d39c0cf0ebdda4c6877ea436420ff00637f49545d2d48d7e5588ac7eb6656a54837fde545f854edd
7
+ data.tar.gz: c66702d714612d629c91b3b6c927142661d11fce8089ddf7a11b38c6d0474094bdb99c99ab813d8c971b50688d28cbdabf09c564c0790a0724ef6359b66e581f
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -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 j.loyolarangel@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,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in game_of_life_gto.gemspec
6
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Jaime
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.
@@ -0,0 +1,48 @@
1
+ # Conway's Game of Life by JJaimelr
2
+ This is an implementation of the classic Conway's Game of Life using the Ruby
3
+ language.
4
+
5
+ The is displayed in console.
6
+
7
+ ## Patterns
8
+ The game has attached many common patterns of different types discovered in the Game of life.
9
+
10
+ ### Still Life
11
+ The block:
12
+ ![Block](http://web.stanford.edu/%7Ecdebs/GameOfLife/img/still/block.png)
13
+
14
+ ### Oscillators
15
+ - Blinker:
16
+ ![Blincker](http://web.stanford.edu/%7Ecdebs/GameOfLife/img/oscillators/blinker.gif)
17
+ - Beacon:
18
+ ![Beacon](http://web.stanford.edu/%7Ecdebs/GameOfLife/img/oscillators/beacon.gif)
19
+ - Pulsar:
20
+ ![Pulsar](http://web.stanford.edu/%7Ecdebs/GameOfLife/img/oscillators/toad.gif)
21
+ - Gliders and Spaceships
22
+ ![Glider](http://web.stanford.edu/%7Ecdebs/GameOfLife/img/spaceships/glider.gif)
23
+
24
+ ## Installation
25
+
26
+ Add this line to your application's Gemfile:
27
+
28
+ gem 'game_of_life_gto'
29
+
30
+ And then execute:
31
+
32
+ $ bundle
33
+
34
+ Or install it yourself as:
35
+
36
+ $ gem install game_of_life_gto
37
+
38
+ ## Usage
39
+
40
+ Open your IRB console with:
41
+
42
+ $ irb
43
+
44
+ The start the game with the following command:
45
+
46
+ > Life.start
47
+
48
+ And let the fun begin! (So to say).
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "game_of_life_gto"
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(__FILE__)
@@ -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
@@ -0,0 +1,36 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "game_of_life_gto/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "game_of_life_gto"
8
+ spec.version = GameOfLifeGto::VERSION
9
+ spec.authors = ["Jaimelr"]
10
+ spec.email = ["j.loyolarangel@gmail.com"]
11
+
12
+ spec.summary = %q{A Ruby implementation of Conway's Game of Life}
13
+ spec.description = %q{A Ruby implementation of Conway's Game of Life to play in terminal with example patterns to watch}
14
+ spec.homepage = "https://github.com/jaimelr/game-of-life"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against " \
23
+ "public gem pushes."
24
+ end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.16"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "rspec", "~> 3.3.0"
36
+ end
@@ -0,0 +1,224 @@
1
+ require "game_of_life_gto/version"
2
+
3
+ module GameOfLifeGto
4
+ class Board
5
+ attr_accessor :array, :width, :height
6
+
7
+ def initialize(width, height)
8
+ @width = width
9
+ @height = height
10
+ @array = Array.new(@width) do
11
+ Array.new(@height) { Cell.new(rand(0..1)) }
12
+ end
13
+ end
14
+
15
+ def reset
16
+ @array.each do |row|
17
+ row.each { |cell| cell.id = 0 }
18
+ end
19
+ end
20
+
21
+ def display
22
+ @array.each do |row|
23
+ row.each { |cell| print cell.id }
24
+ print "\n"
25
+ end
26
+ end
27
+ end
28
+
29
+ class Cell
30
+ attr_accessor :id
31
+
32
+ def initialize(id)
33
+ @id = id
34
+ end
35
+
36
+ def is_alive?
37
+ if @id != 0
38
+ return true
39
+ end
40
+ return false
41
+ end
42
+
43
+ def dies
44
+ @id = 0
45
+ end
46
+
47
+ def resuscitates
48
+ @id = 1
49
+ end
50
+ end
51
+
52
+ class Game
53
+ attr_reader :board
54
+
55
+ def initialize(board)
56
+ @board = board
57
+ @temp_board = Marshal::load(Marshal.dump(board))
58
+ @temp_board.reset
59
+ end
60
+
61
+ def play(loop_size)
62
+ loop_size.times do
63
+ thick
64
+ @board = Marshal::load(Marshal.dump(@temp_board))
65
+ @board.display
66
+ @temp_board.reset
67
+ sleep 0.5
68
+ system('clear')
69
+ end
70
+ end
71
+
72
+ def thick
73
+ @board.array.each_with_index do |row, y|
74
+ row.each_with_index do |cell, x|
75
+ neighborhood = get_neighborhood(x, y)
76
+ if cell.is_alive?
77
+ if deserves_to_die?(neighborhood)
78
+ @temp_board.array[y][x].dies
79
+ else
80
+ @temp_board.array[y][x].id = 1
81
+ end
82
+ else
83
+ if deserves_to_revive?(neighborhood)
84
+ @temp_board.array[y][x].resuscitates
85
+ else
86
+ @temp_board.array[y][x].id = 0
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
92
+
93
+ private
94
+ def get_neighborhood(x, y)
95
+ neighborhood = Array.new
96
+ coordinates = { x: x - 1, y: y - 1 }
97
+ 3.times do
98
+ 3.times do
99
+ if outter_coordinates?(coordinates)
100
+ neighborhood << Cell.new(0)
101
+ else
102
+ if coordinates[:x] == x && coordinates[:y] == y
103
+ coordinates[:x] += 1
104
+ next
105
+ end
106
+ neighborhood.push(board.array[coordinates[:y]][coordinates[:x]])
107
+ end
108
+ coordinates[:x] += 1
109
+ end
110
+ coordinates[:x] = x -1
111
+ coordinates[:y] += 1
112
+ end
113
+
114
+ return neighborhood
115
+ end
116
+
117
+ def outter_coordinates?(coordinates)
118
+ if coordinates[:x] < 0 || coordinates[:x] > board.width - 1 || coordinates[:y] < 0 || coordinates[:y] > board.height - 1
119
+ return true
120
+ end
121
+
122
+ return false
123
+ end
124
+
125
+ def deserves_to_die?(neighborhood)
126
+ points = 0
127
+ neighborhood.each { |cell| points += 1 if cell.is_alive? }
128
+ return true if points < 2 || points > 3
129
+ return false if points == 2 || points == 3
130
+ end
131
+
132
+ def deserves_to_revive?(neighborhood)
133
+ points = 0
134
+ neighborhood.each { |cell| points += 1 if cell.is_alive? }
135
+ return true if points == 3
136
+
137
+ return false
138
+ end
139
+ end
140
+
141
+ class Life
142
+ def self.start
143
+ puts 'Conway\'s Game of Life - JJaimelr version'
144
+ puts 'WELCOME'
145
+ puts 'Please choose an option to watch the game in action'
146
+ puts '[1] random'
147
+ puts '[2] pattern'
148
+ option = gets.chomp.to_s
149
+ case option
150
+ when 'random', '1'
151
+ puts 'Enter the loop size'
152
+ loop_size = gets.chomp.to_i
153
+ board = Board.new(40, 40)
154
+ blinkerBoard = Board.new(5, 5)
155
+ board.display
156
+ game = Game.new(board)
157
+ game.play loop_size
158
+ when 'pattern', '2'
159
+ self.watch_pattern
160
+ else
161
+ puts '*Confused* Do you enter the number (or name) of the option? If so, could you please try again?'
162
+ end
163
+ end
164
+
165
+ def self.watch_pattern
166
+ puts 'Enter the name or the number of the pattern'
167
+ puts '[1] blinker'
168
+ puts '[2] beacon'
169
+ puts '[3] block'
170
+ puts '[4] pulsar'
171
+ puts '[5] glider'
172
+ pattern = gets.chomp
173
+ puts 'Enter the loop size'
174
+ loop_size = gets.chomp.to_i
175
+ case pattern
176
+ when 'blinker', '1'
177
+ board = Board.new(5, 5)
178
+ board.reset
179
+ board.array[2][1] = Cell.new(1)
180
+ board.array[2][2] = Cell.new(1)
181
+ board.array[2][3] = Cell.new(1)
182
+ when 'beacon', '2'
183
+ board = Board.new(6, 6)
184
+ board.reset
185
+ board.array[1][1] = Cell.new(1)
186
+ board.array[1][2] = Cell.new(1)
187
+ board.array[2][1] = Cell.new(1)
188
+ board.array[4][3] = Cell.new(1)
189
+ board.array[4][4] = Cell.new(1)
190
+ board.array[3][4] = Cell.new(1)
191
+ when 'block', '3'
192
+ board = Board.new(4, 4)
193
+ board.reset
194
+ board.array[1][1] = Cell.new(1)
195
+ board.array[1][2] = Cell.new(1)
196
+ board.array[2][1] = Cell.new(1)
197
+ board.array[2][2] = Cell.new(1)
198
+ when 'pulsar', '4'
199
+ board = Board.new(6, 6)
200
+ board.reset
201
+ board.array[2][2] = Cell.new(1)
202
+ board.array[2][3] = Cell.new(1)
203
+ board.array[2][4] = Cell.new(1)
204
+ board.array[3][1] = Cell.new(1)
205
+ board.array[3][2] = Cell.new(1)
206
+ board.array[3][3] = Cell.new(1)
207
+ when 'glider', '5'
208
+ board = Board.new(20, 20)
209
+ board.reset
210
+ board.array[1][2] = Cell.new(1)
211
+ board.array[2][3] = Cell.new(1)
212
+ board.array[3][1] = Cell.new(1)
213
+ board.array[3][2] = Cell.new(1)
214
+ board.array[3][3] = Cell.new(1)
215
+ board.display
216
+ else
217
+ puts 'Pattern not found'
218
+ end
219
+ game = Game.new(board)
220
+ board.display
221
+ game.play loop_size
222
+ end
223
+ end
224
+ end
@@ -0,0 +1,3 @@
1
+ module GameOfLifeGto
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: game_of_life_gto
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jaimelr
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-05-11 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.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
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
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.3.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.3.0
55
+ description: A Ruby implementation of Conway's Game of Life to play in terminal with
56
+ example patterns to watch
57
+ email:
58
+ - j.loyolarangel@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - CODE_OF_CONDUCT.md
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - game_of_life_gto.gemspec
72
+ - lib/game_of_life_gto.rb
73
+ - lib/game_of_life_gto/version.rb
74
+ homepage: https://github.com/jaimelr/game-of-life
75
+ licenses:
76
+ - MIT
77
+ metadata:
78
+ allowed_push_host: https://rubygems.org
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.7.6
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: A Ruby implementation of Conway's Game of Life
99
+ test_files: []