pitchcar 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 +7 -0
- data/LICENSE.md +22 -0
- data/Readme.md +42 -0
- data/boyermoore.rb +117 -0
- data/finder.rb +50 -0
- data/piece.rb +66 -0
- data/pitchcar_tracks +20160 -0
- data/track.rb +99 -0
- metadata +50 -0
    
        data/track.rb
    ADDED
    
    | @@ -0,0 +1,99 @@ | |
| 1 | 
            +
            require 'csv'
         | 
| 2 | 
            +
            require_relative 'piece'
         | 
| 3 | 
            +
            require_relative 'boyermoore'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            module Pitchcar
         | 
| 6 | 
            +
              class Track
         | 
| 7 | 
            +
                attr_accessor :pieces
         | 
| 8 | 
            +
             | 
| 9 | 
            +
                def initialize(pieces)
         | 
| 10 | 
            +
                  self.pieces = pieces
         | 
| 11 | 
            +
                end
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                def self.build_from(track_pieces)
         | 
| 14 | 
            +
                  pieces = PieceList.new([Piece.starting_piece])
         | 
| 15 | 
            +
             | 
| 16 | 
            +
                  track_pieces[1..-1].chars.each do |piece_str|
         | 
| 17 | 
            +
                    piece = Piece.new
         | 
| 18 | 
            +
                    piece.x = pieces.last.x
         | 
| 19 | 
            +
                    piece.y = pieces.last.y
         | 
| 20 | 
            +
                    piece.type = Piece.type_from_string(piece_str)
         | 
| 21 | 
            +
             | 
| 22 | 
            +
                    case pieces.last.direction
         | 
| 23 | 
            +
                    when Piece::DIRECTIONS[:NORTH]
         | 
| 24 | 
            +
                      piece.y = pieces.last.y + 1
         | 
| 25 | 
            +
                    when Piece::DIRECTIONS[:EAST]
         | 
| 26 | 
            +
                      piece.x = pieces.last.x + 1
         | 
| 27 | 
            +
                    when Piece::DIRECTIONS[:WEST]
         | 
| 28 | 
            +
                      piece.x = pieces.last.x - 1
         | 
| 29 | 
            +
                    when Piece::DIRECTIONS[:SOUTH]
         | 
| 30 | 
            +
                      piece.y = pieces.last.y - 1
         | 
| 31 | 
            +
                    end
         | 
| 32 | 
            +
                    piece.direction = piece.next_direction(pieces.last.direction)
         | 
| 33 | 
            +
             | 
| 34 | 
            +
                    pieces << piece
         | 
| 35 | 
            +
                  end
         | 
| 36 | 
            +
             | 
| 37 | 
            +
                  Track.new(pieces)
         | 
| 38 | 
            +
                end
         | 
| 39 | 
            +
             | 
| 40 | 
            +
                def valid?(tracks=[])
         | 
| 41 | 
            +
                  ends_correctly? && !overlaps? && !rotation_exists?(tracks)
         | 
| 42 | 
            +
                end
         | 
| 43 | 
            +
             | 
| 44 | 
            +
                def to_s
         | 
| 45 | 
            +
                  pieces[0].type = Piece::TYPES[:STRAIGHT_RIGHT_WALL]
         | 
| 46 | 
            +
                  pieces.map(&:to_s).join(' ')
         | 
| 47 | 
            +
                end
         | 
| 48 | 
            +
             | 
| 49 | 
            +
                def to_json
         | 
| 50 | 
            +
                  pieces.map(&:to_h).to_json
         | 
| 51 | 
            +
                end
         | 
| 52 | 
            +
             | 
| 53 | 
            +
                def overlaps?
         | 
| 54 | 
            +
                  @overlaps ||= pieces.group_by { |piece| [piece.x, piece.y] }.values.any? { |set| set.size > 1 }
         | 
| 55 | 
            +
                end
         | 
| 56 | 
            +
             | 
| 57 | 
            +
                def with_wall_combinations(pieces = PieceList.new(self.pieces)[1..-1], combinations = [])
         | 
| 58 | 
            +
                  straight_index = pieces.find_index { |piece| piece.type == Piece::TYPES[:STRAIGHT] }
         | 
| 59 | 
            +
                  if straight_index
         | 
| 60 | 
            +
                    combos = []
         | 
| 61 | 
            +
                    [Piece::TYPES[:STRAIGHT_LEFT_WALL], Piece::TYPES[:STRAIGHT_RIGHT_WALL]].each do |piece_type|
         | 
| 62 | 
            +
                      pieces_copy = PieceList.new(pieces)
         | 
| 63 | 
            +
                      pieces_copy[straight_index].type = piece_type
         | 
| 64 | 
            +
                      combos = with_wall_combinations(pieces_copy, combinations)
         | 
| 65 | 
            +
                    end
         | 
| 66 | 
            +
                    combos
         | 
| 67 | 
            +
                  else
         | 
