xxxxxxxxxx
107
let currentRoundCandidates = [];
let nextRoundCandidates = [];
let currentPair = [];
let roundNumber = 1;
let gameIsOver = false;
let showMessage = false;
let messageStartFrame;
const messageDuration = 80;
let candidates = [];
function setup() {
createCanvas(600, 400);
candidates = Array.from({length: 32}, (_, i) => i + 1);
currentRoundCandidates = shuffle(candidates); // shuffle array
updateCurrentPair();
}
function gameSetup() {
// reshuffle candidates array
candidates = Array.from({length: 32}, (_, i) => i + 1);
currentRoundCandidates = shuffle(candidates); // shuffle array
nextRoundCandidates = [];
currentPair = [];
updateCurrentPair();
roundNumber = 1;
gameIsOver = false;
loop(); // draw() loop
showMessage = true;
messageStartFrame = frameCount; // save current frame
}
function draw() {
background(220);
textAlign(CENTER, CENTER);
textSize(32);
if (gameIsOver) {
text(nextRoundCandidates[0], width / 2, height / 2)
text("WINNER IS: ", width / 2, height / 2 - 160);
text("CLICK TO RESTART", width / 2, height / 2 + 160);
return;
}
// show current round number
text("ROUND " + roundNumber, width / 2, 55);
text("vs", width / 2, height / 2);
// Check if the currentPair array contains a value before displaying current candidates
if (currentPair.length === 2 && currentPair[0] !== undefined && currentPair[1] !== undefined) {
text(currentPair[0], width * 0.25, height / 2); // show candidate 1
text(currentPair[1], width * 0.75, height / 2); // show candidate 2
}
if (showMessage) {
if (frameCount <= messageStartFrame + messageDuration) {
textSize(20)
text("Game setup complete, game restarted.", width / 2, height - 40);
} else {
showMessage = false; // don not show the message if the message duration time is over
}
}
}
function mousePressed() {
if (gameIsOver) {
gameSetup(); // restart game if it is over
return;
}
if (currentPair.length === 2) {
let winner = (mouseX < width / 2) ? currentPair[0] : currentPair[1];
nextRoundCandidates.push(winner);
if (currentRoundCandidates.length === 0) {
if (nextRoundCandidates.length === 1) { // decide the winner
gameIsOver = true;
return;
}
// Prepare next round
currentRoundCandidates = shuffle(nextRoundCandidates);
nextRoundCandidates = [];
roundNumber++;
}
updateCurrentPair();
}
}
function updateCurrentPair() {
if (currentRoundCandidates.length > 0) {
currentPair = [currentRoundCandidates.shift(), currentRoundCandidates.shift()];
} else {
// If the program went over all the candidates on this round, shuffle the candidates again for the next round
currentRoundCandidates = shuffle(nextRoundCandidates);
nextRoundCandidates = [];
roundNumber++;
if(currentRoundCandidates.length > 0) {
currentPair = [currentRoundCandidates.shift(), currentRoundCandidates.shift()];
}
}
}