pitchcar 0.2.0 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 702ba918ac8264afbcae75439e3523513b4d05d5
4
- data.tar.gz: 1f8dbe2e3391e7256c5948413572ebf13af6f135
3
+ metadata.gz: 2c58c55c7dd606fb47608a84e45d5b2b2b3b02f4
4
+ data.tar.gz: b54fa3cc18b8e9dd78c6194e2f734268c3c3afbc
5
5
  SHA512:
6
- metadata.gz: 430cc6d2ecf23b22d0fd0c0f5e410329d480768978b33995729b6461e5d71503c3f2f094f49fa3e221bb90b66c8174245f4771e2846009bfd8bcdc151e8fe136
7
- data.tar.gz: 1f2777b165ea539d566e093cccd1ca67d6da8544537e916ce7872825c990281f8e9ebd6c055fe9d3e8fd6fc2fda62f8e0a6441250684eb6239a82b9433f4f393
6
+ metadata.gz: c38eb9cc4657a81ac872c1d44968ee2652acaf9c74dbd73133b0c7288594cfb2753e59de11898159a1325107de1bd4420d7f0121033790428d295df3a4d290fd
7
+ data.tar.gz: 4816b8fc9925dd92e1362e67aa9ecdc052b3638b7736f8cd511f55f0597a3a7ef1ae5876470bc0e2e986030acc55fe9e37a2bb95578a163c9a8547768df1645d
data/Readme.md CHANGED
@@ -33,6 +33,13 @@ track = Pitchcar::Pitchcar.random_valid_track(6, 10)
33
33
  track.to_s # => 'Slw Slw L Slw R R Slw Slw R L R R L Slw R R'
34
34
  ```
35
35
 
36
+ #### With size restrictions
37
+ ```ruby
38
+ require 'pitchcar'
39
+ # Either min or max can be omitted
40
+ track = Pitchcar::Pitchcar.random_valid_track(6, 10, { min: { x: 1, y: 2 }, max: { x: 5, y: 5 } })
41
+ ```
42
+
36
43
  ### Find `n` random valid tracks
37
44
  ```ruby
38
45
  require 'pitchcar'
@@ -40,3 +47,10 @@ require 'pitchcar'
40
47
  track = Pitchcar::Pitchcar.random_valid_tracks(6, 10, 4)
41
48
  track.map(&:to_s) # => ["Slw Srw L R Srw L L Srw R L L Slw R Slw L L", "Slw L Slw L Slw R Srw L L R L Srw L R L Slw", "Slw R Slw Slw R Slw Srw L R R Srw R L R L R", "Slw Slw L Srw L R L Srw R L L Srw L R Slw L"]
