Skip to content

Instantly share code, notes, and snippets.

@labocho
Created October 23, 2011 09:49
Show Gist options
  • Save labocho/1307188 to your computer and use it in GitHub Desktop.
Save labocho/1307188 to your computer and use it in GitHub Desktop.
Footwork Trainer for Fencer
#!/usr/bin/env ruby
# encoding: UTF-8
# Footwork trainer for fencer
# for Mac OS X
class Footwork
attr_accessor :name, :weight, :duration
def initialize(attrs = {})
attrs.each do |k, v|
send("#{k}=", v)
end
end
def say
puts name
system "say", name
sleep duration
end
end
class Trainer
class << self
attr_accessor :footworks, :total_weight, :weight_table
def initialize_weight_table
lowerbound = 0.0
@weight_table = {}
@footworks.each do |f|
range = lowerbound...(lowerbound + f.weight)
@weight_table[range] = f
lowerbound += f.weight
end
end
def say
select.say
end
def select
f = rand(total_weight)
weight_table.find{|k, v| k.include?(f)}[1]
end
end
@footworks = [
{name: "marche", weight: 5.0, duration: 0.5},
{name: "marche deux", weight: 2.0, duration: 1.0},
{name: "marche trois", weight: 1.0, duration: 2.0},
{name: "rompe", weight: 4.0, duration: 0.5},
{name: "rompe deux", weight: 2.0, duration: 1.0},
{name: "rompe trois", weight: 1.0, duration: 2.0},
{name: "bond avant", weight: 1.0, duration: 0.5},
{name: "bond arriere", weight: 1.0, duration: 0.5},
{name: "appel", weight: 1.0, duration: 0.5},
{name: "allongez le bras", weight: 1.0, duration: 0.0},
{name: "coup droit", weight: 1.0, duration: 0.5},
{name: "fandez vous", weight: 3.0, duration: 1.0},
{name: "balestra", weight: 1.0, duration: 0.5}
].map{|fw|
Footwork.new(fw)
}
@total_weight = @footworks.inject(0.0) {|total, f| total + f.weight }
initialize_weight_table
end
loop { Trainer.say }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment