Script: Dice

Common dice function

function isNum(s) {
  if (s == '') return false;
  for (var i=0; i<s.length; i++) {
    if (s.charAt(i) < '0') return false;
    if (s.charAt(i) > '9') return false;
  }
  return true;
}
 
function sortedList(n, d) {
  var t = 0;
  var a = new Array();
  for (var i=0; i<n; i++) {
    a[i] = Math.floor(Math.random()*d+1);
  }
  for (var i=0; i<n-1; i++) {
    for (var j=i; j<n; j++) {
      if (a[i] > a[j]) {
        t = a[i];
        a[i] = a[j];
        a[j] = t;
      }
    }
  }
  return a;
}
 
function dice(s) {
  var p = n = d = t = w = 0;
  p = s.indexOf('+');
  if (p >= 0) {
    t = dice(s.substring(0, p)) + dice(s.substring(p+1));
    return t;
  }
  p = s.indexOf('-');
  if (p >= 0) {
    t = dice(s.substring(0, p)) - dice(s.substring(p+1));
    return t;
  }
  p = s.indexOf('*');
  if (p >= 0) {
    return dice(s.substring(0, p)) * dice(s.substring(p+1));
  }
  p = s.indexOf('b');
  if (p >= 0) {
    w = s.substring(p+1);
    s = s.substring(0, p);
    p = s.indexOf('d');
    if (p < 0) return 0;
    n = s.substring(0, p);
    d = s.substring(p+1);
    if (w > n) w = n;
    var a = sortedList(n, d);
    t = 0;
    for (var i=n-w; i<n; i++)
      t += a[i];
    return t;
  }
  p = s.indexOf('w');
  if (p >= 0) {
    w = s.substring(p+1);
    s = s.substring(0, p);
    p = s.indexOf('d');
    if (p < 0) return 0;
    n = s.substring(0, p);
    d = s.substring(p+1);
    if (w > n) w = n;
    var a = sortedList(n, d);
    t = 0;
    for (var i=0; i<w; i++)
      t += a[i];
    return t;
  }
  p = s.indexOf('d');
  if (p >= 0) {
    n = s.substring(0, p);
    d = s.substring(p+1);
    t = 0;
    for (var i=0; i<n; i++) {
      t += Math.floor(Math.random()*d+1);
    }
    return t;
  }
  if (isNum(s)) {
    return 1 * s;
  } else {
    return 0;
  }
}

Basic dice roller

<html>
<head>
<script language="javascript" src="http://grpg.wikidot.com/script:dice/code/1"></script>
<script language="javascript">
function roll() {
  document.getElementById('ans').innerHTML = dice(document.myForm.myDice.value);
}
</script>
</head>
<body>
<form name="myForm">
<table border=0>
  <tr>
    <td><input type="text" name="myDice"></td>
    <td style="width: 80px; text-align:center;"><input type="button" value="Roll" onClick="roll()"></td>
    <td style="width: 80px; text-align:center; font-size:18pt;"><span id="ans">&nbsp;</span></td>
  </tr>
</table>
</form>
</body>
</html>

Attribute Array

<html>
<head>
<script language="javascript" src="http://grpg.wikidot.com/script:dice/code/1"></script>
<script language="javascript">
function roll() {
  var a = new Array();
  for (var i=0; i<7; i++) {
    a[i] = dice('4d6b3');
    document.getElementById('a' + i).innerHTML = a[i];
  }
}
</script>
</head>
<body>
<form name="myForm">
<table border=0>
  <tr>
    <td style="width: 80px;"><input type="button" value="Roll" onClick="roll()"></td>
    <td style="width: 50px; text-align:center; font-size:18pt;"><span id="a0">&nbsp;</td>
    <td style="width: 50px; text-align:center; font-size:18pt;"><span id="a1">&nbsp;</td>
    <td style="width: 50px; text-align:center; font-size:18pt;"><span id="a2">&nbsp;</td>
    <td style="width: 50px; text-align:center; font-size:18pt;"><span id="a3">&nbsp;</td>
    <td style="width: 50px; text-align:center; font-size:18pt;"><span id="a4">&nbsp;</td>
    <td style="width: 50px; text-align:center; font-size:18pt;"><span id="a5">&nbsp;</td>
    <td style="width: 50px; text-align:center; font-size:18pt; border: solid 1pt black;"><span id="a6">&nbsp;</td>
  </tr>