42
49
  ```
50
+
51
+ #### With size restrictions
52
+ ```ruby
53
+ require 'pitchcar'
54
+ # Either min or max can be omitted
55
+ track = Pitchcar::Pitchcar.random_valid_tracks(6, 10, 4, { min: { x: 1, y: 2 }, max: { x: 5, y: 5 } })
56
+ ```
data/lib/pitchcar.rb CHANGED
@@ -10,15 +10,15 @@ module Pitchcar
10
10
  tracks.map(&:with_wall_combinations).flatten
11
11
  end
12
12
 
13
- def random_valid_track(straight, left_right)
13
+ def random_valid_track(straight, left_right, size_restrictions = {})
14
14
  track = nil
15
- track = random_track(straight, left_right) until !track.nil? && track.valid?
15
+ track = random_track(straight, left_right, size_restrictions) until !track.nil? && track.valid?
16
16
  track.with_wall_combinations.sample
17
17
  end
18
18
 
19
- def random_valid_tracks(straight, left_right, count = 2)
19
+ def random_valid_tracks(straight, left_right, count = 2, size_restrictions = {})
20
20
  tracks = []
21
- count.times { tracks << random_valid_track(straight, left_right) }
21
+ count.times { tracks << random_valid_track(straight, left_right, size_restrictions) }
22
22
  tracks
23
23
  end
24
24
 
@@ -40,10 +40,10 @@ module Pitchcar
40
40
  tracks
41
41
  end
42
42
 
43
- def random_track(straight, left_right)
43
+ def random_track(straight, left_right, size_restrictions = {})
44
44
  left = Random.rand(1..left_right)
45
45
  right = left_right - left
46
- Track.build_from("S#{'S' * (straight - 1)}#{'L' * left}#{'R' * right}".split('').shuffle.join)
46
+ Track.build_from("S#{'S' * (straight - 1)}#{'L' * left}#{'R' * right}".split('').shuffle.join, size_restrictions)
47
47
  end
48
48
  end
49
49
  end
data/lib/track.rb CHANGED
@@ -4,13 +4,15 @@ require_relative 'boyermoore'
4
4
 
5
5
  module Pitchcar
6
6
  class Track
7
- attr_accessor :pieces
7
+ attr_accessor :pieces, :max_size, :min_size
8
8
 
9
- def initialize(pieces)
9
+ def initialize(pieces, size_restrictions = {})
10
10
  self.pieces = pieces
11
+ self.max_size = size_restrictions.fetch(:max, x: Float::INFINITY, y: Float::INFINITY)
12
+ self.min_size = size_restrictions.fetch(:min, x: 1, y: 1)
11
13
  end
12
14
 
13
- def self.build_from(track_pieces)
15
+ def self.build_from(track_pieces, size_restrictions = {})
14
16
  pieces = PieceList.new([Piece.starting_piece])
15
17
 
16
18
  track_pieces[1..-1].chars.each do |piece_str|
@@ -34,11 +36,11 @@ module Pitchcar
34
36
  pieces << piece
35
37
  end
36
38
 
37
- Track.new(pieces)
39
+ Track.new(pieces, size_restrictions)
38
40
  end
39
41
 
40
42
  def valid?(tracks=[])
41
- ends_correctly? && !overlaps? && !rotation_exists?(tracks)
43
+ ends_correctly? && !overlaps? && !rotation_exists?(tracks) && within_size_restrictions?
42
44
  end
43
45
 
44
46
  def to_s
@@ -54,7 +56,7 @@ module Pitchcar
54
56
  x_coords = pieces.map(&:x)
55
57
  y_coords = pieces.map(&:y)
56
58
  # We have to add one, because a piece takes up a discrete distance
57
- [x_coords.max - x_coords.min + 1, y_coords.max - y_coords.min + 1]
59
+ { x: x_coords.max - x_coords.min + 1, y: y_coords.max - y_coords.min + 1 }
58
60
  end
59
61
 
60
62
  def overlaps?
@@ -89,6 +91,10 @@ module Pitchcar
89
91
  def ends_correctly?
90
92
  pieces.last.x == pieces.first.x && pieces.last.y == pieces.first.y + 1 && pieces.last.direction == Piece::DIRECTIONS[:SOUTH]
91
93
  end
94
+
95
+ def within_size_restrictions?
96
+ size[:x].between?(self.min_size[:x], self.max_size[:x]) && size[:y].between?(self.min_size[:y], self.max_size[:y])
97
+ end
92
98
  end
93
99
 
94
100
  class PieceList < Array
data/pitchcar.gemspec CHANGED
@@ -1,10 +1,10 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'pitchcar'
3
- s.version = '0.2.0'
3
+ s.version = '0.3.0'
4
4
  s.date = '2017-02-23'
5
- s.summary = "Pitchcar Track Generator"
6
- s.description = "Generates tracks for pitchcar"
7
- s.authors = ["Jonah Hirsch"]
5
+ s.summary = 'Pitchcar Track Generator'
6
+ s.description = 'Generates tracks for pitchcar'
7
+ s.authors = ['Jonah Hirsch']
8
8
  s.email = 'jhirsch@rmn.com'
9
9
  s.homepage = 'https://github.com/jonahwh/pitchcar_track_generator'
10
10
  s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
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.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonah Hirsch