This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
SnowsongWolf's profile picture

D&D Ability Mod Percentage Program

Started by SnowsongWolf, 31 March 2013 - 01:52 PM
SnowsongWolf #1
Posted 31 March 2013 - 03:52 PM
Okay so admittedly this isn't really relevant to Minecraft. I just wanted to write the program and ComputerCraft was the place I had to do the least amount of work to get the code working.

This program assumes a block of monitors atop the computer with at least 20 lines of text. It would run faster if I didn't have it update the display with every iteration but it looks cooler this way. It assumes the "roll 4d6 and drop the lowest die" rule.

Oh yeah, and I started to implement a list for the percent chance that a die value will be the dropped die, but didn't get around to finishing that. That data would be stored in the dice[] array.


d6 = {}
scores = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
dice = {0,0,0,0,0,0}
runs = 100000
math.randomseed(os.time())
barStart = 13
m = peripheral.wrap("top")
term.redirect(m)
function setPos(x, y)
  term.setCursorPos(x, y)
end
function yield()
  os.queueEvent("blah")
  os.pullEvent("blah")
end
function bar(val, maxVal, pos)
  local w, h = term.getSize()
  local maxPos = math.floor(val / maxVal * (w - barStart) + 0.5)
  for i=barStart, maxPos + barStart do
	setPos(i, pos)
	term.write("#")
  end
  for i=maxPos + barStart + 1, w do
	setPos(i, pos)
	term.write(" ")
  end
end
function progress(run)
  local w, h = term.getSize()
  local pos = math.floor(run / runs * w + 0.5)
  setPos(pos, 1)
  term.write("#")
  setPos(math.floor(w / 2 - 3 + 0.5), 1)
  term.write((math.floor(run / runs * 10000) / 100) .. "%")
end
function setupScreen()
  for x=3,18 do
	setPos(1, x)
	print("("..x..")")
  end
end
function math.sum(arr)
  total = 0
  for i=1, #arr do
	total = total + arr[i]
  end
  return total
end
function results()
  topVal = math.max(unpack(scores))
  for x=3,18 do
	setPos(6, x)
	print((math.floor(scores[x] / math.sum(scores) * 10000 + 0.5) / 100).."%  ")
	bar(scores[x], topVal, x)
  end
end
term.clear()
setupScreen()
for x=1, runs + 1 do
  score = 0
  for y=1, 4 do
	d6[y] = math.random(1,6)
  end
  score = math.sum(d6) - math.min(unpack(d6))
  scores[score] = scores[score] + 1
  dice[math.min(unpack(d6))] = dice[math.min(unpack(d6))] + 1
  progress(x)
  results()
  --sleep(0.1)
  yield()
end
term.clear()
setupScreen()
setPos(1,1)
print("Score distribution: ("..runs.." die rolls)")
results()
term.restore()
oeed #2
Posted 01 April 2013 - 12:16 AM
So, what does this do?