robojora 0.1.0

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
+ SHA256:
3
+ metadata.gz: 49bb8a26a341996273ea81d944a2de3f60c61c4958bb61cc4b8a66524021afcf
4
+ data.tar.gz: 8bb791fc706555bae991368918f7ebb85e9750a7da2da9836afd9b8c97e2c9d4
5
+ SHA512:
6
+ metadata.gz: 1defbf0a9b6f16d7b104bcf4b5a72c14a8eb1c4effd1eefe0f3407380dfef5f1ca1a752a6f071d93416ef47ccab2608bbdaaf15c8c9fc63d4eafca7fae62f9aa
7
+ data.tar.gz: 571930d287b3d186adc0474671ef5f68fe6e4bd04eccacf1aeadcdcba1cafec71100f9efe650f4cfd717a240a97d73a44acf78b827729b448498dc6bf3dbd1c1
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.3
7
+ before_install: gem install bundler -v 2.0.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in robojora.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,41 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ robojora (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ coderay (1.1.2)
10
+ diff-lcs (1.3)
11
+ method_source (0.9.2)
12
+ pry (0.12.2)
13
+ coderay (~> 1.1.0)
14
+ method_source (~> 0.9.0)
15
+ rake (10.5.0)
16
+ rspec (3.9.0)
17
+ rspec-core (~> 3.9.0)
18
+ rspec-expectations (~> 3.9.0)
19
+ rspec-mocks (~> 3.9.0)
20
+ rspec-core (3.9.0)
21
+ rspec-support (~> 3.9.0)
22
+ rspec-expectations (3.9.0)
23
+ diff-lcs (>= 1.2.0, < 2.0)
24
+ rspec-support (~> 3.9.0)
25
+ rspec-mocks (3.9.0)
26
+ diff-lcs (>= 1.2.0, < 2.0)
27
+ rspec-support (~> 3.9.0)
28
+ rspec-support (3.9.0)
29
+
30
+ PLATFORMS
31
+ ruby
32
+
33
+ DEPENDENCIES
34
+ bundler (~> 2.0)
35
+ pry (~> 0.12.2)
36
+ rake (~> 10.0)
37
+ robojora!
38
+ rspec (~> 3.0)
39
+
40
+ BUNDLED WITH
41
+ 2.0.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Enrique Salceda
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,39 @@
1
+ # Robojora
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/robojora`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'robojora'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install robojora
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/robojora.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "robojora"
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__)
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
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Robojora
4
+ class Board
5
+ MINIMUM_AXIS_VALUE = 0
6
+ MAXIMUM_AXIS_VALUE = 4
7
+
8
+ def initialize
9
+ @minimum_axis_value = MINIMUM_AXIS_VALUE
10
+ @maximum_axis_value = MAXIMUM_AXIS_VALUE
11
+ end
12
+
13
+ def build
14
+ grid_template.each_with_object([]) do |coordinates, array|
15
+ array << create_grid_square(coordinates)
16
+ end
17
+ end
18
+
19
+ def grid_template
20
+ axis_values = calculate_axis_values
21
+ axis_values.product(axis_values)
22
+ end
23
+
24
+ private
25
+
26
+ attr_reader :minimum_axis_value, :maximum_axis_value
27
+
28
+ def calculate_axis_values
29
+ (minimum_axis_value..maximum_axis_value).to_a
30
+ end
31
+
32
+ def create_grid_square(coordinates)
33
+ Robojora::Square.new(
34
+ coordinates: coordinates,
35
+ borders: Robojora::BorderClassifier.new(coordinates).assign_border_limit
36
+ )
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,39 @@
1
+ module Robojora
2
+ class BorderClassifier
3
+ MINIMUM_AXIS_VALUE = 0
4
+ MAXIMUM_AXIS_VALUE = 4
5
+
6
+ attr_reader :borders, :coordinates
7
+
8
+ def initialize(coordinates)
9
+ @borders = []
10
+ @coordinates = coordinates
11
+ end
12
+
13
+ def assign_border_limit
14
+ assign_to_north_border_if_north_coordinate
15
+ assign_to_west_border_if_west_coordinate
16
+ assign_to_south_border_if_south_coordinate
17
+ assign_to_east_border_if_east_coordinate
18
+ borders
19
+ end
20
+
21
+ private
22
+
23
+ def assign_to_north_border_if_north_coordinate
24
+ borders << 'north' if coordinates.last == MAXIMUM_AXIS_VALUE
25
+ end
26
+
27
+ def assign_to_west_border_if_west_coordinate
28
+ borders << 'west' if coordinates.first == MINIMUM_AXIS_VALUE
29
+ end
30
+
31
+ def assign_to_south_border_if_south_coordinate
32
+ borders << 'south' if coordinates.last == MINIMUM_AXIS_VALUE
33
+ end
34
+
35
+ def assign_to_east_border_if_east_coordinate
36
+ borders << 'east' if coordinates.first == MAXIMUM_AXIS_VALUE
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,15 @@
1
+ module Robojora
2
+ class CardinalDirections
3
+ CARDINAL_POINTS = %w[north west south east].freeze
4
+
5
+ def self.all
6
+ CARDINAL_POINTS
7
+ end
8
+
9
+ Robojora::CardinalDirections::CARDINAL_POINTS.each do |cardinal_point|
10
+ define_singleton_method(:"#{cardinal_point}") do
11
+ "#{state.upcase}"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ require 'robojora/movers/mover'
2
+
3
+ module Robojora
4
+ module Movers
5
+ class East < Robojora::Movers::Mover
6
+ def new_coordinates
7
+ [current_coordinates.first + 1, current_coordinates.last]
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,24 @@
1
+ module Robojora
2
+ module Movers
3
+ class Mover
4
+ attr_reader :current_square, :current_coordinates, :new_square, :robot
5
+
6
+ def initialize(robot)
7
+ @robot = robot
8
+ @current_coordinates = current_robot_coordinates
9
+ end
10
+
11
+ def move
12
+ robot.place(*new_coordinates, nil)
13
+ end
14
+
15
+ def new_coordinates; end
16
+
17
+ protected
18
+
19
+ def current_robot_coordinates
20
+ robot.select_square_by_name.coordinates
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,11 @@
1
+ require 'robojora/movers/mover'
2
+
3
+ module Robojora
4
+ module Movers
5
+ class North < Robojora::Movers::Mover
6
+ def new_coordinates
7
+ [current_coordinates.first, current_coordinates.last + 1]
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require 'robojora/movers/mover'
2
+
3
+ module Robojora
4
+ module Movers
5
+ class South < Robojora::Movers::Mover
6
+ def new_coordinates
7
+ [current_coordinates.first, current_coordinates.last - 1]
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require 'robojora/movers/mover'
2
+
3
+ module Robojora
4
+ module Movers
5
+ class West < Robojora::Movers::Mover
6
+ def new_coordinates
7
+ [current_coordinates.first - 1, current_coordinates.last]
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,74 @@
1
+ require 'pry'
2
+ module Robojora
3
+ class Robot
4
+ NOT_IN_BOARD = 'Robot is not on the board'.freeze
5
+
6
+ attr_reader :board, :name
7
+ attr_accessor :facing
8
+
9
+ def initialize(name)
10
+ @name = name
11
+ @board = board
12
+ end
13
+
14
+ def play_board(board)
15
+ @board = board
16
+ end
17
+
18
+ def place(x_coordinate, y_coordinate, facing)
19
+ square = select_square_by_coordinates(x_coordinate, y_coordinate)
20
+ robot_removing_and_placing(facing, square) if square
21
+ end
22
+
23
+ def report
24
+ select_square_by_name && facing ? location_report : NOT_IN_BOARD
25
+ end
26
+
27
+ def right
28
+ self.facing = rotator.right
29
+ end
30
+
31
+ def left
32
+ self.facing = rotator.left
33
+ end
34
+
35
+ def move
36
+ raise Robojora::RobotNotOnBoardError if select_square_by_name.nil?
37
+
38
+ mover.move
39
+ end
40
+
41
+ def select_square_by_name
42
+ board.select { |square| square.presence == name }.first
43
+ end
44
+
45
+ def select_square_by_coordinates(x_coordinate, y_coordinate)
46
+ board.select { |square| square.coordinates == [x_coordinate, y_coordinate] }.first
47
+ end
48
+
49
+ def robot_removing_and_placing(facing, square)
50
+ remove_robot_from_current_square
51
+ self.facing ||= facing
52
+ square.presence = name
53
+ end
54
+
55
+ private
56
+
57
+ def location_report
58
+ "#{select_square_by_name.coordinates.join(', ')}, #{facing}"
59
+ end
60
+
61
+ def remove_robot_from_current_square
62
+ current_square = select_square_by_name
63
+ current_square.presence = nil if current_square
64
+ end
65
+
66
+ def rotator
67
+ Robojora::Rotator.new(facing)
68
+ end
69
+
70
+ def mover
71
+ Robojora::Movers.const_get(facing.capitalize).new(self)
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,30 @@
1
+ module Robojora
2
+ class Rotator
3
+ CARDINAL_POINTS_FOR_LEFT = %w[north west south east north].freeze
4
+ CARDINAL_POINTS_FOR_RIGHT = CARDINAL_POINTS_FOR_LEFT.reverse.freeze
5
+
6
+ attr_reader :initial_direction
7
+
8
+ def initialize(initial_direction)
9
+ @initial_direction = initial_direction
10
+ end
11
+
12
+ def left
13
+ CARDINAL_POINTS_FOR_LEFT.fetch(left_desired_cardinal_direction_index)
14
+ end
15
+
16
+ def right
17
+ CARDINAL_POINTS_FOR_RIGHT.fetch(right_desired_cardinal_direction_index)
18
+ end
19
+
20
+ private
21
+
22
+ def left_desired_cardinal_direction_index
23
+ CARDINAL_POINTS_FOR_LEFT.index(initial_direction) + 1
24
+ end
25
+
26
+ def right_desired_cardinal_direction_index
27
+ CARDINAL_POINTS_FOR_RIGHT.index(initial_direction) + 1
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,12 @@
1
+ module Robojora
2
+ class Square
3
+ attr_reader :coordinates, :borders
4
+ attr_accessor :presence
5
+
6
+ def initialize(borders:, coordinates:)
7
+ @borders = borders
8
+ @coordinates = coordinates
9
+ @presence = nil
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module Robojora
2
+ VERSION = '0.1.0'.freeze
3
+ end
data/lib/robojora.rb ADDED
@@ -0,0 +1,16 @@
1
+ require 'robojora/version'
2
+ require 'robojora/board'
3
+ require 'robojora/border_classifier'
4
+ require 'robojora/square'
5
+ require 'robojora/robot'
6
+ require 'robojora/rotator'
7
+ require 'robojora/cardinal_directions'
8
+ require 'robojora/movers/mover'
9
+ require 'robojora/movers/north'
10
+ require 'robojora/movers/south'
11
+ require 'robojora/movers/west'
12
+ require 'robojora/movers/east'
13
+
14
+ module Robojora
15
+ class RobotNotOnBoardError < StandardError; end
16
+ end
data/robojora.gemspec ADDED
@@ -0,0 +1,33 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'robojora/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'robojora'
7
+ spec.version = Robojora::VERSION
8
+ spec.authors = ['Enrique Salceda']
9
+ spec.email = 'enriquesalceda@gmail.com'
10
+
11
+ spec.summary = 'Jora Assignment - Robot game'
12
+ spec.description = 'Robot'
13
+ spec.homepage = 'https://github.com/enriquesalceda/robojora'
14
+ spec.license = 'MIT'
15
+
16
+ spec.metadata['homepage_uri'] = spec.homepage
17
+ spec.metadata['source_code_uri'] = 'https://github.com/enriquesalceda/robojora'
18
+ spec.metadata['changelog_uri'] = 'https://github.com/enriquesalceda/robojora'
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ end
25
+ spec.bindir = 'exe'
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ['lib']
28
+
29
+ spec.add_development_dependency 'bundler', '~> 2.0'
30
+ spec.add_development_dependency 'rake', '~> 10.0'
31
+ spec.add_development_dependency 'pry', '~> 0.12.2'
32
+ spec.add_development_dependency 'rspec', '~> 3.0'
33
+ end
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: robojora
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Enrique Salceda
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-12-01 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: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
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: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.12.2
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.12.2
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: Robot
70
+ email: enriquesalceda@gmail.com
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - ".gitignore"
76
+ - ".rspec"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - Gemfile.lock
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - lib/robojora.rb
86
+ - lib/robojora/board.rb
87
+ - lib/robojora/border_classifier.rb
88
+ - lib/robojora/cardinal_directions.rb
89
+ - lib/robojora/movers/east.rb
90
+ - lib/robojora/movers/mover.rb
91
+ - lib/robojora/movers/north.rb
92
+ - lib/robojora/movers/south.rb
93
+ - lib/robojora/movers/west.rb
94
+ - lib/robojora/robot.rb
95
+ - lib/robojora/rotator.rb
96
+ - lib/robojora/square.rb
97
+ - lib/robojora/version.rb
98
+ - robojora.gemspec
99
+ homepage: https://github.com/enriquesalceda/robojora
100
+ licenses:
101
+ - MIT
102
+ metadata:
103
+ homepage_uri: https://github.com/enriquesalceda/robojora
104
+ source_code_uri: https://github.com/enriquesalceda/robojora
105
+ changelog_uri: https://github.com/enriquesalceda/robojora
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.7.6
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: Jora Assignment - Robot game
126
+ test_files: []