Last active
December 19, 2021 11:45
-
-
Save parrot-studio/38fa9f8a806994dd897766edcc5ea055 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Race | |
| def initialize(racers) | |
| @racers = racers.map { |rc| rc.new } | |
| results = @racers.map { |_r| 1 } | |
| @racers.each do |r| | |
| r.set_turn_results(1, 1, results) | |
| end | |
| @goaled = [] | |
| end | |
| def start! | |
| turn = 1 | |
| loop do | |
| break if @racers.empty? | |
| @racers.each(&:run) | |
| @racers.sort_by! { |r| -r.position } | |
| puts '--------------------------------' | |
| puts "turn : #{turn}" | |
| @racers.each do |r| | |
| puts "#{r.class} => position:#{r.position}" | |
| end | |
| running = [] | |
| @racers.each do |r| | |
| r.goal? ? (@goaled << r) : (running << r) | |
| end | |
| break if running.empty? | |
| rank = 1 | |
| pos = 100 | |
| results = [] | |
| running.each.with_index(1) do |r, ind| | |
| rank = (pos > r.position ? ind : rank) | |
| results << rank | |
| pos = r.position | |
| end | |
| puts results.join(',') | |
| turn += 1 | |
| running.zip(results).each { |r, rank| r.set_turn_results(turn, rank, results) } | |
| @racers = running | |
| end | |
| puts '-------- RESULTS -------' | |
| @goaled.each.with_index(1) do |r, rank| | |
| puts "#{rank} => #{r.class}" | |
| end | |
| end | |
| end | |
| class Racer | |
| attr_reader :rank, :position, :turn | |
| def initialize | |
| @rank = 0 | |
| @position = 0 | |
| @turn = 0 | |
| end | |
| def goal? | |
| @position >= 100 | |
| end | |
| def set_turn_results(turn, rank, results) | |
| @turn = turn | |
| @rank = rank | |
| @results = results | |
| end | |
| def run | |
| @position += move_value | |
| end | |
| private | |
| # need override | |
| def move_value | |
| 0 | |
| end | |
| end | |
| class GoRockstar < Racer | |
| private | |
| def move_value | |
| last? ? 7 : 0 | |
| end | |
| def last? | |
| @results.max == @rank | |
| end | |
| end | |
| class Maruchan < Racer | |
| private | |
| def move_value | |
| @turn.odd? ? 3 : 0 | |
| end | |
| end | |
| class Danriki < Racer | |
| private | |
| def move_value | |
| head_only? ? -2 : 4 | |
| end | |
| def head_only? | |
| @rank == 1 && @results.select { |r| r == 1 }.size == 1 | |
| end | |
| end | |
| class Syakariki < Racer | |
| private | |
| def move_value | |
| same_rankers | |
| end | |
| def same_rankers | |
| @results.select { |r| r == @rank }.size | |
| end | |
| end | |
| class Syaroku < Racer | |
| private | |
| def move_value | |
| head? ? 1 : 3 | |
| end | |
| def head? | |
| @rank == 1 | |
| end | |
| end | |
| class RinRin < Racer | |
| private | |
| def move_value | |
| @turn.odd? ? 5 : -2 | |
| end | |
| end | |
| class Hijyousyoku < Racer | |
| private | |
| def move_value | |
| @power ||= 0 | |
| @power += (head_only? ? -4 : 1) | |
| @power | |
| end | |
| def head_only? | |
| @rank == 1 && @results.select { |r| r == 1 }.size == 1 | |
| end | |
| end | |
| class TimeX < Racer | |
| private | |
| def move_value | |
| @turn.modulo(5).zero? ? 9 : 0 | |
| end | |
| end | |
| race = Race.new( | |
| [ | |
| GoRockstar, | |
| Maruchan, | |
| Danriki, | |
| Syakariki, | |
| Syaroku, | |
| RinRin, | |
| Hijyousyoku, | |
| TimeX | |
| ] | |
| ) | |
| race.start! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment