diff options
-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 |