| 68 | 
            +
                    combinations << Track.new([self.pieces.first] + pieces)
         | 
| 69 | 
            +
                  end
         | 
| 70 | 
            +
                end
         | 
| 71 | 
            +
             | 
| 72 | 
            +
                private
         | 
| 73 | 
            +
             | 
| 74 | 
            +
                def rotation_exists?(tracks)
         | 
| 75 | 
            +
                  tracks.each do |other_track|
         | 
| 76 | 
            +
                    other_track = other_track.to_s.gsub(' ', '') * 2
         | 
| 77 | 
            +
                    return true unless BoyerMoore.search(other_track, to_s.gsub(' ', '')).nil?
         | 
| 78 | 
            +
                  end
         | 
| 79 | 
            +
                  false
         | 
| 80 | 
            +
                end
         | 
| 81 | 
            +
             | 
| 82 | 
            +
                def ends_correctly?
         | 
| 83 | 
            +
                  pieces.last.x == pieces.first.x && pieces.last.y == pieces.first.y + 1 && pieces.last.direction == Piece::DIRECTIONS[:SOUTH]
         | 
| 84 | 
            +
                end
         | 
| 85 | 
            +
              end
         | 
| 86 | 
            +
             | 
| 87 | 
            +
              class PieceList < Array
         | 
| 88 | 
            +
                def initialize(pieces)
         | 
| 89 | 
            +
                  pieces.each do |piece|
         | 
| 90 | 
            +
                    new_piece = Piece.new
         | 
| 91 | 
            +
                    new_piece.x = piece.x
         | 
| 92 | 
            +
                    new_piece.y = piece.y
         | 
| 93 | 
            +
                    new_piece.type = piece.type
         | 
| 94 | 
            +
                    new_piece.direction = piece.direction
         | 
| 95 | 
            +
                    self.<< new_piece
         | 
| 96 | 
            +
                  end
         | 
| 97 | 
            +
                end
         | 
| 98 | 
            +
              end
         | 
| 99 | 
            +
            end
         | 
    
        metadata
    ADDED
    
    | @@ -0,0 +1,50 @@ | |
| 1 | 
            +
            --- !ruby/object:Gem::Specification
         | 
| 2 | 
            +
            name: pitchcar
         | 
| 3 | 
            +
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            +
              version: 0.1.0
         | 
| 5 | 
            +
            platform: ruby
         | 
| 6 | 
            +
            authors:
         | 
| 7 | 
            +
            - Jonah Hirsch
         | 
| 8 | 
            +
            autorequire: 
         | 
| 9 | 
            +
            bindir: bin
         | 
| 10 | 
            +
            cert_chain: []
         | 
| 11 | 
            +
            date: 2017-02-23 00:00:00.000000000 Z
         | 
| 12 | 
            +
            dependencies: []
         | 
| 13 | 
            +
            description: Generates tracks for pitchcar
         | 
| 14 | 
            +
            email: jhirsch@rmn.com
         | 
| 15 | 
            +
            executables: []
         | 
| 16 | 
            +
            extensions: []
         | 
| 17 | 
            +
            extra_rdoc_files: []
         | 
| 18 | 
            +
            files:
         | 
| 19 | 
            +
            - LICENSE.md
         | 
| 20 | 
            +
            - Readme.md
         | 
| 21 | 
            +
            - boyermoore.rb
         | 
| 22 | 
            +
            - finder.rb
         | 
| 23 | 
            +
            - piece.rb
         | 
| 24 | 
            +
            - pitchcar_tracks
         | 
| 25 | 
            +
            - track.rb
         | 
| 26 | 
            +
            homepage: https://github.com/jonahwh/pitchcar_track_generator
         | 
| 27 | 
            +
            licenses:
         | 
| 28 | 
            +
            - MIT
         | 
| 29 | 
            +
            metadata: {}
         | 
| 30 | 
            +
            post_install_message: 
         | 
| 31 | 
            +
            rdoc_options: []
         | 
| 32 | 
            +
            require_paths:
         | 
| 33 | 
            +
            - lib
         | 
| 34 | 
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         | 
| 35 | 
            +
              requirements:
         | 
| 36 | 
            +
              - - ">="
         | 
| 37 | 
            +
                - !ruby/object:Gem::Version
         | 
| 38 | 
            +
                  version: '0'
         | 
| 39 | 
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         | 
| 40 | 
            +
              requirements:
         | 
| 41 | 
            +
              - - ">="
         | 
| 42 | 
            +
                - !ruby/object:Gem::Version
         | 
| 43 | 
            +
                  version: '0'
         | 
| 44 | 
            +
            requirements: []
         | 
| 45 | 
            +
            rubyforge_project: 
         | 
| 46 | 
            +
            rubygems_version: 2.6.3
         | 
| 47 | 
            +
            signing_key: 
         | 
| 48 | 
            +
            specification_version: 4
         | 
| 49 | 
            +
            summary: Pitchcar Track Generator
         | 
| 50 | 
            +
            test_files: []
         |