xxxxxxxxxx
72
const numPlayers = 3;
const maxCoins = 3;
const data = [];
let barWidth;
let maxBarHeight;
let barScale = 10;
let maxVal = 0;
const simSteps = 10
const bottomBorder = 50;
function setup() {
createCanvas(400, 400);
const perms = (numPlayers * maxCoins) + 1;
barWidth = width/perms;
maxBarHeight = height - bottomBorder;
for(let i = 0; i < perms; i ++) {
data.push(0);
}
}
function draw() {
for(let i = 0; i < simSteps; i ++) {
playRound();
}
background(0);
line(0, maxBarHeight, width, maxBarHeight);
for(let i = 0; i < data.length; i ++) {
const d = data[i];
fill(255);
stroke(0);
if(d >= maxVal) {
maxVal = d;
fill(255, 0, 0);
stroke(100, 0, 0);
}
const x = i * barWidth;
const barHeight = d * barScale;
text(i, x + barWidth/2, maxBarHeight + bottomBorder/2);
rect(x, maxBarHeight - barHeight, barWidth, barHeight);
}
while(maxVal * barScale > maxBarHeight) {
barScale *= 0.99
}
}
function playRound() {
let t = 0;
for(let p = 0; p < numPlayers; p ++) {
t += hand();
}
data[t] += 1;
}
function hand() {
return int(random(0, maxCoins + 1));
}