aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNat Lasseter <user@4574.co.uk>2024-05-07 21:27:05 +0100
committerNat Lasseter <user@4574.co.uk>2024-05-07 21:27:05 +0100
commite796ea2d6d3aabe84ff165d8be110506482f895d (patch)
treeb395e9c8d846756f23436ad01b4601e24a6019ba
parent18728ccf1262f3cd1425a8277264d18af73a8a26 (diff)
Two levels!
-rw-r--r--01-hub2.rb3
-rw-r--r--02-hub3.rb34
-rwxr-xr-x[-rw-r--r--]forward-please (renamed from forward-please.rb)34
-rw-r--r--frame.rb1
-rw-r--r--level.rb9
5 files changed, 72 insertions, 9 deletions
diff --git a/01-hub2.rb b/01-hub2.rb
index 40f54bc..2a88ad9 100644
--- a/01-hub2.rb
+++ b/01-hub2.rb
@@ -1,11 +1,12 @@
require './level'
require './frame'
-class L1_Hub2 < Level
+class Level
interfaces %w(1 2)
description <<~EOD
You are a two-port hub. Your job is to forward frames from one interface to the other.
EOD
+ clicks 3
def self.target(frame)
case frame.iface
diff --git a/02-hub3.rb b/02-hub3.rb
new file mode 100644
index 0000000..8750027
--- /dev/null
+++ b/02-hub3.rb
@@ -0,0 +1,34 @@
+require './level'
+require './frame'
+
+class Level
+ interfaces %w(1 2 3)
+ description <<~EOD
+ You are now a three-port hub. Your job is to forward frames from one interface to all the others.
+ EOD
+ clicks 5
+
+ def self.target(frame)
+ case frame.iface
+ when ?1
+ [frame.to(?2), frame.to(?3)]
+ when ?2
+ [frame.to(?1), frame.to(?3)]
+ when ?3
+ [frame.to(?1), frame.to(?2)]
+ else
+ puts "ERR"
+ []
+ end
+ end
+
+ def self.generate
+ frames = {}
+ @@interfaces.each do |iface|
+ next if rand < 0.3
+ frames[@@count.to_s] = Frame.new(iface, "Frame #{@@count}")
+ @@count += 1
+ end
+ frames
+ end
+end
diff --git a/forward-please.rb b/forward-please
index c919079..70c6986 100644..100755
--- a/forward-please.rb
+++ b/forward-please
@@ -1,8 +1,9 @@
-require './01-hub2.rb'
+#!/usr/bin/env ruby
-$level = L1_Hub2
+$levels = %w(01-hub2 02-hub3)
+require "./#{$levels.shift}"
-puts $level.description
+puts Level.description
def help
puts <<~EOF
@@ -24,6 +25,7 @@ def forward(frame, iface)
end
def clickactions
+ puts "Click #{$click}:"
puts "Frames:"
$clickframes.each do |id, frame|
puts " #{id}: #{frame.description} from Interface #{frame.iface}"
@@ -36,14 +38,17 @@ def clickactions
end
end
+$click = 1
+
def click
right = 0
wrong = 0
targetframes = []
$clickframes.each do |id, frame|
- targetframes += $level.target(frame)
+ targetframes += Level.target(frame)
end
+ targetframes.flatten!
$instructions.each do |id, frame|
if targetframes.include?(frame)
@@ -58,7 +63,20 @@ def click
puts "#{right} out of #{right+wrong} correct"
$instructions = []
- $clickframes = $level.generate
+
+ $click += 1
+ if $click > Level.clicks
+ $click = 1
+ level = $levels.shift
+ if level.nil?
+ puts "Demo all done"
+ exit 0
+ end
+ require "./#{level}"
+ puts Level.description
+ end
+
+ $clickframes = Level.generate
clickactions
end
@@ -74,9 +92,9 @@ def handle(cmd)
puts "f {frame} {iface}"
end
when ?d
- puts $level.description
+ puts Level.description
when ?i
- puts $level.interfaces
+ puts Level.interfaces
when ?a
clickactions
when ?c
@@ -86,7 +104,7 @@ def handle(cmd)
end
end
-$clickframes = $level.generate
+$clickframes = Level.generate
clickactions
loop do
diff --git a/frame.rb b/frame.rb
index a8d23e6..a61c547 100644
--- a/frame.rb
+++ b/frame.rb
@@ -13,6 +13,7 @@ class Frame
end
def ==(oth)
+ @description == oth.description &&
@iface == oth.iface
end
end
diff --git a/level.rb b/level.rb
index 21ebc61..1a5cfee 100644
--- a/level.rb
+++ b/level.rb
@@ -2,6 +2,7 @@ class Level
@@interfaces = []
@@description = ""
@@count = 0
+ @@clicks = 5
attr_accessor :description
@@ -20,4 +21,12 @@ class Level
@@description = desc
end
end
+
+ def self.clicks(clicks = nil)
+ if clicks.nil?
+ @@clicks
+ else
+ @@clicks = clicks
+ end
+ end
end