swarmer_life 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ee8a0b440af8900073e498d53d16b2111c095d5e
4
+ data.tar.gz: a75a084ee84d23fd588cd41064a546d78bd6606c
5
+ SHA512:
6
+ metadata.gz: 3778ffaface95ce100247063657314c0c8c81ea0995329e58ac8c090277aeaa479b87345ce87d954e0319297e1a90e5b8f129f3f5dcc8f2cbe3200383b1708ec
7
+ data.tar.gz: 8ef8ddc960025f9ac71dd1c90ab773947db0efaa157e1124873cc28f7c06f46ee799911eb6e95e919eca2c8140836202231e2fbaa760c133cbeac98faf7bd828
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,34 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ swarmer_life (0.1.0)
5
+ curses (~> 1.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ curses (1.0.1)
11
+ diff-lcs (1.2.5)
12
+ rake (10.4.2)
13
+ rspec (3.2.0)
14
+ rspec-core (~> 3.2.0)
15
+ rspec-expectations (~> 3.2.0)
16
+ rspec-mocks (~> 3.2.0)
17
+ rspec-core (3.2.3)
18
+ rspec-support (~> 3.2.0)
19
+ rspec-expectations (3.2.1)
20
+ diff-lcs (>= 1.2.0, < 2.0)
21
+ rspec-support (~> 3.2.0)
22
+ rspec-mocks (3.2.1)
23
+ diff-lcs (>= 1.2.0, < 2.0)
24
+ rspec-support (~> 3.2.0)
25
+ rspec-support (3.2.2)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ bundler (~> 1.9)
32
+ rake (~> 10.0)
33
+ rspec (~> 3.0)
34
+ swarmer_life!
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Anton Barkovsky
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,15 @@
1
+ # Task 3
2
+ Game of Life and RSpec
3
+
4
+ ## Usage
5
+ $ gem install swarmer_life
6
+
7
+ In ruby:
8
+
9
+ require "life"
10
+ Life::UI.execute()
11
+
12
+
13
+
14
+ ## Why is there some much shit here?
15
+ Powered by `bundle gem`
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "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
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ lib = File.expand_path("../lib", File.dirname(__FILE__))
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+
6
+ require "life"
7
+
8
+ Life::UI.execute()
@@ -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
@@ -0,0 +1,9 @@
1
+ require "curses"
2
+
3
+ module Life
4
+ LIB_PATH = File.dirname(__FILE__)
5
+ end
6
+
7
+ require "life/version"
8
+ require "life/ui"
9
+ require "life/board"
@@ -0,0 +1,116 @@
1
+ module Life
2
+ class Board
3
+ attr_reader :width, :height
4
+
5
+ def initialize(width, height)
6
+ if width < 3 || height < 3
7
+ raise ArgumentError.new("Life Board dimensions must be bigger than 3")
8
+ end
9
+
10
+ @width = width
11
+ @height = height
12
+ @cells = Array.new(height) do
13
+ Array.new(width) { false }
14
+ end
15
+ end
16
+
17
+ def [](x, y)
18
+ wx, wy = wrapped_coords(x, y)
19
+ @cells[wy][wx]
20
+ end
21
+
22
+ def []=(x, y, value)
23
+ wx, wy = wrapped_coords(x, y)
24
+ @cells[wy][wx] = value
25
+ end
26
+
27
+ def to_a
28
+ clone_2d_array(@cells)
29
+ end
30
+
31
+ def update_from_array(cells)
32
+ @cells = clone_2d_array(cells)
33
+ end
34
+
35
+ def self.from_array(cells)
36
+ width = cells[0].size
37
+ height = cells.size
38
+ board = new(width, height)
39
+ board.update_from_array(cells)
40
+ board
41
+ end
42
+
43
+ def self.from_text(text)
44
+ lines = text.split("\n")
45
+
46
+ width = lines[0].size
47
+ height = lines.size
48
+ board = Life::Board.new(width, height)
49
+
50
+ lines.each_with_index do |line, y|
51
+ line.each_char.with_index do |char, x|
52
+ board[x, y] = (char == "#")
53
+ end
54
+ end
55
+
56
+ board
57
+ end
58
+
59
+ def count_neighbors(x, y)
60
+ count = 0
61
+
62
+ each_neighbor(x, y) do |nx, ny, alive|
63
+ count += 1 if alive
64
+ end
65
+
66
+ count
67
+ end
68
+
69
+ def clone
70
+ self.class.from_array(@cells)
71
+ end
72
+
73
+ def each
74
+ (0...height).each do |y|
75
+ (0...width).each do |x|
76
+ yield x, y, self[x, y]
77
+ end
78
+ end
79
+ end
80
+
81
+ def each_neighbor(x, y)
82
+ (-1..1).each do |dy|
83
+ (-1..1).each do |dx|
84
+ next if dx == 0 && dy == 0
85
+
86
+ nx = x + dx
87
+ ny = y + dy
88
+ yield nx, ny, self[nx, ny]
89
+ end
90
+ end
91
+ end
92
+
93
+ def tick
94
+ old_board = clone()
95
+ each do |x, y, alive|
96
+ neighbor_count = old_board.count_neighbors(x, y)
97
+
98
+ if alive
99
+ self[x, y] = (2..3).include?(neighbor_count)
100
+ else
101
+ self[x, y] = (neighbor_count == 3)
102
+ end
103
+ end
104
+ end
105
+
106
+ private
107
+
108
+ def clone_2d_array(arr)
109
+ arr.map { |row| row.clone }
110
+ end
111
+
112
+ def wrapped_coords(x, y)
113
+ [x % width, y % height]
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,18 @@
1
+
2
+ # #
3
+ # #
4
+ ### ## ###
5
+
6
+
7
+
8
+
9
+
10
+
11
+
12
+
13
+
14
+
15
+
16
+
17
+
18
+
@@ -0,0 +1,51 @@
1
+ module Life
2
+ class UI
3
+ DELAY = 0.05
4
+
5
+ def self.execute
6
+ ui = new(load_board("life/glider.txt"))
7
+
8
+ loop do
9
+ ui.render()
10
+ sleep(DELAY)
11
+ ui.update()
12
+ end
13
+ rescue Interrupt, IRB::Abort
14
+ return
15
+ ensure
16
+ ui.close()
17
+ end
18
+
19
+ def self.load_board(relative_path)
20
+ path = File.join(LIB_PATH, relative_path)
21
+ Life::Board.from_text(File.read(path))
22
+ end
23
+
24
+ def initialize(board)
25
+ Curses.init_screen()
26
+ Curses.curs_set(0)
27
+ @window = Curses::Window.new(0, 0, 0, 0)
28
+
29
+ @board = board
30
+ end
31
+
32
+ def render
33
+ @window.clear()
34
+
35
+ @board.each do |x, y, alive|
36
+ @window.setpos(y, x)
37
+ @window.addstr(alive ? "#" : " ")
38
+ end
39
+
40
+ @window.refresh()
41
+ end
42
+
43
+ def update
44
+ @board.tick()
45
+ end
46
+
47
+ def close
48
+ Curses::close_screen()
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,3 @@
1
+ module Life
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'life/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "swarmer_life"
8
+ spec.version = Life::VERSION
9
+ spec.authors = ["Anton Barkovsky"]
10
+ spec.email = ["anton@swarmer.me"]
11
+
12
+ spec.summary = %q{Task 3}
13
+ spec.homepage = "https://github.com/rubyroidlabs/bsuir-courses"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.9"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+
25
+ spec.add_dependency "curses", "~> 1.0"
26
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: swarmer_life
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Anton Barkovsky
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-05-17 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.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
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.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.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: curses
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.0'
69
+ description:
70
+ email:
71
+ - anton@swarmer.me
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".rspec"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - Gemfile.lock
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/life
85
+ - bin/setup
86
+ - lib/life.rb
87
+ - lib/life/board.rb
88
+ - lib/life/glider.txt
89
+ - lib/life/ui.rb
90
+ - lib/life/version.rb
91
+ - swarmer_life.gemspec
92
+ homepage: https://github.com/rubyroidlabs/bsuir-courses
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.4.6
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Task 3
116
+ test_files: []