pitchcar 0.6.3 → 0.7.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 +4 -4
- data/Gemfile.lock +1 -1
- data/lib/{images → pieces/images}/curve_tile.png +0 -0
- data/lib/{images → pieces/images}/start_tile.png +0 -0
- data/lib/{images → pieces/images}/straight_tile.png +0 -0
- data/lib/pieces/left.rb +47 -0
- data/lib/pieces/piece.rb +62 -0
- data/lib/pieces/right.rb +47 -0
- data/lib/pieces/start.rb +23 -0
- data/lib/pieces/straight.rb +31 -0
- data/lib/pieces/straight_left_wall.rb +21 -0
- data/lib/pieces/straight_right_wall.rb +21 -0
- data/lib/pitchcar.rb +16 -6
- data/lib/track.rb +28 -58
- data/pitchcar.gemspec +1 -1
- metadata +12 -6
- data/lib/piece.rb +0 -126
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6e5d25f33141f96aecd5559af9d9e4ed55fe443e
|
4
|
+
data.tar.gz: 65ada2bad29c4c1caa7ec5fb09216c1abc6e2fdf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 86b6cfa6f33c97bec657a9d73ef6d4c419a0acea6bb6adbdcd8953859f64425251c786edd9b2b95067aaf60584656c8502009cb46261b13278b9980c4a415e67
|
7
|
+
data.tar.gz: 6630c665a33b9c7e9c0e6b5b1978254f1eb8667157e40fe4f24249accc67b7afc4df8b12af8d360e22de2a4713e366c227526f44c3163fb3a7a21d9250a094fc
|
data/Gemfile.lock
CHANGED
File without changes
|
File without changes
|
File without changes
|
data/lib/pieces/left.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
module Pitchcar
|
2
|
+
module Pieces
|
3
|
+
class Left < Piece
|
4
|
+
IMAGE = Magick::Image.read(File.expand_path('../images/curve_tile.png', __FILE__))[0]
|
5
|
+
|
6
|
+
def next_direction
|
7
|
+
if north?
|
8
|
+
DIRECTIONS[:WEST]
|
9
|
+
elsif east?
|
10
|
+
DIRECTIONS[:NORTH]
|
11
|
+
elsif west?
|
12
|
+
DIRECTIONS[:SOUTH]
|
13
|
+
elsif south?
|
14
|
+
DIRECTIONS[:EAST]
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def next_coordinate
|
19
|
+
if north?
|
20
|
+
{ x: x - 1, y: y }
|
21
|
+
elsif east?
|
22
|
+
{ x: x, y: y + 1 }
|
23
|
+
elsif west?
|
24
|
+
{ x: x, y: y - 1 }
|
25
|
+
elsif south?
|
26
|
+
{ x: x + 1, y: y }
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def image
|
31
|
+
if north?
|
32
|
+
IMAGE.rotate(180)
|
33
|
+
elsif east?
|
34
|
+
IMAGE.rotate(270)
|
35
|
+
elsif west?
|
36
|
+
IMAGE.rotate(90)
|
37
|
+
elsif south?
|
38
|
+
IMAGE
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def to_s
|
43
|
+
'L'
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/lib/pieces/piece.rb
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'rmagick'
|
3
|
+
|
4
|
+
module Pitchcar
|
5
|
+
module Pieces
|
6
|
+
class Piece
|
7
|
+
DIRECTIONS = { NORTH: 0, EAST: 1, WEST: 2, SOUTH: 3 }
|
8
|
+
attr_accessor :direction, :x, :y, :type
|
9
|
+
|
10
|
+
def initialize(properties)
|
11
|
+
self.x = properties[:x]
|
12
|
+
self.y = properties[:y]
|
13
|
+
self.direction = properties[:direction]
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.first_from_string(piece_string)
|
17
|
+
Pieces::Piece.type_from_string(piece_string).new(x: 0, y: 0, direction: DIRECTIONS[:SOUTH])
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.type_from_string(string)
|
21
|
+
case string
|
22
|
+
when 'S'
|
23
|
+
Straight
|
24
|
+
when 'L'
|
25
|
+
Left
|
26
|
+
when 'R'
|
27
|
+
Right
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def to_h
|
32
|
+
{ x: x, y: y, type: name, direction_name: DIRECTIONS.key(direction).downcase, direction: direction }
|
33
|
+
end
|
34
|
+
|
35
|
+
def name
|
36
|
+
self.class.name.split('::').last
|
37
|
+
end
|
38
|
+
|
39
|
+
def coordinate
|
40
|
+
{ x: x, y: y }
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def north?
|
46
|
+
direction == DIRECTIONS[:NORTH]
|
47
|
+
end
|
48
|
+
|
49
|
+
def south?
|
50
|
+
direction == DIRECTIONS[:SOUTH]
|
51
|
+
end
|
52
|
+
|
53
|
+
def west?
|
54
|
+
direction == DIRECTIONS[:WEST]
|
55
|
+
end
|
56
|
+
|
57
|
+
def east?
|
58
|
+
direction == DIRECTIONS[:EAST]
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
data/lib/pieces/right.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
module Pitchcar
|
2
|
+
module Pieces
|
3
|
+
class Right < Piece
|
4
|
+
IMAGE = Magick::Image.read(File.expand_path('../images/curve_tile.png', __FILE__))[0]
|
5
|
+
|
6
|
+
def next_direction
|
7
|
+
if north?
|
8
|
+
DIRECTIONS[:EAST]
|
9
|
+
elsif east?
|
10
|
+
DIRECTIONS[:SOUTH]
|
11
|
+
elsif west?
|
12
|
+
DIRECTIONS[:NORTH]
|
13
|
+
elsif south?
|
14
|
+
DIRECTIONS[:WEST]
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def next_coordinate
|
19
|
+
if north?
|
20
|
+
{ x: x + 1, y: y }
|
21
|
+
elsif east?
|
22
|
+
{ x: x, y: y - 1 }
|
23
|
+
elsif west?
|
24
|
+
{ x: x, y: y + 1 }
|
25
|
+
elsif south?
|
26
|
+
{ x: x - 1, y: y }
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def image
|
31
|
+
if north?
|
32
|
+
IMAGE.rotate(90)
|
33
|
+
elsif east?
|
34
|
+
IMAGE.rotate(180)
|
35
|
+
elsif west?
|
36
|
+
IMAGE
|
37
|
+
elsif south?
|
38
|
+
IMAGE.rotate(270)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def to_s
|
43
|
+
'R'
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/lib/pieces/start.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
module Pitchcar
|
2
|
+
module Pieces
|
3
|
+
class Start < Straight
|
4
|
+
IMAGE = Magick::Image.read(File.expand_path('../images/start_tile.png', __FILE__))[0]
|
5
|
+
|
6
|
+
def image
|
7
|
+
if north?
|
8
|
+
IMAGE.rotate(270)
|
9
|
+
elsif east?
|
10
|
+
IMAGE
|
11
|
+
elsif west?
|
12
|
+
IMAGE.rotate(180)
|
13
|
+
elsif south?
|
14
|
+
IMAGE.rotate(90)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_s
|
19
|
+
'St'
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Pitchcar
|
2
|
+
module Pieces
|
3
|
+
class Straight < Piece
|
4
|
+
IMAGE = Magick::Image.read(File.expand_path('../images/straight_tile.png', __FILE__))[0]
|
5
|
+
def next_direction
|
6
|
+
direction
|
7
|
+
end
|
8
|
+
|
9
|
+
def next_coordinate
|
10
|
+
if north?
|
11
|
+
{ x: x, y: y + 1 }
|
12
|
+
elsif east?
|
13
|
+
{ x: x + 1, y: y }
|
14
|
+
elsif west?
|
15
|
+
{ x: x - 1, y: y }
|
16
|
+
elsif south?
|
17
|
+
{ x: x, y: y - 1 }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def image
|
22
|
+
return IMAGE.rotate(90) if north? || south?
|
23
|
+
IMAGE
|
24
|
+
end
|
25
|
+
|
26
|
+
def to_s
|
27
|
+
'S'
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Pitchcar
|
2
|
+
module Pieces
|
3
|
+
class StraightLeftWall < Straight
|
4
|
+
def image
|
5
|
+
if north?
|
6
|
+
IMAGE.rotate(90)
|
7
|
+
elsif east?
|
8
|
+
IMAGE.rotate(180)
|
9
|
+
elsif west?
|
10
|
+
IMAGE
|
11
|
+
elsif south?
|
12
|
+
IMAGE.rotate(270)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_s
|
17
|
+
'Slw'
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Pitchcar
|
2
|
+
module Pieces
|
3
|
+
class StraightRightWall < Straight
|
4
|
+
def image
|
5
|
+
if south?
|
6
|
+
IMAGE.rotate(90)
|
7
|
+
elsif west?
|
8
|
+
IMAGE.rotate(180)
|
9
|
+
elsif east?
|
10
|
+
IMAGE
|
11
|
+
elsif north?
|
12
|
+
IMAGE.rotate(270)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_s
|
17
|
+
'Srw'
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/pitchcar.rb
CHANGED
@@ -1,12 +1,20 @@
|
|
1
|
-
require_relative 'piece'
|
1
|
+
require_relative 'pieces/piece'
|
2
|
+
require_relative 'pieces/straight'
|
3
|
+
require_relative 'pieces/start'
|
4
|
+
require_relative 'pieces/straight_right_wall'
|
5
|
+
require_relative 'pieces/straight_left_wall'
|
6
|
+
require_relative 'pieces/left'
|
7
|
+
require_relative 'pieces/right'
|
2
8
|
require_relative 'track'
|
3
9
|
|
10
|
+
Thread.abort_on_exception = true
|
11
|
+
|
4
12
|
module Pitchcar
|
5
13
|
class Pitchcar
|
6
14
|
|
7
15
|
class << self
|
8
16
|
def find_all_tracks(straight, left_right)
|
9
|
-
tracks = find_tracks(straight
|
17
|
+
tracks = find_tracks(straight, left_right, '', [])
|
10
18
|
tracks.map(&:with_wall_combinations).flatten
|
11
19
|
end
|
12
20
|
|
@@ -39,10 +47,12 @@ module Pitchcar
|
|
39
47
|
|
40
48
|
def find_tracks(straight, left_right, track_pieces, tracks)
|
41
49
|
print "Found #{tracks.size} tracks\r"
|
42
|
-
|
43
|
-
|
50
|
+
unless track_pieces.empty?
|
51
|
+
track = Track.build_from(track_pieces)
|
52
|
+
return false if track.overlaps?
|
53
|
+
return tracks << track if straight == 0 && left_right == 0 && track.valid?(tracks)
|
54
|
+
end
|
44
55
|
|
45
|
-
return tracks << track if straight == 0 && left_right == 0 && track.valid?(tracks)
|
46
56
|
[track_pieces].product((['S'] * straight + ['L'] * left_right + ['R'] * left_right).uniq).each do |result|
|
47
57
|
if result[1] == 'S'
|
48
58
|
find_tracks(straight - 1, left_right, result.join, tracks)
|
@@ -56,7 +66,7 @@ module Pitchcar
|
|
56
66
|
def random_track(straight, left_right, size_restrictions = {})
|
57
67
|
left = Random.rand(1..left_right)
|
58
68
|
right = left_right - left
|
59
|
-
Track.build_from("
|
69
|
+
Track.build_from("#{'S' * straight}#{'L' * left}#{'R' * right}".split('').shuffle.join, size_restrictions)
|
60
70
|
end
|
61
71
|
end
|
62
72
|
end
|
data/lib/track.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'csv'
|
2
2
|
require 'bazaar'
|
3
|
-
|
3
|
+
require 'digest'
|
4
|
+
require_relative 'pieces/piece'
|
4
5
|
require_relative 'track_image'
|
5
6
|
require_relative 'boyermoore'
|
6
7
|
|
@@ -15,30 +16,11 @@ module Pitchcar
|
|
15
16
|
end
|
16
17
|
|
17
18
|
def self.build_from(track_pieces, size_restrictions = {})
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
piece = Piece.new
|
22
|
-
piece.x = pieces.last.x
|
23
|
-
piece.y = pieces.last.y
|
24
|
-
piece.type = Piece.type_from_string(piece_str)
|
25
|
-
|
26
|
-
case pieces.last.direction
|
27
|
-
when Piece::DIRECTIONS[:NORTH]
|
28
|
-
piece.y = pieces.last.y + 1
|
29
|
-
when Piece::DIRECTIONS[:EAST]
|
30
|
-
piece.x = pieces.last.x + 1
|
31
|
-
when Piece::DIRECTIONS[:WEST]
|
32
|
-
piece.x = pieces.last.x - 1
|
33
|
-
when Piece::DIRECTIONS[:SOUTH]
|
34
|
-
piece.y = pieces.last.y - 1
|
35
|
-
end
|
36
|
-
piece.direction = piece.next_direction(pieces.last.direction)
|
37
|
-
|
38
|
-
pieces << piece
|
19
|
+
track = Track.new([Pieces::Piece.first_from_string(track_pieces.chars.first)], size_restrictions)
|
20
|
+
track_pieces.chars[1..-1].each do |piece_str|
|
21
|
+
track.pieces << Pieces::Piece.type_from_string(piece_str).new(track.pieces.last.next_coordinate.merge(direction: track.pieces.last.next_direction))
|
39
22
|
end
|
40
|
-
|
41
|
-
Track.new(pieces, size_restrictions)
|
23
|
+
track
|
42
24
|
end
|
43
25
|
|
44
26
|
def valid?(tracks=[])
|
@@ -46,15 +28,15 @@ module Pitchcar
|
|
46
28
|
end
|
47
29
|
|
48
30
|
def to_s
|
49
|
-
pieces.map(&:to_s).join(' ')
|
31
|
+
assign_start_piece.pieces.map(&:to_s).join(' ')
|
50
32
|
end
|
51
33
|
|
52
34
|
def to_json
|
53
|
-
pieces.map(&:to_h).to_json
|
35
|
+
assign_start_piece.pieces.map(&:to_h).to_json
|
54
36
|
end
|
55
37
|
|
56
38
|
def to_png
|
57
|
-
TrackImage.new(
|
39
|
+
TrackImage.new(assign_start_piece).render
|
58
40
|
puts "Track image saved to #{Dir.pwd}/track.png"
|
59
41
|
end
|
60
42
|
|
@@ -70,26 +52,18 @@ module Pitchcar
|
|
70
52
|
@overlaps ||= string.include?('LLL') || string.include?('RRR') || pieces.group_by { |piece| [piece.x, piece.y] }.values.any? { |set| set.size > 1 }
|
71
53
|
end
|
72
54
|
|
73
|
-
def with_wall_combinations(pieces =
|
74
|
-
straight_index = pieces.find_index { |piece| piece.
|
55
|
+
def with_wall_combinations(pieces = self.pieces, tracks = [])
|
56
|
+
straight_index = pieces.find_index { |piece| piece.instance_of? Pieces::Straight }
|
75
57
|
if straight_index
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
pieces_copy[straight_index]
|
80
|
-
|
81
|
-
end
|
82
|
-
combos
|
58
|
+
pieces_copy = pieces.dup
|
59
|
+
|
60
|
+
[Pieces::StraightLeftWall, Pieces::StraightRightWall].map do |piece_type|
|
61
|
+
pieces_copy[straight_index] = piece_type.new(pieces[straight_index].to_h)
|
62
|
+
with_wall_combinations(pieces_copy, tracks)
|
63
|
+
end.last
|
83
64
|
else
|
84
|
-
|
85
|
-
track.pieces.first.type = Piece::TYPES[:STRAIGHT_RIGHT_WALL]
|
86
|
-
random_right_wall = track.pieces.each_index.select do |index|
|
87
|
-
track.pieces[index].type == Piece::TYPES[:STRAIGHT_RIGHT_WALL]
|
88
|
-
end.sample
|
89
|
-
track.pieces[random_right_wall].type = Piece::TYPES[:STRAIGHT_START]
|
90
|
-
combinations << track
|
65
|
+
tracks << Track.new(pieces.dup)
|
91
66
|
end
|
92
|
-
return combinations
|
93
67
|
end
|
94
68
|
|
95
69
|
# Returns pieces list sorted from in a top-bottom left-right manner
|
@@ -115,6 +89,14 @@ module Pitchcar
|
|
115
89
|
"#{results[:adjs].sample} #{results[:nouns].sample}"
|
116
90
|
end
|
117
91
|
|
92
|
+
def assign_start_piece
|
93
|
+
start_index = pieces.each_index.select { |i| pieces[i].is_a? Pieces::StraightRightWall }.sample
|
94
|
+
# If there are no straight right pieces, pick any straight piece to be the start
|
95
|
+
start_index = pieces.each_index.select { |i| pieces[i].is_a? Pieces::Straight }.sample if start_index.nil?
|
96
|
+
self.pieces[start_index] = Pieces::Start.new(pieces[start_index].to_h) unless start_index.nil?
|
97
|
+
self
|
98
|
+
end
|
99
|
+
|
118
100
|
private
|
119
101
|
|
120
102
|
def rotation_exists?(tracks)
|
@@ -126,7 +108,8 @@ module Pitchcar
|
|
126
108
|
end
|
127
109
|
|
128
110
|
def ends_correctly?
|
129
|
-
|
111
|
+
return false if pieces.length < 2
|
112
|
+
pieces.last.next_coordinate == pieces.first.coordinate && pieces.last.next_direction == Pieces::Piece::DIRECTIONS[:SOUTH]
|
130
113
|
end
|
131
114
|
|
132
115
|
def within_size_restrictions?
|
@@ -137,17 +120,4 @@ module Pitchcar
|
|
137
120
|
Digest::SHA256.hexdigest(to_s)
|
138
121
|
end
|
139
122
|
end
|
140
|
-
|
141
|
-
class PieceList < Array
|
142
|
-
def initialize(pieces)
|
143
|
-
pieces.each do |piece|
|
144
|
-
new_piece = Piece.new
|
145
|
-
new_piece.x = piece.x
|
146
|
-
new_piece.y = piece.y
|
147
|
-
new_piece.type = piece.type
|
148
|
-
new_piece.direction = piece.direction
|
149
|
-
self.<< new_piece
|
150
|
-
end
|
151
|
-
end
|
152
|
-
end
|
153
123
|
end
|
data/pitchcar.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pitchcar
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonah Hirsch
|
@@ -49,10 +49,16 @@ files:
|
|
49
49
|
- LICENSE.md
|
50
50
|
- Readme.md
|
51
51
|
- lib/boyermoore.rb
|
52
|
-
- lib/images/curve_tile.png
|
53
|
-
- lib/images/start_tile.png
|
54
|
-
- lib/images/straight_tile.png
|
55
|
-
- lib/
|
52
|
+
- lib/pieces/images/curve_tile.png
|
53
|
+
- lib/pieces/images/start_tile.png
|
54
|
+
- lib/pieces/images/straight_tile.png
|
55
|
+
- lib/pieces/left.rb
|
56
|
+
- lib/pieces/piece.rb
|
57
|
+
- lib/pieces/right.rb
|
58
|
+
- lib/pieces/start.rb
|
59
|
+
- lib/pieces/straight.rb
|
60
|
+
- lib/pieces/straight_left_wall.rb
|
61
|
+
- lib/pieces/straight_right_wall.rb
|
56
62
|
- lib/pitchcar.rb
|
57
63
|
- lib/track.rb
|
58
64
|
- lib/track_image.rb
|
@@ -78,7 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
78
84
|
version: '0'
|
79
85
|
requirements: []
|
80
86
|
rubyforge_project:
|
81
|
-
rubygems_version: 2.
|
87
|
+
rubygems_version: 2.6.3
|
82
88
|
signing_key:
|
83
89
|
specification_version: 4
|
84
90
|
summary: Pitchcar Track Generator
|
data/lib/piece.rb
DELETED
@@ -1,126 +0,0 @@
|
|
1
|
-
require 'json'
|
2
|
-
require 'rmagick'
|
3
|
-
|
4
|
-
module Pitchcar
|
5
|
-
class Piece
|
6
|
-
TYPES = { STRAIGHT: 0, LEFT: 1, RIGHT: 2, STRAIGHT_LEFT_WALL: 3, STRAIGHT_RIGHT_WALL: 4, STRAIGHT_START: 5 }
|
7
|
-
DIRECTIONS = { NORTH: 0, EAST: 1, WEST: 2, SOUTH: 3 }
|
8
|
-
STRAIGHT_IMAGE, CURVE_IMAGE, START_IMAGE = Magick::ImageList.new(File.expand_path('../images/straight_tile.png', __FILE__),
|
9
|
-
File.expand_path('../images/curve_tile.png', __FILE__),
|
10
|
-
File.expand_path('../images/start_tile.png', __FILE__)).to_a
|
11
|
-
attr_accessor :direction, :x, :y, :type
|
12
|
-
|
13
|
-
def self.starting_piece
|
14
|
-
piece = new
|
15
|
-
piece.x = 0
|
16
|
-
piece.y = 0
|
17
|
-
piece.type = TYPES[:STRAIGHT]
|
18
|
-
piece.direction = DIRECTIONS[:SOUTH]
|
19
|
-
piece
|
20
|
-
end
|
21
|
-
|
22
|
-
def self.type_from_string(string)
|
23
|
-
case string
|
24
|
-
when 'S'
|
25
|
-
TYPES[:STRAIGHT]
|
26
|
-
when 'L'
|
27
|
-
TYPES[:LEFT]
|
28
|
-
when 'R'
|
29
|
-
TYPES[:RIGHT]
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
def next_direction(from)
|
34
|
-
case type
|
35
|
-
when TYPES[:LEFT]
|
36
|
-
case from
|
37
|
-
when DIRECTIONS[:NORTH]
|
38
|
-
DIRECTIONS[:WEST]
|
39
|
-
when DIRECTIONS[:EAST]
|
40
|
-
DIRECTIONS[:NORTH]
|
41
|
-
when DIRECTIONS[:WEST]
|
42
|
-
DIRECTIONS[:SOUTH]
|
43
|
-
when DIRECTIONS[:SOUTH]
|
44
|
-
DIRECTIONS[:EAST]
|
45
|
-
end
|
46
|
-
when TYPES[:RIGHT]
|
47
|
-
case from
|
48
|
-
when DIRECTIONS[:NORTH]
|
49
|
-
DIRECTIONS[:EAST]
|
50
|
-
when DIRECTIONS[:EAST]
|
51
|
-
DIRECTIONS[:SOUTH]
|
52
|
-
when DIRECTIONS[:WEST]
|
53
|
-
DIRECTIONS[:NORTH]
|
54
|
-
when DIRECTIONS[:SOUTH]
|
55
|
-
DIRECTIONS[:WEST]
|
56
|
-
end
|
57
|
-
else
|
58
|
-
from
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
def to_s
|
63
|
-
TYPES.key(type).to_s.split('_').map { |word| word[0] }.join.downcase.capitalize
|
64
|
-
end
|
65
|
-
|
66
|
-
def to_h
|
67
|
-
{ x: x, y: y, type: TYPES.key(type).downcase, direction: DIRECTIONS.key(direction).downcase }
|
68
|
-
end
|
69
|
-
|
70
|
-
# Slw N = Srw S
|
71
|
-
# Slw E = Srw W
|
72
|
-
# Slw W == Srw E
|
73
|
-
# Slw S = Srw N
|
74
|
-
|
75
|
-
def image
|
76
|
-
if (type == TYPES[:STRAIGHT_LEFT_WALL] && direction == DIRECTIONS[:NORTH]) || (type == TYPES[:STRAIGHT_RIGHT_WALL] && direction == DIRECTIONS[:SOUTH])
|
77
|
-
return STRAIGHT_IMAGE.rotate(90)
|
78
|
-
elsif (type == TYPES[:STRAIGHT_LEFT_WALL] && direction == DIRECTIONS[:EAST]) || (type == TYPES[:STRAIGHT_RIGHT_WALL] && direction == DIRECTIONS[:WEST])
|
79
|
-
return STRAIGHT_IMAGE.rotate(180)
|
80
|
-
elsif (type == TYPES[:STRAIGHT_LEFT_WALL] && direction == DIRECTIONS[:WEST]) || (type == TYPES[:STRAIGHT_RIGHT_WALL] && direction == DIRECTIONS[:EAST])
|
81
|
-
return STRAIGHT_IMAGE
|
82
|
-
elsif (type == TYPES[:STRAIGHT_LEFT_WALL] && direction == DIRECTIONS[:SOUTH]) || (type == TYPES[:STRAIGHT_RIGHT_WALL] && direction == DIRECTIONS[:NORTH])
|
83
|
-
return STRAIGHT_IMAGE.rotate(270)
|
84
|
-
end
|
85
|
-
|
86
|
-
case type
|
87
|
-
when TYPES[:STRAIGHT]
|
88
|
-
return STRAIGHT_IMAGE.rotate(90) if direction == DIRECTIONS[:NORTH] || direction == DIRECTIONS[:SOUTH]
|
89
|
-
STRAIGHT_IMAGE
|
90
|
-
when TYPES[:STRAIGHT_START]
|
91
|
-
case direction
|
92
|
-
when DIRECTIONS[:NORTH]
|
93
|
-
START_IMAGE.rotate(270)
|
94
|
-
when DIRECTIONS[:EAST]
|
95
|
-
START_IMAGE
|
96
|
-
when DIRECTIONS[:WEST]
|
97
|
-
START_IMAGE.rotate(180)
|
98
|
-
when DIRECTIONS[:SOUTH]
|
99
|
-
START_IMAGE.rotate(90)
|
100
|
-
end
|
101
|
-
when TYPES[:LEFT]
|
102
|
-
case direction
|
103
|
-
when DIRECTIONS[:NORTH]
|
104
|
-
CURVE_IMAGE.rotate(270)
|
105
|
-
when DIRECTIONS[:EAST]
|
106
|
-
CURVE_IMAGE
|
107
|
-
when DIRECTIONS[:WEST]
|
108
|
-
CURVE_IMAGE.rotate(180)
|
109
|
-
when DIRECTIONS[:SOUTH]
|
110
|
-
CURVE_IMAGE.rotate(90)
|
111
|
-
end
|
112
|
-
when TYPES[:RIGHT]
|
113
|
-
case direction
|
114
|
-
when DIRECTIONS[:NORTH]
|
115
|
-
CURVE_IMAGE
|
116
|
-
when DIRECTIONS[:EAST]
|
117
|
-
CURVE_IMAGE.rotate(90)
|
118
|
-
when DIRECTIONS[:WEST]
|
119
|
-
CURVE_IMAGE.rotate(270)
|
120
|
-
when DIRECTIONS[:SOUTH]
|
121
|
-
CURVE_IMAGE.rotate(180)
|
122
|
-
end
|
123
|
-
end
|
124
|
-
end
|
125
|
-
end
|
126
|
-
end
|