aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--01-hub2.rb13
-rw-r--r--forward-please.rb42
-rw-r--r--frame.rb4
3 files changed, 47 insertions, 12 deletions
diff --git a/01-hub2.rb b/01-hub2.rb
index 4ed0f6b..40f54bc 100644
--- a/01-hub2.rb
+++ b/01-hub2.rb
@@ -9,17 +9,20 @@ class L1_Hub2 < Level
def self.target(frame)
case frame.iface
- when 1
- [frame.to(2)]
- when 2
- [frame.to(1)]
+ when ?1
+ [frame.to(?2)]
+ when ?2
+ [frame.to(?1)]
+ else
+ puts "ERR"
+ []
end
end
def self.generate
frames = {}
@@interfaces.each do |iface|
- next if rand < 0.4
+ next if rand < 0.3
frames[@@count.to_s] = Frame.new(iface, "Frame #{@@count}")
@@count += 1
end
diff --git a/forward-please.rb b/forward-please.rb
index fc36a6e..c919079 100644
--- a/forward-please.rb
+++ b/forward-please.rb
@@ -25,18 +25,41 @@ end
def clickactions
puts "Frames:"
- $clickframes.each do |i, frame|
- puts " #{i}: Interface #{frame.iface} -> #{frame.description}"
+ $clickframes.each do |id, frame|
+ puts " #{id}: #{frame.description} from Interface #{frame.iface}"
end
- l = 1
+ i = 1
puts "Actions:"
$instructions.each do |instr|
- puts " #{l}: Forward frame #{instr[0]} to interface #{instr[1].iface}"
+ puts " #{i}: Forward frame #{instr[0]} to interface #{instr[1].iface}"
end
end
def click
+ right = 0
+ wrong = 0
+
+ targetframes = []
+ $clickframes.each do |id, frame|
+ targetframes += $level.target(frame)
+ end
+
+ $instructions.each do |id, frame|
+ if targetframes.include?(frame)
+ right += 1
+ targetframes.delete(frame)
+ else
+ wrong += 1
+ end
+ end
+ wrong += targetframes.count
+
+ puts "#{right} out of #{right+wrong} correct"
+
+ $instructions = []
+ $clickframes = $level.generate
+ clickactions
end
def handle(cmd)
@@ -44,7 +67,12 @@ def handle(cmd)
when ?h
help
when ?f
- forward(*cmd.split[1..])
+ args = cmd.split[1..]
+ if args.count == 2
+ forward(*args)
+ else
+ puts "f {frame} {iface}"
+ end
when ?d
puts $level.description
when ?i
@@ -53,14 +81,14 @@ def handle(cmd)
clickactions
when ?c
click
- when :q
+ when ?q
exit 0
end
end
$clickframes = $level.generate
-pp $clickframes
+clickactions
loop do
print ">: "
handle(gets.strip)
diff --git a/frame.rb b/frame.rb
index 70216c8..a8d23e6 100644
--- a/frame.rb
+++ b/frame.rb
@@ -11,4 +11,8 @@ class Frame
t.iface = iface
t
end
+
+ def ==(oth)
+ @iface == oth.iface
+ end
end