From 18728ccf1262f3cd1425a8277264d18af73a8a26 Mon Sep 17 00:00:00 2001 From: Nat Lasseter Date: Sun, 5 May 2024 13:26:48 +0100 Subject: L1 playable --- 01-hub2.rb | 13 ++++++++----- forward-please.rb | 42 +++++++++++++++++++++++++++++++++++------- frame.rb | 4 ++++ 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 -- cgit v1.2.1