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
@@ -408,74 +408,79 @@
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;
- // 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'
- }
+ 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];
}
- // Add more levels here
};
- // Initialize the puzzle for current level
+ self.reset = function () {
+ self.currentLevel = 1;
+ self.selectedTile = null;
+ self.isComplete = false;
+ self.initPuzzle();
+ };
self.initPuzzle = function () {
- log("Initializing puzzle for level", self.currentLevel);
- // Clear existing grid
- self.grid = [];
- // 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 = [];
+ for (var i = 0; i < 3; i++) {
self.grid[i] = [];
- for (var j = 0; j < self.gridSize; j++) {
+ for (var j = 0; j < 3; 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),
- x = _pos$split$map[0],
- y = _pos$split$map[1];
- self.grid[x][y].setRotation(config.rotations[pos]);
- }
+ };
+ self.moveTile = function (direction) {
+ if (!self.selectedTile) {
+ return;
}
+ var x = self.selectedTile.position.x;
+ var y = self.selectedTile.position.y;
+ var newX = x;
+ var newY = y;
+ // Calculate new position
+ switch (direction) {
+ case 'right':
+ newX++;
+ break;
+ case 'left':
+ newX--;
+ break;
+ case 'down':
+ newY++;
+ break;
+ case 'up':
+ newY--;
+ break;
+ }
+ // Swap tiles
+ var movingTile = self.selectedTile;
+ var emptyTile = self.grid[newY][newX];
+ // Update grid
+ self.grid[newY][newX] = movingTile;
+ self.grid[y][x] = emptyTile;
+ // Update positions
+ movingTile.updatePosition(newX, newY);
+ emptyTile.updatePosition(x, y);
+ // Clear selection
+ self.selectedTile = null;
};
- // Check if the current puzzle is solved
self.checkWinCondition = function () {
// Find start position
var startX = -1,
startY = -1;
- for (var i = 0; i < self.gridSize; i++) {
- for (var j = 0; j < self.gridSize; j++) {
+ 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;
@@ -491,9 +496,9 @@
return this.canReachEnd(startX, startY + 1, visited); // Start flowing down from start
};
self.canReachEnd = function (x, y, visited) {
// Check bounds
- if (x < 0 || x >= self.gridSize || y < 0 || y >= self.gridSize) {
+ if (x < 0 || x >= 3 || y < 0 || y >= 3) {
return false;
}
// Create position key
var key = x + ',' + y;
@@ -539,293 +544,8 @@
}
}
return false;
};
- // Handle tile selection and movement
- 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.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++;
- break;
- case 'left':
- newX--;
- break;
- case 'down':
- newY++;
- break;
- case 'up':
- newY--;
- break;
- }
- // Swap tiles
- var movingTile = self.grid[oldX][oldY];
- var emptyTile = self.grid[newX][newY];
- // Update grid
- self.grid[newX][newY] = movingTile;
- self.grid[oldX][oldY] = emptyTile;
- // Update positions
- movingTile.updatePosition(newX, newY);
- emptyTile.updatePosition(oldX, oldY);
- // Clear selection
- self.selectedTile = null;
- // Check if puzzle is solved
- if (self.checkWinCondition()) {
- self.startWaterFlow();
- levelText.setText('Level: ' + self.currentLevel + ' Solved!');
- isPlaying = false;
- }
- };
- self.reset = function () {
- self.currentLevel = 1;
- self.grid = [];
- self.selectedTile = null;
- self.waterFlowing = false;
- self.initPuzzle();
- };
- self.deselectTile = function () {
- self.selectedTile = null;
- };
- self.rotateTile = function (angle) {
- if (!self.selectedTile) {
- return;
- }
- var tile = self.selectedTile.tile;
- if (angle > Math.PI / 4 && angle < 3 * Math.PI / 4) {
- tile.setRotation('down');
- } else if (angle > -3 * Math.PI / 4 && angle < -Math.PI / 4) {
- tile.setRotation('up');
- } else if (angle <= Math.PI / 4 && angle >= -Math.PI / 4) {
- tile.setRotation('right');
- } else {
- tile.setRotation('left');
- }
- };
- self.startWaterFlow = function () {
- // Find start position
- var startX = -1,
- startY = -1;
- for (var i = 0; i < self.gridSize; i++) {
- for (var j = 0; j < self.gridSize; j++) {
- if (self.grid[i][j].type === 'start') {
- startX = i;
- startY = j;
- break;
- }
- }
- if (startX !== -1) {
- break;
- }
- }
- // Start the flow animation from the start tile
- self.flowThroughPipe(startX, startY, []);
- };
- self.flowThroughPipe = function (x, y, visited) {
- var key = x + ',' + y;
- if (visited.includes(key)) {
- log('Already visited tile at', x, y);
- return;
- }
- visited.push(key);
- var tile = self.grid[x][y];
- if (!tile || tile.type === 'empty') {
- log('Invalid or empty tile at', x, y);
- return;
- }
- log('Flowing through tile:', tile.type, 'at position:', x, y);
- // Enable flow for current tile and set water direction
- tile.flow = true;
- if (tile.water) {
- tile.water.visible = true;
- // Set water direction based on tile type
- switch (tile.type) {
- case 'start':
- case 'straightPipeV':
- //tile.water.dir = 'tb';
- break;
- case 'straightPipeH':
- //tile.water.dir = 'lr';
- break;
- case 'cornerPipe':
- // Set direction based on rotation
- if (tile.rotation === Math.PI * 0.5) {
- //tile.water.dir = 'tr'; // top to right
- }
- // Add other rotation cases here
- break;
- case 'crossPipe':
- // Set direction based on where water came from
- if (self.grid[x][y - 1] && self.grid[x][y - 1].flow) {
- //tile.water.dir = 'tb';
- } else if (self.grid[x - 1][y] && self.grid[x - 1][y].flow) {
- //tile.water.dir = 'lr';
- }
- break;
- }
- log('Set water direction for tile at', x, y, 'to:', tile.water.dir);
- } else {
- log('Warning: No water asset found for tile at', x, y);
- }
- // Wait for flow animation to complete before moving to next tile
- LK.setTimeout(function () {
- // Get next tile position based on current tile type
- var nextPos = self.getNextPosition(x, y, tile);
- if (nextPos) {
- log('Moving to next tile at:', nextPos[0], nextPos[1]);
- self.flowThroughPipe(nextPos[0], nextPos[1], visited);
- } else {
- log('No next position found for tile at', x, y);
- }
- }, 300); // Wait 500ms before flowing to next tile
- };
- self.getNextPosition = function (x, y, tile) {
- log('Getting next position for tile:', tile.type, 'at', x, y);
- switch (tile.type) {
- case 'start':
- log('Start tile - flowing down');
- return [x, y + 1];
- // Flow down from start
- case 'straightPipeV':
- // Always flow downward if we came from above
- if (y > 0 && self.grid[x][y - 1] && self.grid[x][y - 1].flow) {
- log('Vertical pipe - flowing down');
- return [x, y + 1];
- }
- // Flow upward if we came from below
- if (y < self.gridSize - 1 && self.grid[x][y + 1] && self.grid[x][y + 1].flow) {
- log('Vertical pipe - flowing up');
- return [x, y - 1];
- }
- // If no previous flow, default to flowing down
- log('Vertical pipe - default flow down');
- return [x, y + 1];
- case 'straightPipeH':
- // Always flow right if we came from left
- if (x > 0 && self.grid[x - 1][y] && self.grid[x - 1][y].flow) {
- log('Horizontal pipe - flowing right');
- return [x + 1, y];
- }
- // Flow left if we came from right
- if (x < self.gridSize - 1 && self.grid[x + 1][y] && self.grid[x + 1][y].flow) {
- log('Horizontal pipe - flowing left');
- return [x - 1, y];
- }
- // If no previous flow, default to flowing right
- log('Horizontal pipe - default flow right');
- return [x + 1, y];
- case 'cornerPipe':
- // For corner pipe at π/2 (90 degrees), when water comes from above, flow right
- if (Math.abs(tile.rotation - Math.PI * 0.5) < 0.01) {
- // Use approximate equality for floating point
- if (y > 0 && self.grid[x][y - 1] && self.grid[x][y - 1].flow) {
- log('Corner pipe π/2 - water from above, flowing right');
- return [x + 1, y];
- }
- // Default to flowing right if no previous flow (shouldn't happen in normal gameplay)
- log('Corner pipe π/2 - default flowing right');
- return [x + 1, y];
- } else if (Math.abs(tile.rotation - Math.PI) < 0.01) {
- // π (180 degrees)
- if (y > 0 && self.grid[x][y - 1] && self.grid[x][y - 1].flow) {
- log('Corner pipe π - water from above, flowing left');
- return [x - 1, y];
- }
- } else if (Math.abs(tile.rotation - Math.PI * 1.5) < 0.01) {
- // 3π/2 (270 degrees)
- if (y < self.gridSize - 1 && self.grid[x][y + 1] && self.grid[x][y + 1].flow) {
- log('Corner pipe 3π/2 - water from below, flowing left');
- return [x - 1, y];
- }
- } else {
- // 0 or default rotation
- if (x > 0 && self.grid[x - 1][y] && self.grid[x - 1][y].flow) {
- log('Corner pipe 0 - water from left, flowing up');
- return [x, y - 1];
- }
- }
- break;
- case 'crossPipe':
- // For cross pipe, continue in the same direction we came from
- if (y > 0 && self.grid[x][y - 1] && self.grid[x][y - 1].flow) {
- log('Cross pipe - flowing down');
- return [x, y + 1];
- }
- if (y < self.gridSize - 1 && self.grid[x][y + 1] && self.grid[x][y + 1].flow) {
- log('Cross pipe - flowing up');
- return [x, y - 1];
- }
- if (x > 0 && self.grid[x - 1][y] && self.grid[x - 1][y].flow) {
- log('Cross pipe - flowing right');
- return [x + 1, y];
- }
- if (x < self.gridSize - 1 && self.grid[x + 1][y] && self.grid[x + 1][y].flow) {
- log('Cross pipe - flowing left');
- return [x - 1, y];
- }
- break;
- case 'end':
- log('Reached end tile');
- return null;
- }
- log('No valid next position found');
- return null;
- };
return self;
};
/****
* Game Variables
@@ -1019,21 +739,26 @@
game.move = function (x, y, obj) {
if (currentState !== GAME_STATE.PLAYING || !isMouseDown || !puzzleManager || !puzzleManager.selectedTile) {
return;
}
- var dx = x - startX;
- var dy = y - startY;
- var distance = Math.sqrt(dx * dx + dy * dy);
- if (distance > dragThreshold) {
- var angle = Math.atan2(dy, dx);
- puzzleManager.rotateTile(angle);
+ var deltaX = x - startX;
+ var deltaY = y - startY;
+ // Only move if drag distance exceeds threshold
+ if (Math.abs(deltaX) > dragThreshold || Math.abs(deltaY) > dragThreshold) {
+ var direction = null;
+ if (Math.abs(deltaX) > Math.abs(deltaY)) {
+ direction = deltaX > 0 ? 'right' : 'left';
+ } else {
+ direction = deltaY > 0 ? 'down' : 'up';
+ }
+ puzzleManager.moveTile(direction);
isMouseDown = false; // Reset after move
}
};
game.up = function (x, y, obj) {
isMouseDown = false;
if (currentState === GAME_STATE.PLAYING && puzzleManager) {
- puzzleManager.deselectTile();
+ puzzleManager.selectedTile = null;
}
};
/****
* Main Update Loop
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