User prompt
When we click on the box, a random number pops out. After clicking on the box, we can see which number is inside.
User prompt
When we click on the box, the random number inside will appear.
User prompt
Once the boxes are opened, we can see what's inside.
User prompt
Let us see the random numbers in the boxes.
User prompt
Let there be 100 boxes and numbers on the boxes
User prompt
Please fix the bug: 'Uncaught TypeError: setTimeout is not a function' in or related to this line: 'setTimeout(function () {' Line Number: 89
Code edit (1 edits merged)
Please save this source code
User prompt
Prison Box Challenge
Initial prompt
The game will consist of the following: 100 2D prisoners and a room. Inside the room are 100 boxes with rows from 1 to 100. Inside each box will be a random number from 1 to 100.Each number can only be used once, and each prisoner can enter one by one and view the boxes until he finds the number written on it.The prisoner who finds his number goes out, and the new prisoner cannot see inside the boxes. Each prisoner has the chance to try 50 boxes.
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var Box = Container.expand(function (boxNumber, containsNumber) {
var self = Container.call(this);
self.boxNumber = boxNumber;
self.containsNumber = containsNumber;
self.isOpen = false;
self.boxGraphic = self.attachAsset('box', {
anchorX: 0.5,
anchorY: 0.5
});
self.numberText = new Text2(boxNumber.toString(), {
size: 20,
fill: 0xFFFFFF
});
self.numberText.anchor.set(0.5, 0.5);
self.numberText.y = -30;
self.addChild(self.numberText);
self.contentText = new Text2(containsNumber.toString(), {
size: 25,
fill: 0x000000
});
self.contentText.anchor.set(0.5, 0.5);
self.contentText.visible = true;
self.addChild(self.contentText);
self.openBox = function () {
if (!self.isOpen) {
self.isOpen = true;
self.removeChild(self.boxGraphic);
self.boxGraphic = self.attachAsset('boxOpen', {
anchorX: 0.5,
anchorY: 0.5
});
self.contentText.setText(self.containsNumber.toString());
self.contentText.visible = true;
LK.getSound('boxOpen').play();
return self.containsNumber;
}
return null;
};
self.closeBox = function () {
if (self.isOpen) {
self.isOpen = false;
self.removeChild(self.boxGraphic);
self.boxGraphic = self.attachAsset('box', {
anchorX: 0.5,
anchorY: 0.5
});
self.contentText.visible = false;
}
};
self.down = function (x, y, obj) {
if (gameState === 'playing' && !self.isOpen && attemptsLeft > 0) {
var foundNumber = self.openBox();
attemptsLeft--;
attemptsText.setText('Attempts: ' + attemptsLeft);
if (foundNumber === currentPrisoner) {
prisonerFound = true;
LK.getSound('success').play();
successText.setText('Prisoner ' + currentPrisoner + ' found their number!');
successText.visible = true;
LK.setTimeout(function () {
nextPrisoner();
}, 2000);
} else if (attemptsLeft <= 0) {
LK.getSound('failure').play();
failureText.setText('Prisoner ' + currentPrisoner + ' failed!');
failureText.visible = true;
gameState = 'gameOver';
LK.setTimeout(function () {
LK.showGameOver();
}, 2000);
}
}
};
return self;
});
var Prisoner = Container.expand(function (prisonerNumber) {
var self = Container.call(this);
self.prisonerNumber = prisonerNumber;
self.prisonerGraphic = self.attachAsset('prisoner', {
anchorX: 0.5,
anchorY: 1.0
});
self.numberText = new Text2(prisonerNumber.toString(), {
size: 30,
fill: 0xFFFFFF
});
self.numberText.anchor.set(0.5, 0.5);
self.numberText.y = -60;
self.addChild(self.numberText);
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x2F4F4F
});
/****
* Game Code
****/
var totalPrisoners = 100;
var boxes = [];
var prisoners = [];
var currentPrisoner = 1;
var attemptsLeft = Math.floor(totalPrisoners / 2);
var maxAttempts = Math.floor(totalPrisoners / 2);
var gameState = 'setup'; // setup, playing, gameOver, victory
var prisonerFound = false;
var successfulPrisoners = 0;
// UI Elements
var titleText = new Text2('Prison Box Challenge', {
size: 60,
fill: 0xFFFFFF
});
titleText.anchor.set(0.5, 0);
titleText.x = 1024;
titleText.y = 50;
game.addChild(titleText);
var instructionText = new Text2('Each prisoner must find their number in ' + maxAttempts + ' attempts', {
size: 35,
fill: 0xCCCCCC
});
instructionText.anchor.set(0.5, 0);
instructionText.x = 1024;
instructionText.y = 120;
game.addChild(instructionText);
var currentPrisonerText = new Text2('Current Prisoner: 1', {
size: 45,
fill: 0xFFFF00
});
currentPrisonerText.anchor.set(0.5, 0);
currentPrisonerText.x = 1024;
currentPrisonerText.y = 180;
game.addChild(currentPrisonerText);
var attemptsText = new Text2('Attempts: ' + attemptsLeft, {
size: 40,
fill: 0xFFFFFF
});
attemptsText.anchor.set(0.5, 0);
attemptsText.x = 1024;
attemptsText.y = 230;
game.addChild(attemptsText);
var successText = new Text2('', {
size: 40,
fill: 0x00FF00
});
successText.anchor.set(0.5, 0.5);
successText.x = 1024;
successText.y = 1366;
successText.visible = false;
game.addChild(successText);
var failureText = new Text2('', {
size: 40,
fill: 0xFF0000
});
failureText.anchor.set(0.5, 0.5);
failureText.x = 1024;
failureText.y = 1366;
failureText.visible = false;
game.addChild(failureText);
var progressText = new Text2('Prisoners succeeded: 0/' + totalPrisoners, {
size: 35,
fill: 0xFFFFFF
});
progressText.anchor.set(0.5, 0);
progressText.x = 1024;
progressText.y = 280;
game.addChild(progressText);
// Create shuffled array of numbers 1 to totalPrisoners
function shuffleArray(array) {
var shuffled = array.slice();
for (var i = shuffled.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = shuffled[i];
shuffled[i] = shuffled[j];
shuffled[j] = temp;
}
return shuffled;
}
// Initialize the game
function setupGame() {
// Create array of numbers 1 to totalPrisoners
var numbers = [];
for (var i = 1; i <= totalPrisoners; i++) {
numbers.push(i);
}
// Shuffle the numbers
var shuffledNumbers = shuffleArray(numbers);
// Create boxes in a grid
var boxesPerRow = 10;
var startX = 1024 - boxesPerRow * 90 / 2 + 45;
var startY = 350;
for (var i = 0; i < totalPrisoners; i++) {
var row = Math.floor(i / boxesPerRow);
var col = i % boxesPerRow;
var box = new Box(i + 1, shuffledNumbers[i]);
box.x = startX + col * 90;
box.y = startY + row * 90;
boxes.push(box);
game.addChild(box);
}
// Create current prisoner display
var prisoner = new Prisoner(currentPrisoner);
prisoner.x = 200;
prisoner.y = 1200;
prisoners.push(prisoner);
game.addChild(prisoner);
gameState = 'playing';
}
function nextPrisoner() {
// Close all boxes
for (var i = 0; i < boxes.length; i++) {
boxes[i].closeBox();
}
successfulPrisoners++;
progressText.setText('Prisoners succeeded: ' + successfulPrisoners + '/' + totalPrisoners);
if (successfulPrisoners >= totalPrisoners) {
// All prisoners succeeded!
gameState = 'victory';
LK.setScore(100);
LK.setTimeout(function () {
LK.showYouWin();
}, 1000);
return;
}
currentPrisoner++;
attemptsLeft = maxAttempts;
prisonerFound = false;
// Update UI
currentPrisonerText.setText('Current Prisoner: ' + currentPrisoner);
attemptsText.setText('Attempts: ' + attemptsLeft);
successText.visible = false;
failureText.visible = false;
// Update prisoner display
if (prisoners.length > 0) {
prisoners[0].numberText.setText(currentPrisoner.toString());
}
gameState = 'playing';
}
// Start the game
setupGame();
// Game update loop
game.update = function () {
// No continuous updates needed for this turn-based game
}; ===================================================================
--- original.js
+++ change.js
@@ -22,14 +22,14 @@
});
self.numberText.anchor.set(0.5, 0.5);
self.numberText.y = -30;
self.addChild(self.numberText);
- self.contentText = new Text2('', {
+ self.contentText = new Text2(containsNumber.toString(), {
size: 25,
fill: 0x000000
});
self.contentText.anchor.set(0.5, 0.5);
- self.contentText.visible = false;
+ self.contentText.visible = true;
self.addChild(self.contentText);
self.openBox = function () {
if (!self.isOpen) {
self.isOpen = true;