xxxxxxxxxx
99
let buttonASilent, buttonABetray, buttonBSilent, buttonBBetray;
let lastChoiceA = "";
let lastChoiceB = "";
let outcome = "";
function setup() {
createCanvas(800, 600);
textSize(20);
// Buttons for Prisoner A
buttonASilent = createButton('Remain Silent');
buttonASilent.position(150, 150);
buttonASilent.mousePressed(() => updateChoices("A", "Silent"));
buttonABetray = createButton('Betray Other');
buttonABetray.position(150, 190);
buttonABetray.mousePressed(() => updateChoices("A", "Betray"));
// Buttons for Prisoner B
buttonBSilent = createButton('Remain Silent');
buttonBSilent.position(550, 150);
buttonBSilent.mousePressed(() => updateChoices("B", "Silent"));
buttonBBetray = createButton('Betray Other');
buttonBBetray.position(550, 190);
buttonBBetray.mousePressed(() => updateChoices("B", "Betray"));
}
function draw() {
background(240);
// Display Labels and Rectangles
fill(200);
rect(100, 50, 200, 50);
rect(500, 50, 200, 50);
fill(0);
text("Prisoner A", 200, 75);
text("Prisoner B", 600, 75);
// Show outcomes in a 2x2 grid
textSize(16);
text("Both Remain Silent: Lightest Sentence", 200, 300);
text("Both Betray: Harsher Sentence", 200, 340);
text("A Betrays, B Silent: A free, B max", 200, 380);
text("B Betrays, A Silent: B free, A max", 200, 420);
// Highlight outcome
if (outcome !== "") {
highlightOutcome();
}
// Show last choices
textSize(20);
fill(0);
text("Last Choice A: " + lastChoiceA, 200, 500);
text("Last Choice B: " + lastChoiceB, 600, 500);
}
function updateChoices(prisoner, choice) {
if (prisoner === "A") {
lastChoiceA = choice;
} else {
lastChoiceB = choice;
}
determineOutcome();
}
function determineOutcome() {
if (lastChoiceA === "Silent" && lastChoiceB === "Silent") {
outcome = "silent";
} else if (lastChoiceA === "Betray" && lastChoiceB === "Betray") {
outcome = "betray";
} else if (lastChoiceA === "Betray" && lastChoiceB === "Silent") {
outcome = "aWins";
} else if (lastChoiceB === "Betray" && lastChoiceA === "Silent") {
outcome = "bWins";
} else {
outcome = "";
}
}
function highlightOutcome() {
stroke(255, 0, 0);
strokeWeight(2);
noFill();
if (outcome === "silent") {
rect(50, 280, 700, 30);
} else if (outcome === "betray") {
rect(50, 320, 700, 30);
} else if (outcome === "aWins") {
rect(50, 360, 700, 30);
} else if (outcome === "bWins") {
rect(50, 400, 700, 30);
}
noStroke();
}