game_grid 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: 550e52bf74f8f46b2c14e69b2b533aed12b90d53
4
+ data.tar.gz: d1c04f7e0a5b2c96d23c274f633ae9e9e6d82dfe
5
+ SHA512:
6
+ metadata.gz: 6dbf5ce44689c9f58acf8440bd68c237b6c9da466645b5232fdd03238acb3d834aed4fbbce94c6f86a2831884f5dcea87349e66d1d5ba3801ed610124ee3aa7c
7
+ data.tar.gz: c887acd7f41ab471f3671ff54df6920798f56c6584a0014e10015dde4fee0379551506e47cca6fc1ff2bf2f1a9f1ca887bc43347b46cd1611f183e6ba7632fdd
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.3.1
5
+ before_install: gem install bundler -v 1.13.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in game_grid.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,43 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ ## Uncomment and set this to only include directories you want to watch
5
+ # directories %w(app lib config test spec features) \
6
+ # .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}
7
+
8
+ ## Note: if you are using the `directories` clause above and you are not
9
+ ## watching the project directory ('.'), then you will want to move
10
+ ## the Guardfile to a watched dir and symlink it back, e.g.
11
+ #
12
+ # $ mkdir config
13
+ # $ mv Guardfile config/
14
+ # $ ln -s config/Guardfile .
15
+ #
16
+ # and, you'll have to watch "config/Guardfile" instead of "Guardfile"
17
+
18
+ guard :minitest do
19
+ # with Minitest::Unit
20
+ watch(%r{^test/(.*)\/?(.*)_test\.rb$})
21
+ watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[1]}#{m[2]}_test.rb" }
22
+ watch(%r{^lib/game_grid/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[1]}#{m[2]}_test.rb" }
23
+ watch(%r{^test/test_helper\.rb$}) { 'test' }
24
+
25
+ # with Minitest::Spec
26
+ # watch(%r{^spec/(.*)_spec\.rb$})
27
+ # watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
28
+ # watch(%r{^spec/spec_helper\.rb$}) { 'spec' }
29
+
30
+ # Rails 4
31
+ # watch(%r{^app/(.+)\.rb$}) { |m| "test/#{m[1]}_test.rb" }
32
+ # watch(%r{^app/controllers/application_controller\.rb$}) { 'test/controllers' }
33
+ # watch(%r{^app/controllers/(.+)_controller\.rb$}) { |m| "test/integration/#{m[1]}_test.rb" }
34
+ # watch(%r{^app/views/(.+)_mailer/.+}) { |m| "test/mailers/#{m[1]}_mailer_test.rb" }
35
+ # watch(%r{^lib/(.+)\.rb$}) { |m| "test/lib/#{m[1]}_test.rb" }
36
+ # watch(%r{^test/.+_test\.rb$})
37
+ # watch(%r{^test/test_helper\.rb$}) { 'test' }
38
+
39
+ # Rails < 4
40
+ # watch(%r{^app/controllers/(.*)\.rb$}) { |m| "test/functional/#{m[1]}_test.rb" }
41
+ # watch(%r{^app/helpers/(.*)\.rb$}) { |m| "test/helpers/#{m[1]}_test.rb" }
42
+ # watch(%r{^app/models/(.*)\.rb$}) { |m| "test/unit/#{m[1]}_test.rb" }
43
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Anthony Fuentes
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,37 @@
1
+
2
+ # TODO: Name gem
3
+
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ ```ruby
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install board
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Development
24
+
25
+ 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.
26
+
27
+ 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).
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/TODO:
32
+
33
+
34
+ ## License
35
+
36
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
37
+
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+
2
+ require "bundler/gem_tasks"
3
+ require "rake/testtask"
4
+
5
+ Rake::TestTask.new(:test) do |t|
6
+ t.libs << "test"
7
+ t.libs << "lib"
8
+ t.test_files = FileList['test/**/*_test.rb']
9
+ end
10
+
11
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "game_grid"
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/game_grid.gemspec ADDED
@@ -0,0 +1,38 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'game_grid/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "game_grid"
8
+ spec.version = GameGrid::VERSION
9
+ spec.authors = ["Anthony Fuentes"]
10
+ spec.email = ["anthony.c.fuentes@gmail.com"]
11
+
12
+ spec.summary = %q{A modular, game-based grid.}
13
+ spec.description = %q{A modular, game-based grid.}
14
+ spec.homepage = "https://github.com/anthonyfuentes/game_grid"
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.13"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "minitest", "~> 5.0"
36
+ spec.add_development_dependency "guard", "~> 2.14.0"
37
+ spec.add_development_dependency "guard-minitest", "~> 2.4.6"
38
+ end
data/lib/game_grid.rb ADDED
@@ -0,0 +1,8 @@
1
+
2
+ require 'game_grid/version'
3
+ require 'game_grid/cell'
4
+ require 'game_grid/coords'
5
+ require 'game_grid/grid'
6
+
7
+ module GameGrid
8
+ end
@@ -0,0 +1,18 @@
1
+
2
+ module GameGrid
3
+ class Cell
4
+ attr_accessor :value
5
+
6
+ def initialize(args = {})
7
+ @coordinates = args[:coords]
8
+ @value = args[:value]
9
+ end
10
+
11
+ def coords
12
+ [coordinates.x, coordinates.y]
13
+ end
14
+
15
+ private
16
+ attr_reader :coordinates
17
+ end
18
+ end
@@ -0,0 +1,11 @@
1
+
2
+ module GameGrid
3
+ class Coords
4
+ attr_reader :x, :y
5
+
6
+ def initialize(args = {})
7
+ @x = args[:x]
8
+ @y = args[:y]
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,211 @@
1
+
2
+ require_relative 'cell'
3
+ require_relative 'coords'
4
+
5
+ module GameGrid
6
+ class Grid
7
+ attr_reader :default, :height, :width
8
+
9
+ extend Forwardable
10
+ def_delegator :cells, :each
11
+
12
+ include Enumerable
13
+
14
+ def initialize(args = {})
15
+ @default = args[:default]
16
+ @height = args[:height] || 3
17
+ @width = args[:width] || 3
18
+ @grid = build(width, height)
19
+ populate if default
20
+ end
21
+
22
+ def cell(x, y)
23
+ select { |cell| cell.coords == [x, y] }[0]
24
+ end
25
+
26
+ def cells
27
+ columns.flatten
28
+ end
29
+
30
+ def clear
31
+ populate(nil)
32
+ end
33
+
34
+ def columns
35
+ grid
36
+ end
37
+
38
+ def diagonals
39
+ positive_diagonals + negative_diagonals
40
+ end
41
+
42
+ def positive_diagonals
43
+ diagonals = []
44
+
45
+ positive_diagonal_starting_points.each do |coordinate|
46
+ diagonals << get_positive_diagonal(coordinate)
47
+ end
48
+
49
+ diagonals
50
+ end
51
+
52
+ def negative_diagonals
53
+ diagonals = []
54
+
55
+ negative_diagonal_starting_points.each do |coordinate|
56
+ diagonals << get_negative_diagonal(coordinate)
57
+ end
58
+
59
+ diagonals
60
+ end
61
+
62
+ def neighbors(cell: nil, coords: nil)
63
+ raise ArgumentError unless cell || coords
64
+
65
+ coordinates = cell ? cell.coords : coords
66
+ neighbors_coordinates = neighbors_coordinates(coordinates)
67
+
68
+ cells.select do |neighbor|
69
+ neighbors_coordinates.include?(neighbor.coords)
70
+ end
71
+ end
72
+
73
+ def populate(new_value = default)
74
+ each do |cell|
75
+ begin
76
+ value = new_value.dup
77
+ rescue TypeError
78
+ value = new_value
79
+ ensure
80
+ cell.value = value
81
+ end
82
+ end
83
+ end
84
+
85
+ def rows
86
+ grid.transpose
87
+ end
88
+
89
+ def view
90
+ rows.map do |row|
91
+ stringify_row(row)
92
+ end.join("\n")
93
+ end
94
+
95
+ private
96
+ attr_reader :grid
97
+
98
+ def stringify_row(row)
99
+ (row.map { |cell| "|#{ cell.value || '-' }" } + ["|"]).join
100
+ end
101
+
102
+ def get_positive_diagonal(starting_coordinate)
103
+ diagonal = []
104
+
105
+ x_position = starting_coordinate.x
106
+ y_position = starting_coordinate.y
107
+
108
+ while y_position <= upper_limit
109
+ break if x_position > right_hand_limit
110
+ diagonal << cell(x_position, y_position)
111
+ x_position += 1
112
+ y_position += 1
113
+ end
114
+
115
+ diagonal
116
+ end
117
+
118
+ def positive_diagonal_starting_points
119
+ diagonal_starting_points = []
120
+
121
+ upper_limit.downto(lower_limit).each do |y|
122
+ diagonal_starting_points << Coords.new(x: left_hand_limit, y: y)
123
+ end
124
+
125
+ ((left_hand_limit + 1)..right_hand_limit).each do |x|
126
+ diagonal_starting_points << Coords.new(x: x, y: lower_limit)
127
+ end
128
+
129
+ diagonal_starting_points
130
+ end
131
+
132
+ def get_negative_diagonal(starting_coordinate)
133
+ diagonal = []
134
+
135
+ x_position = starting_coordinate.x
136
+ y_position = starting_coordinate.y - 1
137
+
138
+ while y_position >= lower_limit
139
+ break if x_position > right_hand_limit
140
+ diagonal << cell(x_position, y_position)
141
+ x_position += 1
142
+ y_position -= 1
143
+ end
144
+
145
+ diagonal
146
+ end
147
+
148
+ def negative_diagonal_starting_points
149
+ diagonal_starting_points = []
150
+
151
+ ((lower_limit + 1)..height).each do |y|
152
+ diagonal_starting_points << Coords.new(x: left_hand_limit, y: y)
153
+ end
154
+
155
+ ((left_hand_limit + 1)..right_hand_limit).each do |x|
156
+ diagonal_starting_points << Coords.new(x: x, y: height)
157
+ end
158
+
159
+ diagonal_starting_points
160
+ end
161
+
162
+ def build(width, height)
163
+ grid = Array.new(width) { Array.new }
164
+ cell_coordinates.each do |coords|
165
+ grid[coords.x] << GameGrid::Cell.new(coords: coords)
166
+ end
167
+ grid
168
+ end
169
+
170
+ def cell_coordinates
171
+ cell_coordinates = []
172
+ x_range.each do |x_position|
173
+ y_range.each do |y_position|
174
+ cell_coordinates << Coords.new(x: x_position, y: y_position)
175
+ end
176
+ end
177
+ cell_coordinates
178
+ end
179
+
180
+ def neighbors_coordinates(coordinates)
181
+ x, y = coordinates
182
+ [[(x - 1), (y - 1)], [(x - 1), y], [(x - 1), (y + 1)],
183
+ [x, (y + 1)], [(x + 1), (y + 1)], [(x + 1), y],
184
+ [(x + y), (y - 1)], [x, (y - 1)]]
185
+ end
186
+
187
+ def left_hand_limit
188
+ 0
189
+ end
190
+
191
+ def right_hand_limit
192
+ (width - 1)
193
+ end
194
+
195
+ def upper_limit
196
+ (height - 1)
197
+ end
198
+
199
+ def lower_limit
200
+ 0
201
+ end
202
+
203
+ def x_range
204
+ (left_hand_limit..right_hand_limit)
205
+ end
206
+
207
+ def y_range
208
+ (lower_limit..upper_limit)
209
+ end
210
+ end
211
+ end
@@ -0,0 +1,4 @@
1
+
2
+ module GameGrid
3
+ VERSION = "0.1.0"
4
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: game_grid
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Anthony Fuentes
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-11-06 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
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: guard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 2.14.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 2.14.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: guard-minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 2.4.6
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 2.4.6
83
+ description: A modular, game-based grid.
84
+ email:
85
+ - anthony.c.fuentes@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".travis.yml"
92
+ - Gemfile
93
+ - Guardfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - game_grid.gemspec
100
+ - lib/game_grid.rb
101
+ - lib/game_grid/cell.rb
102
+ - lib/game_grid/coords.rb
103
+ - lib/game_grid/grid.rb
104
+ - lib/game_grid/version.rb
105
+ homepage: https://github.com/anthonyfuentes/game_grid
106
+ licenses:
107
+ - MIT
108
+ metadata:
109
+ allowed_push_host: https://rubygems.org
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.5.1
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: A modular, game-based grid.
130
+ test_files: []