robot_toy_jonhnes 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: 88d0dadccc5be9a917eedaeb07eebd7e739cdd43768188a441ce3f7a774df22a
4
+ data.tar.gz: 34391ff81a3ddbc558025c8708cc45e6eb98fa1d5b9e2c82fb2f60e6193f99e5
5
+ SHA512:
6
+ metadata.gz: 33c9bc4fae79d5e5431d294ff6930a200d8db246dd2b69719bdde91477648fa8257c2cb0a7667972c37873179e742bd0236c618d53eddca7efdbf4a40a80e421
7
+ data.tar.gz: 309fee1b3ae5b00acf6f13c1e933f2d17d3039f8225bc1731f19eddf6a15cda49b503a98122e422e1ce9693bc4c1b40b00d1cd2c29799ae18716b4294624f7e2
@@ -0,0 +1,8 @@
1
+ require "robot_toy_jonhnes/version"
2
+ require "robot_toy_jonhnes/game"
3
+ require "robot_toy_jonhnes/position_validator"
4
+ require "robot_toy_jonhnes/robot_mover"
5
+
6
+ module RobotToyJonhnes
7
+ class Error < StandardError; end
8
+ end
@@ -0,0 +1,70 @@
1
+ module RobotToyJonhnes
2
+ class Game
3
+ CARDINAL_POINTS = %i[NORTH EAST SOUTH WEST].freeze
4
+ attr_accessor :position_line, :position_column, :robot_height, :robot_width,
5
+ :facing, :command
6
+
7
+ def initialize
8
+ @position_line = @position_column = 0
9
+ @robot_height = @robot_width = 1
10
+ @facing = :EAST
11
+ end
12
+
13
+ def board_height
14
+ @robot_height * 5
15
+ end
16
+
17
+ def board_width
18
+ @robot_width * 5
19
+ end
20
+
21
+ def place(new_place)
22
+ new_line, new_column, new_orientation = new_place.split(',')
23
+ RobotMover.new(self).to_local new_line, new_column, new_orientation
24
+ end
25
+
26
+ def report
27
+ board_print = "\n"
28
+ board_height.times { |index| board_print += game_line(index) + "\n" }
29
+ limit_line + board_print + limit_line
30
+ end
31
+
32
+ def rotate_left
33
+ facing_index = CARDINAL_POINTS.find_index @facing
34
+ @facing = CARDINAL_POINTS[facing_index - 1]
35
+ end
36
+
37
+ def rotate_right
38
+ facing_index = CARDINAL_POINTS.find_index @facing
39
+ @facing = CARDINAL_POINTS[facing_index + 1] || CARDINAL_POINTS[0]
40
+ end
41
+
42
+ def move
43
+ movement_options = {NORTH: :to_north, SOUTH: :to_south,
44
+ WEST: :to_west, EAST: :to_east}
45
+ RobotMover.new(self).send movement_options[@facing]
46
+ end
47
+
48
+ private
49
+
50
+ def limit_line
51
+ '#' * (board_width + 2)
52
+ end
53
+
54
+ def robot_in_line?(line)
55
+ last_line_position = robot_width + position_line - 1
56
+ (position_line..last_line_position).to_a.include? line
57
+ end
58
+
59
+ def robot_column_indexes
60
+ last_column_position = position_column + robot_width - 1
61
+ position_column..last_column_position
62
+ end
63
+
64
+ def game_line(line)
65
+ line_draw = '-' * board_width
66
+ line_draw[robot_column_indexes] = facing[0] if robot_in_line? line
67
+ '#' + line_draw + '#'
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,17 @@
1
+ module RobotToyJonhnes
2
+ class PositionValidator
3
+ def self.new_position_valid?(new_position, board_limit:)
4
+ hit_top = new_position.to_i.negative?
5
+ hit_bottom = new_position.to_i >= board_limit
6
+ !hit_top && !hit_bottom
7
+ end
8
+
9
+ def self.new_place_valid?(new_line, new_column, orientation, game)
10
+ line_valid = new_position_valid? new_line, board_limit: game.board_width
11
+ column_valid = new_position_valid? new_column, board_limit: game.board_height
12
+ face_valid = Game::CARDINAL_POINTS.include? orientation&.upcase&.to_sym
13
+
14
+ line_valid && column_valid && face_valid
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,59 @@
1
+ module RobotToyJonhnes
2
+ class RobotMover
3
+ attr :game
4
+
5
+ def initialize(game)
6
+ @game = game
7
+ end
8
+
9
+ def to_north
10
+ new_line_position = game.position_line - 1
11
+ update_line_position new_line_position
12
+ end
13
+
14
+ def to_south
15
+ new_line_position = game.position_line + 1
16
+ update_line_position new_line_position
17
+ end
18
+
19
+ def to_west
20
+ new_column_position = game.position_column - 1
21
+ update_column_position new_column_position
22
+ end
23
+
24
+ def to_east
25
+ new_column_position = game.position_column + 1
26
+ update_column_position new_column_position
27
+ end
28
+
29
+ def to_local(new_line, new_column, new_orientation)
30
+ can_save = PositionValidator.new_place_valid? new_line, new_column,
31
+ new_orientation, game
32
+ raise StandardError, 'Invalid place' unless can_save
33
+
34
+ game.position_line = new_line.to_i
35
+ game.position_column = new_column.to_i
36
+ game.facing = new_orientation.upcase.to_sym
37
+ end
38
+
39
+ private
40
+
41
+ def update_line_position(new_position)
42
+ can_save = PositionValidator.new_position_valid?(
43
+ new_position, board_limit: game.board_height
44
+ )
45
+ raise StandardError, 'You reached the border' unless can_save
46
+
47
+ game.position_line = new_position.to_i
48
+ end
49
+
50
+ def update_column_position(new_position)
51
+ can_save = PositionValidator.new_position_valid?(
52
+ new_position, board_limit: game.board_width
53
+ )
54
+ raise StandardError, 'You reached the border' unless can_save
55
+
56
+ game.position_column = new_position.to_i
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,3 @@
1
+ module RobotToyJonhnes
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: robot_toy_jonhnes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jonhnes Lopes
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-04-13 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.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
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
+ description: Core of robot toy app
56
+ email:
57
+ - jonhnes@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - lib/robot_toy_jonhnes.rb
63
+ - lib/robot_toy_jonhnes/game.rb
64
+ - lib/robot_toy_jonhnes/position_validator.rb
65
+ - lib/robot_toy_jonhnes/robot_mover.rb
66
+ - lib/robot_toy_jonhnes/version.rb
67
+ homepage: https://bitbucket.org/jonhnes/robot_toy_jonhnes
68
+ licenses:
69
+ - MIT
70
+ metadata:
71
+ homepage_uri: https://bitbucket.org/jonhnes/robot_toy_jonhnes
72
+ source_code_uri: https://bitbucket.org/jonhnes/robot_toy_jonhnes
73
+ changelog_uri: https://bitbucket.org/jonhnes/robot_toy_jonhnes
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubygems_version: 3.0.6
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: Gem for robot toy project
93
+ test_files: []