User prompt
log states enter and change and the checkWinCondition
Code edit (1 edits merged)
Please save this source code
User prompt
add logs in checkWinCondition
User prompt
Please fix the bug: 'Uncaught TypeError: Set is not a constructor' in or related to this line: 'return self.canReachEnd(startX, startY, new Set());' Line Number: 676
Code edit (1 edits merged)
Please save this source code
User prompt
Please fix the bug: 'Uncaught TypeError: self.checkWinCondition is not a function' in or related to this line: 'if (self.checkWinCondition()) {' Line Number: 640
Code edit (1 edits merged)
Please save this source code
User prompt
add log in initPuzlle
User prompt
add a log in each initXXXState function
User prompt
add log on states changes
Code edit (1 edits merged)
Please save this source code
User prompt
Please fix the bug: 'Uncaught TypeError: Cannot read properties of undefined (reading 'updatePosition')' in or related to this line: 'emptyTile.updatePosition(x, y);' Line Number: 493
User prompt
Please fix the bug: 'Uncaught TypeError: Cannot read properties of undefined (reading '1')' in or related to this line: 'var baseTileAsset = type == 'start' || type == 'end' || puzzleManager && puzzleManager.levelConfigs[puzzleManager.currentLevel].fixedTiles.includes(x + ',' + y) ? 'baseTile' : 'baseMobileTile';' Line Number: 43
Code edit (1 edits merged)
Please save this source code
User prompt
Please fix the bug: 'Uncaught TypeError: puzzleManager.rotateTile is not a function' in or related to this line: 'puzzleManager.rotateTile(angle);' Line Number: 1033
User prompt
Please fix the bug: 'Uncaught TypeError: puzzleManager.deselectTile is not a function' in or related to this line: 'puzzleManager.deselectTile();' Line Number: 1037
User prompt
Please fix the bug: 'Uncaught TypeError: puzzleManager.reset is not a function' in or related to this line: 'puzzleManager.reset();' Line Number: 916
Code edit (1 edits merged)
Please save this source code
User prompt
Please fix the bug: 'Uncaught TypeError: PuzzleManager is not a constructor' in or related to this line: 'puzzleManager = new PuzzleManager();' Line Number: 439
Code edit (1 edits merged)
Please save this source code
User prompt
Please fix the bug: 'TypeError: gridBoard.hideMessage is not a function' in or related to this line: 'gridBoard.hideMessage();' Line Number: 493
User prompt
Please fix the bug: 'TypeError: gridBoard.hideMessage is not a function' in or related to this line: 'gridBoard.hideMessage();' Line Number: 494
User prompt
Please fix the bug: 'TypeError: gridBoard.hideMessage is not a function' in or related to this line: 'gridBoard.hideMessage();' Line Number: 493
User prompt
Please fix the bug: 'Uncaught ReferenceError: GridBoard is not defined' in or related to this line: 'gridBoard = new GridBoard();' Line Number: 439
Code edit (3 edits merged)
Please save this source code
===================================================================
--- original.js
+++ change.js
@@ -19,9 +19,9 @@
self.rotation = 0;
// Methods
self.setType = function (type, x, y) {
self.type = type;
- var baseTileAsset = type == 'start' || type == 'end' || puzzleManager && puzzleManager.levelConfigs && puzzleManager.levelConfigs[puzzleManager.currentLevel] && puzzleManager.levelConfigs[puzzleManager.currentLevel].fixedTiles.includes(x + ',' + y) ? 'baseTile' : 'baseMobileTile';
+ var baseTileAsset = type == 'start' || type == 'end' || puzzleManager && puzzleManager.levelConfigs[puzzleManager.currentLevel].fixedTiles.includes(x + ',' + y) ? 'baseTile' : 'baseMobileTile';
if (type == 'empty') {
return;
}
// Attach baseTile asset
@@ -404,51 +404,193 @@
/****
* Game Code
****/
+function _slicedToArray(r, e) {
+ return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest();
+}
+function _nonIterableRest() {
+ throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
+}
+function _unsupportedIterableToArray(r, a) {
+ if (r) {
+ if ("string" == typeof r) {
+ return _arrayLikeToArray(r, a);
+ }
+ var t = {}.toString.call(r).slice(8, -1);
+ return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
+ }
+}
+function _arrayLikeToArray(r, a) {
+ (null == a || a > r.length) && (a = r.length);
+ for (var e = 0, n = Array(a); e < a; e++) {
+ n[e] = r[e];
+ }
+ return n;
+}
+function _iterableToArrayLimit(r, l) {
+ var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
+ if (null != t) {
+ var e,
+ n,
+ i,
+ u,
+ a = [],
+ f = !0,
+ o = !1;
+ try {
+ if (i = (t = t.call(r)).next, 0 === l) {
+ if (Object(t) !== t) {
+ return;
+ }
+ f = !1;
+ } else {
+ for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0) {
+ ;
+ }
+ }
+ } catch (r) {
+ o = !0, n = r;
+ } finally {
+ try {
+ if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) {
+ return;
+ }
+ } finally {
+ if (o) {
+ throw n;
+ }
+ }
+ }
+ return a;
+ }
+}
+function _arrayWithHoles(r) {
+ if (Array.isArray(r)) {
+ return r;
+ }
+}
var PuzzleManager = function PuzzleManager() {
var self = this;
// Properties
self.currentLevel = 1;
+ self.maxLevels = 30;
+ self.grid = [];
+ self.gridSize = 4;
self.selectedTile = null;
+ self.waterFlowing = false;
self.isComplete = false;
- // Methods
- self.selectTile = function (x, y) {
- // Convert screen coordinates to grid coordinates
- var gridX = Math.floor((x - boardOffsetX) / tileSize);
- var gridY = Math.floor((y - boardOffsetY) / tileSize);
- // Check if coordinates are within grid bounds
- if (gridX >= 0 && gridX < 3 && gridY >= 0 && gridY < 3) {
- self.selectedTile = self.grid[gridY][gridX];
+ // Level configurations
+ self.levelConfigs = {
+ 1: {
+ tiles: [['start', 'straightPipeV', 'straightPipeV', 'cornerPipe'], [null, null, null, 'straightPipeH'], [null, null, 'straightPipeH', null], [null, null, null, 'end']],
+ rotations: {
+ '0,3': 'left',
+ '3,3': 'left'
+ },
+ fixedTiles: ['0,0', '0,1', '0,2', '0,3', '1,3', '3,3'] // All tiles fixed except 2,2
+ },
+ 2: {
+ tiles: [['start', 'straightPipeH', null, null], [null, 'cornerPipe', null, null], [null, 'straightPipeV', 'cornerPipe', null], [null, null, null, 'end']],
+ rotations: {
+ '0,1': 'right',
+ '1,1': 'down',
+ '2,2': 'left'
+ }
}
+ // Add more levels here
};
- self.reset = function () {
- self.currentLevel = 1;
- self.selectedTile = null;
- self.isComplete = false;
- self.initPuzzle();
- };
self.initPuzzle = function () {
- // Initialize grid
- log('Initializing Puzzle');
+ log("Initializing puzzle for level", self.currentLevel);
+ // Clear existing grid
self.grid = [];
- for (var i = 0; i < 3; i++) {
+ // Get level config
+ var config = self.levelConfigs[self.currentLevel];
+ if (!config) {
+ log("No configuration for level", self.currentLevel);
+ return;
+ }
+ // Initialize grid
+ for (var i = 0; i < self.gridSize; i++) {
self.grid[i] = [];
- for (var j = 0; j < 3; j++) {
+ for (var j = 0; j < self.gridSize; j++) {
var tile = new Tile();
+ if (config.tiles[i] && config.tiles[i][j]) {
+ tile.setType(config.tiles[i][j], i, j);
+ }
+ tile.updatePosition(i, j);
self.grid[i][j] = tile;
game.addChild(tile);
}
}
+ // Apply rotations
+ if (config.rotations) {
+ for (var pos in config.rotations) {
+ var _pos$split$map = pos.split(',').map(Number),
+ _pos$split$map2 = _slicedToArray(_pos$split$map, 2),
+ x = _pos$split$map2[0],
+ y = _pos$split$map2[1];
+ self.grid[x][y].setRotation(config.rotations[pos]);
+ }
+ }
};
- self.moveTile = function (direction) {
+ self.selectTile = function (x, y) {
+ // Convert screen coordinates to grid coordinates
+ if (!isPlaying) {
+ return;
+ }
+ var gridX = Math.floor((x - (gridBoard.x - gridBoard.width / 2 + tileSize / 2)) / tileSize);
+ var gridY = Math.floor((y - (gridBoard.y - gridBoard.height / 2 + tileSize / 2)) / tileSize);
+ // Check if coordinates are within grid
+ if (gridX >= 0 && gridX < self.gridSize && gridY >= 0 && gridY < self.gridSize) {
+ var tile = self.grid[gridX][gridY];
+ var key = gridX + ',' + gridY;
+ // Check if tile exists, is not empty, not start/end, and not fixed
+ if (tile && tile.type !== 'empty' && tile.type !== 'start' && tile.type !== 'end' && !self.levelConfigs[self.currentLevel].fixedTiles.includes(key)) {
+ self.selectedTile = {
+ x: gridX,
+ y: gridY,
+ tile: tile
+ };
+ // Check possible moves
+ self.checkPossibleMoves();
+ }
+ }
+ };
+ self.checkPossibleMoves = function () {
if (!self.selectedTile) {
return;
}
- var x = self.selectedTile.position.x;
- var y = self.selectedTile.position.y;
- var newX = x;
- var newY = y;
+ var x = self.selectedTile.x;
+ var y = self.selectedTile.y;
+ var possibleMoves = [];
+ // Check each direction
+ // Right
+ if (x < self.gridSize - 1 && self.grid[x + 1][y].type === 'empty') {
+ possibleMoves.push('right');
+ }
+ // Left
+ if (x > 0 && self.grid[x - 1][y].type === 'empty') {
+ possibleMoves.push('left');
+ }
+ // Down
+ if (y < self.gridSize - 1 && self.grid[x][y + 1].type === 'empty') {
+ possibleMoves.push('down');
+ }
+ // Up
+ if (y > 0 && self.grid[x][y - 1].type === 'empty') {
+ possibleMoves.push('up');
+ }
+ self.selectedTile.possibleMoves = possibleMoves;
+ };
+ self.moveTile = function (direction) {
+ if (!isPlaying || !self.selectedTile || !self.selectedTile.possibleMoves.includes(direction)) {
+ return;
+ }
+ var oldX = self.selectedTile.x;
+ var oldY = self.selectedTile.y;
+ var newX = oldX;
+ var newY = oldY;
// Calculate new position
switch (direction) {
case 'right':
newX++;
@@ -463,91 +605,32 @@
newY--;
break;
}
// Swap tiles
- var movingTile = self.selectedTile;
- var emptyTile = self.grid[newY][newX];
+ var movingTile = self.grid[oldX][oldY];
+ var emptyTile = self.grid[newX][newY];
// Update grid
- self.grid[newY][newX] = movingTile;
- self.grid[y][x] = emptyTile;
+ self.grid[newX][newY] = movingTile;
+ self.grid[oldX][oldY] = emptyTile;
// Update positions
movingTile.updatePosition(newX, newY);
- if (emptyTile) {
- emptyTile.updatePosition(x, y);
- }
+ emptyTile.updatePosition(oldX, oldY);
// Clear selection
self.selectedTile = null;
- };
- self.checkWinCondition = function () {
- // Find start position
- var startX = -1,
- startY = -1;
- for (var i = 0; i < 3; i++) {
- for (var j = 0; j < 3; j++) {
- if (self.grid[i][j].type === 'start') {
- startX = i;
- startY = j;
- break;
- }
- }
- if (startX !== -1) {
- break;
- }
+ // Check if puzzle is solved
+ if (self.checkWinCondition()) {
+ self.isComplete = true;
+ self.startWaterFlow();
+ levelText.setText('Level: ' + self.currentLevel + ' Solved!');
+ isPlaying = false;
}
- // Use a Set to track visited positions
- var visited = [];
- // Check if we can reach the end tile
- return this.canReachEnd(startX, startY + 1, visited); // Start flowing down from start
};
- self.canReachEnd = function (x, y, visited) {
- // Check bounds
- if (x < 0 || x >= 3 || y < 0 || y >= 3) {
- return false;
- }
- // Create position key
- var key = x + ',' + y;
- if (visited.includes(key)) {
- return false;
- }
- visited.push(key);
- var tile = self.grid[x][y];
- if (!tile || tile.type === 'empty') {
- return false;
- }
- // If we reached the end, success!
- if (tile.type === 'end') {
- return true;
- }
- // Get next possible positions based on current tile type and rotation
- var nextPositions = [];
- switch (tile.type) {
- case 'straightPipeV':
- nextPositions.push([x, y - 1], [x, y + 1]); // Up and down
- break;
- case 'straightPipeH':
- nextPositions.push([x - 1, y], [x + 1, y]); // Left and right
- break;
- case 'cornerPipe':
- // Check rotation to determine flow direction
- if (tile.rotation === Math.PI * 0.5) {
- // Left rotation
- nextPositions.push([x, y - 1], [x + 1, y]); // Up and right
- }
- // Add other rotations as needed
- break;
- case 'crossPipe':
- nextPositions.push([x - 1, y], [x + 1, y], [x, y - 1], [x, y + 1]); // All directions
- break;
- }
- // Try each possible next position
- for (var i = 0; i < nextPositions.length; i++) {
- var nextX = nextPositions[i][0];
- var nextY = nextPositions[i][1];
- if (this.canReachEnd(nextX, nextY, visited)) {
- return true;
- }
- }
- return false;
+ self.reset = function () {
+ self.currentLevel = 1;
+ self.selectedTile = null;
+ self.isComplete = false;
+ self.waterFlowing = false;
+ self.initPuzzle();
};
return self;
};
/****
@@ -609,11 +692,26 @@
}
/****
* Game State Management
****/
+function initializeGame() {
+ // Initialize game assets and variables
+ puzzleManager = new PuzzleManager();
+ // Initialize level text
+ levelText = new Text2("Level 1", {
+ size: 100,
+ fill: "#ffffff"
+ });
+ levelText.x = 2048 / 2;
+ levelText.y = 200;
+ levelText.anchorX = 0.5;
+ // Add the level text to the game
+ game.addChild(levelText);
+ // Transition to menu state
+ changeGameState(GAME_STATE.MENU);
+}
function initMenuState() {
// Show level selection UI
- log('Initializing Menu State');
isPlaying = false;
levelText.visible = true;
levelText.text = "Level 1\nTap to Start";
}
@@ -623,9 +721,8 @@
function cleanMenuState() {
levelText.text = "Level 1";
}
function initNewRoundState() {
- log('Initializing New Round State');
// Reset puzzle manager for new round
if (puzzleManager) {
puzzleManager.reset();
}
@@ -640,9 +737,8 @@
function cleanNewRoundState() {
// Clean up any new round state
}
function initPlayingState() {
- log('Initializing Playing State');
// Start the gameplay
isPlaying = true;
}
function handlePlayingLoop() {
@@ -658,9 +754,8 @@
function cleanPlayingState() {
isPlaying = false;
}
function initScoreState() {
- log('Initializing Score State');
// Show score screen
levelText.visible = true;
levelText.text = "Level Complete!\nTap to continue";
}
@@ -690,9 +785,8 @@
break;
}
// Initialize new state
currentState = newState;
- log('Changing game state from', currentState, 'to', newState);
switch (newState) {
case GAME_STATE.MENU:
initMenuState();
break;
@@ -777,29 +871,12 @@
waterDrops[i].update();
}
}
}
-function initializeGame() {
- gridBoard = LK.getAsset('gridBoard', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- gridBoard.x = 2048 / 2;
- gridBoard.y = 2732 / 2;
- game.addChild(gridBoard);
- // Initialize game assets and variables
- puzzleManager = new PuzzleManager();
- // Initialize level text
- levelText = new Text2("Level 1", {
- size: 100,
- fill: "#ffffff"
- });
- levelText.x = 2048 / 2;
- levelText.y = 200;
- levelText.anchorX = 0.5;
- // Add the level text to the game
- game.addChild(levelText);
- // Transition to menu state
- changeGameState(GAME_STATE.MENU);
-}
+gridBoard = LK.getAsset('gridBoard', {
+ anchorX: 0.5
+});
+gridBoard.x = 2048 / 2;
+gridBoard.y = 2732 / 2;
+game.addChild(gridBoard);
// Start the game
initializeGame();
\ No newline at end of file
straigth zenith view square light wooden pallet. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
straigth zenith view square wooden pallet with big screws in each corner Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
simple yellow rating star. Modern video game style
tileSlide
Sound effect
levelWon
Sound effect
tileBlocked
Sound effect
fountain
Sound effect
waterInPipe
Sound effect
bgMusic
Music
logoBounce
Sound effect
levelStart
Sound effect
bgMusic2
Music
flowerPop
Sound effect
roundResult
Sound effect
gameWon
Sound effect
resetSound
Sound effect
birds
Sound effect
birds2
Sound effect
birds3
Sound effect