xxxxxxxxxx
247
// This program is an attempt to visuallize
// the 100 prisoner problem
// and the chains created therein
// TODO: Go through this line by line and verify that
// I did it right. Because I get numbers that go toward 30% and the video says it should be closer to 31%
var num=100; // Number of prisoners
var size=350; // Size of circle to visualiz
var boxes=[]; // Array of boxes
var attempts=0; // Number of attempts
var successes=0; // Number of successes
var fails=0; // Number of failures
var chains=[]; // An array of chain class
var xarr=[]; // X coords visualizing chain
var yarr=[]; // Same
var flag=false;
function setup() {
createCanvas(400, 400);
initialize();
}
function draw() {
background(220);
fill("white");
circle(width/2, height/2, size);
fill("orange");
for(var i=0; i<xarr.length; i++)
{
circle(xarr[i], yarr[i], 5);
}
showSuccess();
stroke(0);
fill(0);
text("Attempts: " + (attempts-1), 10, 15);
text("Failures: " + fails, 10, 30);
text("Successes: " + successes, 10, 45);
text("Percent success: " + round((successes/(attempts-1))*100,2), 250, 15);
}
function openBoxes()
{
for(var i=0; i<num; i++) // For each prisoner
{
chains.push(new Chain()); // Create chain
complete=false;
chains[i].train.push(boxes[i]);
for(var j=0; (j<=num/2 && !complete); j++) // Start guessing
{
if(chains[i].train[j]==i+1)
{
complete=true;
//console.log("BROKEOUT");
}
else
{
nextbox=chains[i].train[j];
chains[i].train.push(boxes[nextbox-1]);
}
}
}
//console.table(chains)
}
function showSuccess()
{
// If successfull, graph the trains
var success=true;
for(var i=0; i<chains.length && success; i++)
{
success=chains[i].train.length<=num/2;
}
if(success)
{
//console.log(success);
// eliminate extra chains
for(var i=chains.length-1; i>=0; i--)
//for(var i=0; i<chains.length; i++)
{
// First find lowest number in chain
var lowest=101;
var lowind=101;
for(var j=0; j<chains[i].train.length; j++)
{
if(chains[i].train[j]<lowest)
{
lowest=chains[i].train[j];
lowind=j;
}
}
if(lowind>0)
{
chains.splice(i,1);
}
}
//console.log("CHAINS: "+chains.length);
for(var i=0; i<chains.length; i++)
{
for(var j=0; j<chains[i].train.length-1; j++)
{
ind1=chains[i].train[j]-1;
ind2=chains[i].train[j+1]-1;
if(ind2===undefined)
{
ind2=i;
}
col=randColor(i%11);
stroke(col);
line(xarr[ind1], yarr[ind1], xarr[ind2], yarr[ind2]);
}
}
successes++;
initialize();
}
else
{
fails++;
initialize();
//console.log("FAIL");
}
}
function randColor(n)
{
var c="white"
switch(n)
//switch(floor(random(10)))
{
case 0: c="red";
break;
case 1: c="orange";
break;
case 2: c="yellow";
break;
case 3: c="green";
break;
case 4: c="blue";
break;
case 5: c="purple";
break;
case 6: c="black";
break;
case 7: c="skyblue";
break;
case 8: c="cyan";
break;
case 9: c="teal";
break;
case 10: c="magenta";
break;
}
return c;
}
function initialize()
{
attempts++;
boxes.length=0;
xarr.length=0;
yarr.length=0;
chains.length=0;
// Create Prisoner boxes and put their own number in first
for(var i=0; i<num; i++)
{
boxes.push(i+1);
// Also fill xarr and yarr with points
x= size/2 * cos(radians(360/num*i))+width/2;
y= size/2 * sin(radians(360/num*i))+height/2;
xarr.push(x);
yarr.push(y);
}
// Mix up numbers in boxes
for(var l=0; l<1; l++) // Mixing it num times
{
for(var i=0; i<num; i++)
{
var swap=floor(random(num));
var temp=boxes[swap];
boxes[swap]=boxes[i];
boxes[i]=temp;
}
}
//console.table(boxes);
openBoxes();
}
function keyPressed()
{
flag=!flag;
if(flag)
{
noLoop();
}
else
loop();
}