diff options
author | Nat Lasseter <user@4574.co.uk> | 2024-08-06 23:05:06 +0100 |
---|---|---|
committer | Nat Lasseter <user@4574.co.uk> | 2024-08-06 23:05:06 +0100 |
commit | 1eed6c8c6db71005f3ec098ccc833353b52b2632 (patch) | |
tree | 0214b99e294ca45e39e2d59581e93b50bce4c51f | |
parent | 9bc9955e2c8436dfee063b0c1106c6a07838a961 (diff) |
Add goforthanddie.rb
-rw-r--r-- | README | 2 | ||||
-rwxr-xr-x | goforthanddie.rb | 64 |
2 files changed, 66 insertions, 0 deletions
@@ -19,6 +19,8 @@ digits.rb: Digit combinations for target sum gen_object/: The start of an erlang OO behaviour +goforthanddie.rb: + It's a pun. It's also a dice roller. It's not going to be an entire Forth. logcircle.rb: Output cartesian coordinates for the tickmarks along a log scale around a circle make.rb: diff --git a/goforthanddie.rb b/goforthanddie.rb new file mode 100755 index 0000000..9b458cc --- /dev/null +++ b/goforthanddie.rb @@ -0,0 +1,64 @@ +#!/usr/bin/env ruby + +# d20+1 # 20 1 d 1 + p +# 10d6k8 # 6 10 d 10 8 keep 8 .+ p +# 3d6>2 # 6 3 d 3 2 > p +# 3d12/2 # 12 3 d + + 2 / p + +stack = [] + +loop do + print "> " + gets.split.each do |token| + case token + when /[0-9]+/ + stack.push(token.to_i) + when "+" + a, b = stack.pop(2) + stack.push(a + b) + when ".+" + l = stack.pop + h = stack.pop(n) + stack.push(h.sum) + when "-" + a, b = stack.pop(2) + stack.push(a - b) + when "*" + a, b = stack.pop(2) + stack.push(a * b) + when "/" + a, b = stack.pop(2) + stack.push(a / b) + when "%" + a, b = stack.pop(2) + stack.push(a % b) + when "d", "roll" + n = stack.pop + d = stack.pop + n.times { stack.push(rand(d) + 1) } + when "p" + puts stack.pop + when "s" + pp stack + when "k", "keep" + k = stack.pop + n = stack.pop + h = stack.pop(n).sort + stack.push(*h.pop(k)) + when ">" + t = stack.pop + n = stack.pop + h = stack.pop(n) + stack.push(*h.select{|d|d > t}) + when "<" + t = stack.pop + n = stack.pop + h = stack.pop(n) + stack.push(*h.select{|d|d < t}) + when "q" + exit + else + puts "I don't know how to #{token}!" + end + end +end |