vida 0.0.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 756d6b149220f7354906b7bc82a209c6eee8ca0a
4
+ data.tar.gz: 21561c7c6ba6fb14621c1d43427dc5d550164afd
5
+ SHA512:
6
+ metadata.gz: 5ed4241e0b9423fac20de35e363251566df97fdf5c12176682a3da981e9ab74717c642898a77109aac6da472b5b9bf7f5adb3a36a0b481993ffa88df68346228
7
+ data.tar.gz: 46cf8bb0238c47a3bf4dcad3c7c0654ddd06b39e2d74bc828bf044e362edd45968e95de393aa2bd7a68fd05587d1ffe3efdeb8624abcd8571cbe9d46706da90f
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in vida.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Mario Viapiano
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # Vida
2
+
3
+ The Game of Life, also known simply as Life, is a cellular automaton devised by
4
+ the British mathematician John Horton Conway in 1970.
5
+
6
+ ## Rules
7
+ The universe of the Game of Life is an infinite two-dimensional orthogonal grid of
8
+ square cells, each of which is in one of two possible states, alive or dead.
9
+ Every cell interacts with its eight neighbours, which are the cells that are
10
+ horizontally, vertically, or diagonally adjacent. At each step in time, the
11
+ following transitions occur:
12
+
13
+ 1. Any live cell with fewer than two live neighbours dies, as if caused by
14
+ under-population.
15
+ 2. Any live cell with two or three live neighbours lives on to the next generation.
16
+ 3. Any live cell with more than three live neighbours dies, as if by overcrowding.
17
+ 4. Any dead cell with exactly three live neighbours becomes a live cell, as if by
18
+ reproduction.
19
+
20
+ The initial pattern constitutes the seed of the system. The first generation is
21
+ created by applying the above rules simultaneously to every cell in the
22
+ seed—births and deaths occur simultaneously, and the discrete moment at which
23
+ this happens is sometimes called a tick (in other words, each generation is a
24
+ pure function of the preceding one). The rules continue to be applied repeatedly
25
+ to create further generations.
26
+
27
+ ## Installation
28
+
29
+ Add this line to your application's Gemfile:
30
+
31
+ ```ruby
32
+ gem 'vida'
33
+ ```
34
+
35
+ And then execute:
36
+
37
+ $ bundle
38
+
39
+ Or install it yourself as:
40
+
41
+ $ gem install vida
42
+
43
+ ## Usage
44
+
45
+ TODO: Write usage instructions here
46
+
47
+ ## Contributing
48
+
49
+ 1. Fork it ( https://github.com/emeve89/vida/fork )
50
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
51
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
52
+ 4. Push to the branch (`git push origin my-new-feature`)
53
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/vida ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'vida'
4
+
5
+ Vida::Game.new(Vida::Grid.new).play
data/lib/vida.rb ADDED
@@ -0,0 +1,8 @@
1
+ require "vida/version"
2
+ require "vida/game"
3
+ require "vida/grid"
4
+ require "vida/cell"
5
+
6
+ module Vida
7
+ # Your code goes here...
8
+ end
data/lib/vida/cell.rb ADDED
@@ -0,0 +1,17 @@
1
+ class Vida::Cell
2
+ attr_reader :alive, :x, :y
3
+
4
+ def initialize(args)
5
+ @alive = args.fetch(:alive)
6
+ @x = args.fetch(:x)
7
+ @y = args.fetch(:y)
8
+ end
9
+
10
+ def update_status(live_cells_around)
11
+ @alive = alive ? (2..3) === live_cells_around : 3 == live_cells_around
12
+ end
13
+
14
+ def to_s
15
+ alive ? 'o' : ' '
16
+ end
17
+ end
data/lib/vida/game.rb ADDED
@@ -0,0 +1,20 @@
1
+ class Vida::Game
2
+ attr_reader :grid
3
+
4
+ def initialize(grid)
5
+ @grid = grid
6
+ end
7
+
8
+ def play
9
+ loop do
10
+ next_generation
11
+ system('clear')
12
+ puts grid
13
+ sleep(0.05)
14
+ end
15
+ end
16
+
17
+ def next_generation
18
+ grid.update_cells
19
+ end
20
+ end
data/lib/vida/grid.rb ADDED
@@ -0,0 +1,102 @@
1
+ class Vida::Grid
2
+ attr_reader :rows, :columns, :elements
3
+
4
+ def initialize(args = {})
5
+ @rows = args.fetch(:rows, 50)
6
+ @columns = args.fetch(:columns, 50)
7
+ @elements = Array.new(rows) do |row|
8
+ Array.new(columns) do |column|
9
+ Vida::Cell.new(x: column, y: row, alive: random_status)
10
+ end
11
+ end
12
+ @generation_number = 0
13
+ end
14
+
15
+ def live_cells_around(cell)
16
+ cells = []
17
+ cells << [
18
+ north(cell),
19
+ south(cell),
20
+ west(cell),
21
+ east(cell),
22
+ ul_corner(cell),
23
+ ur_corner(cell),
24
+ dl_corner(cell),
25
+ dr_corner(cell)
26
+ ]
27
+ cells.flatten!.count { |c| !c.nil? && c.alive == true }
28
+ end
29
+
30
+ def update_cells
31
+ elements.each do |column|
32
+ column.each do |element|
33
+ element.update_status(live_cells_around(element))
34
+ end
35
+ end
36
+ @generation_number += 1
37
+ end
38
+
39
+ private
40
+
41
+ def to_s
42
+ elements.each do |c|
43
+ c.each do |r|
44
+ print r
45
+ end
46
+ print "\n"
47
+ end
48
+ puts "Generation: #{@generation_number}"
49
+ end
50
+
51
+ def north(cell)
52
+ if cell.y > 0
53
+ elements[cell.x][cell.y - 1]
54
+ end
55
+ end
56
+
57
+ def south(cell)
58
+ if cell.y < (rows - 1)
59
+ elements[cell.x][cell.y + 1]
60
+ end
61
+ end
62
+
63
+ def west(cell)
64
+ if cell.x > 0
65
+ elements[cell.x - 1][cell.y]
66
+ end
67
+ end
68
+
69
+ def east(cell)
70
+ if cell.x < (columns - 1)
71
+ elements[cell.x + 1][cell.y]
72
+ end
73
+ end
74
+
75
+ def ul_corner(cell)
76
+ if cell.x > 0 && cell.y > 0
77
+ elements[cell.x - 1][cell.y - 1]
78
+ end
79
+ end
80
+
81
+ def ur_corner(cell)
82
+ if cell.x < (columns - 1) && cell.y > 0
83
+ elements[cell.x + 1][cell.y - 1]
84
+ end
85
+ end
86
+
87
+ def dl_corner(cell)
88
+ if cell.x > 0 && cell.y < (rows - 1)
89
+ elements[cell.x - 1][cell.y + 1]
90
+ end
91
+ end
92
+
93
+ def dr_corner(cell)
94
+ if cell.x < (columns - 1) && cell.y < (rows - 1)
95
+ elements[cell.x + 1][cell.y + 1]
96
+ end
97
+ end
98
+
99
+ def random_status
100
+ [true, false].sample
101
+ end
102
+ end
@@ -0,0 +1,3 @@
1
+ module Vida
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,54 @@
1
+ require_relative '../../lib/vida/cell'
2
+
3
+ describe Vida::Cell do
4
+ let(:cell) { Vida::Cell.new(x: 5, y: 5, alive: false) }
5
+
6
+ it "should respond to alive" do
7
+ expect(cell.respond_to?(:alive)).to eq true
8
+ end
9
+ it "should respond to x" do
10
+ expect(cell.respond_to?(:x)).to eq true
11
+ end
12
+ it "should respond to y" do
13
+ expect(cell.respond_to?(:y)).to eq true
14
+ end
15
+
16
+ describe ".initialize method" do
17
+ it "sets all the object attributes" do
18
+ expect(cell.x).to eq 5
19
+ expect(cell.y).to eq 5
20
+ expect(cell.alive).to eq false
21
+ end
22
+ end
23
+ describe "#update_status" do
24
+ context "with a living cell" do
25
+ c = Vida::Cell.new(x: 5, y: 5, alive: true)
26
+ it "sets alive to true if argument is 2 or 3" do
27
+ expect(c.update_status(2)).to eq true
28
+ end
29
+ it "sets alive to false if arguments is not 2 or 3" do
30
+ expect(c.update_status(1)).to eq false
31
+ end
32
+ end
33
+ context "with a dead cell" do
34
+ c = Vida::Cell.new(x: 5, y: 5, alive: false)
35
+ it "sets aliva to true if argument is 3" do
36
+ expect(c.update_status(3)).to eq true
37
+ end
38
+ it "sets alive to false if argument is not 3" do
39
+ expect(c.update_status(1)).to eq false
40
+ end
41
+ end
42
+ end
43
+
44
+ describe "#to_s" do
45
+ it "returns 'o' if the cell is alive" do
46
+ c = Vida::Cell.new(x: 5, y:5, alive: true)
47
+ expect(c.to_s).to eq "o"
48
+ end
49
+ it "returns ' ' if the cell is dead" do
50
+ c = Vida::Cell.new(x: 5, y:5, alive: false)
51
+ expect(c.to_s).to eq " "
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,8 @@
1
+ require_relative '../../lib/vida/game'
2
+
3
+ describe Vida::Game do
4
+
5
+ let(:grid) { Grid.new }
6
+ let(:game) { Game.new(grid) }
7
+
8
+ end
@@ -0,0 +1,70 @@
1
+ require_relative '../../lib/vida/grid'
2
+ require_relative '../../lib/vida/cell'
3
+
4
+ describe Vida::Grid do
5
+ let(:grid) { Vida::Grid.new }
6
+
7
+ it "should respond to rows" do
8
+ expect(grid.respond_to?(:rows)).to eq true
9
+ end
10
+ it "should respond to columns" do
11
+ expect(grid.respond_to?(:columns)).to eq true
12
+ end
13
+ it "should respond to elements" do
14
+ expect(grid.respond_to?(:elements)).to eq true
15
+ end
16
+ it "should respond to live_cells_around" do
17
+ expect(grid.respond_to?(:live_cells_around)).to eq true
18
+ end
19
+
20
+ describe "#live_cells_around" do
21
+ it "returns an array with the cells around a given cell" do
22
+ stubbed_response = [
23
+ [Vida::Cell.new(x: 0, y: 0, alive: true), Vida::Cell.new(x: 0, y: 1, alive: true), Vida::Cell.new(x: 0, y: 2, alive: false)],
24
+ [Vida::Cell.new(x: 1, y: 0, alive: false), Vida::Cell.new(x: 1, y: 1, alive: false), Vida::Cell.new(x: 1, y: 2, alive: false)],
25
+ [Vida::Cell.new(x: 2, y: 0, alive: true), Vida::Cell.new(x: 2, y: 1, alive: false), Vida::Cell.new(x: 2, y: 2, alive: false)]
26
+ ]
27
+ grid.stub(:elements).and_return(stubbed_response)
28
+ grid.stub(:columns).and_return(3)
29
+ grid.stub(:rows).and_return(3)
30
+
31
+ expect(grid.live_cells_around(grid.elements[0][0])).to eq 1
32
+ expect(grid.live_cells_around(grid.elements[0][1])).to eq 1
33
+ expect(grid.live_cells_around(grid.elements[0][2])).to eq 1
34
+ expect(grid.live_cells_around(grid.elements[1][0])).to eq 3
35
+ expect(grid.live_cells_around(grid.elements[1][1])).to eq 3
36
+ expect(grid.live_cells_around(grid.elements[1][2])).to eq 1
37
+ expect(grid.live_cells_around(grid.elements[2][0])).to eq 0
38
+ expect(grid.live_cells_around(grid.elements[2][1])).to eq 1
39
+ expect(grid.live_cells_around(grid.elements[2][2])).to eq 0
40
+ end
41
+ end
42
+
43
+ describe ".initialize method" do
44
+ context 'with arguments' do
45
+ it "creates a grid with the given size" do
46
+ grid_wa = Vida::Grid.new(rows: 10, columns: 15)
47
+ expect(grid_wa.rows).to eq 10
48
+ expect(grid_wa.columns).to eq 15
49
+ end
50
+ end
51
+ context 'without arguments' do
52
+ it "creates a grid with default values" do
53
+ expect(grid.rows).to eq 50
54
+ expect(grid.columns).to eq 50
55
+ end
56
+ end
57
+ end
58
+
59
+ describe "Grid elements" do
60
+ it "should be a matrix of Vida::Cell objects" do
61
+ expect(grid.elements.is_a?(Array)).to be true
62
+ grid.elements.each do |column|
63
+ expect(column.is_a?(Array)).to be true
64
+ column.each do |element|
65
+ expect(element.is_a?(Vida::Cell))
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
data/vida.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'vida/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "vida"
8
+ spec.version = Vida::VERSION
9
+ spec.authors = ["Mario Viapiano"]
10
+ spec.email = ["marioeviapiano@gmail.com"]
11
+ spec.summary = "Game of Life"
12
+ spec.description = "Game of Life - emeve89 edition"
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "3.1.0"
24
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vida
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Mario Viapiano
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-22 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
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.1.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.1.0
55
+ description: Game of Life - emeve89 edition
56
+ email:
57
+ - marioeviapiano@gmail.com
58
+ executables:
59
+ - vida
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/vida
69
+ - lib/vida.rb
70
+ - lib/vida/cell.rb
71
+ - lib/vida/game.rb
72
+ - lib/vida/grid.rb
73
+ - lib/vida/version.rb
74
+ - spec/vida/cell_spec.rb
75
+ - spec/vida/game_spec.rb
76
+ - spec/vida/grid_spec.rb
77
+ - vida.gemspec
78
+ homepage: ''
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.4.5
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Game of Life
102
+ test_files:
103
+ - spec/vida/cell_spec.rb
104
+ - spec/vida/game_spec.rb
105
+ - spec/vida/grid_spec.rb