</table>
</form>
</body>
</html>

Dice Roller Graph

<html>
<head>
<script language="javascript" src="http://grpg.wikidot.com/script:dice/code/1"></script>
<script language="javascript">
 
var num, min, max;
 
function startMe() {
  num = new Array(100);
  for (var i=0; i<1000; i++)
    num[i] = 0;
  min = 1000;
  max = 0;
  most = 0;
  throwMe();
}
 
function throwMe() {
  var n;
  for (var i=0; i<500; i++) {
    n = dice(document.myForm.dd.value);
    if (n < 0) n = 0;
    if (min > n) min = n;
    if (max < n) max = n;
    num[n]++;
    if (most < num[n]) most = num[n];
  }
  drawMe();
  if (most < 1000) setTimeout('throwMe();', 200);
}
 
function toHex(n) {
  var digits = '0123456789ABCDEF';
  if (n < 0) n = 0;
  if (n > 255) n = 255;
  var s = 'x';
  s += digits.charAt(Math.floor(n/16));
  s += digits.charAt(n%16);
  return s.substring(1);
}
 
function getColour(p) {
  var r = 0;
  var g = 0;
  var b = 0;
  if (p < 0) p = 0;
  if (p > 1) p = 1;
  if (p < 1/8) {
    g = 255;
    b = 255*8*p;
  } else if (p < 2/8) {
    g = 255*(2 - 8*p);
    b = 255;
  } else if (p < 3/8) {
    b = 255;
    r = 255*(8*p - 2);
  } else if (p < 4/8) {
    b = 255*(4 - 8*p);
    r = 255;
  } else if (p < 6/8) {
    r = 255;
    g = 255*(4*p - 2);
  } else {
    r = 255*(4 - 4*p);
    g = 255;
  }
  return '#' + toHex(r) + toHex(g) + toHex(b);
}
 
function drawMe(){
  var canvas = document.getElementById('myGraph');
  if (canvas.getContext){
    var ctx = canvas.getContext('2d');
    ctx.fillStyle = 'white';
    ctx.fillRect(0, 0, 500, 500);
    ctx.lineWidth = 0.5;
    ctx.strokeStyle = 'black';
    var bar = max - min + 1;
    var wid = Math.floor(450 / bar);
    ctx.font = '8pt sans-serif';
    ctx.textAlign = 'left';
    for (var i=0; i<bar; i++) {
      ctx.fillStyle = getColour(i/bar);
      ctx.fillRect(40 + wid*i, 280 - Math.floor(250*(num[i+min]/most)), wid, Math.floor(250*(num[i+min]/most)));
      ctx.strokeRect(40 + wid*i, 280 - Math.floor(250*(num[i+min]/most)), wid, Math.floor(250*(num[i+min]/most)));
      ctx.fillStyle = 'black';
      ctx.fillText((i+min), 43 + wid*i, 292);
    }
    for (var i=0; i<=most; i+=50) {
      ctx.fillText(i, 15, 280 - Math.floor(250*i/most));
    }
    ctx.strokeStyle = 'red';
    ctx.beginPath();
    ctx.moveTo(40, 10);
    ctx.lineTo(40, 280);
    ctx.lineTo(490, 280);
    ctx.stroke();
    ctx.closePath();
    ctx.fillStyle = 'black';
  }
}
 
</script>
 
</head>
 
<body>
<table border=0>
  <tr valign="top">
    <td align="center">
      <form name="myForm">
      <input type="text" name="dd"><br>
      <input type="button" value="Roll" onClick="startMe();">
      </form>
    </td>
    <td><canvas id="myGraph" width="500" height="300"></canvas></td>
  </tr>
</table>
</body>
 
</html>
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License