Code edit (1 edits merged)
Please save this source code
User prompt
where we ask you to update the code to make sure gems are inserted correctly. What we want you to do is make sure that new gems are spawned such that they are offset on the Y-axis to be spawned above the board.
Code edit (1 edits merged)
Please save this source code
User prompt
implement "//Insert new gems, if there is empty spaces in the grid."
Code edit (2 edits merged)
Please save this source code
User prompt
Reimplement "Move down all gems into available spaces here." to make it work as it should
Code edit (1 edits merged)
Please save this source code
User prompt
On the board class, create a method that allows us to set the target X and target Y position of a gem based on its col and row values.
User prompt
Implement " //Implement that the below code is repeated until no changed occur"
Code edit (1 edits merged)
Please save this source code
User prompt
Implement "//Move the gem here directly"
Code edit (5 edits merged)
Please save this source code
User prompt
Please fix the bug: 'Script error.' in or related to this line: 'self.grid[gem1.row][gem1.col] = undefined;' Line Number: 77
User prompt
In the code where we ask you to move down all gems into available space here, write the code that would detect if there's an empty spot below a given gem and move it down if that's the case.
Code edit (1 edits merged)
Please save this source code
User prompt
In the check match method on the board, please make sure that matches can both happen horizontally and vertically at the same time.
User prompt
In the checkMatch method, it looks like a gem can only be part of one match. That is not how match tree games work. Any gem can be a member of multiple matches at the same time. Please fix this.
User prompt
In the game down method where we are doing debugging, rather than just logging out the matches, loop over all the matches and set the alpha on the gems that have been matched to half opacity, set all other grid nodes to full opacity.
Code edit (1 edits merged)
Please save this source code
User prompt
updateCheckMatch() method to allow for matches of three or more rather than only three as it is right now.
Code edit (2 edits merged)
Please save this source code
User prompt
Add a method on board that loops over the entire board and determine if there's any free matches on the horizontal or the vertical direction. Create an array of those matches.
Code edit (3 edits merged)
Please save this source code
User prompt
While we are asking to InsetLogic to swap in other directions as well, please make sure that we can swap in both x-directions and in both y-directions.
Code edit (1 edits merged)
Please save this source code
/**** * Classes ****/ // Class for the game board var Board = Container.expand(function () { var self = Container.call(this); self.grid = []; self.rows = 8; self.cols = 8; self.spacing = 10; var mesureGem = new Gem(); self.gemSize = mesureGem.width + self.spacing; self.init = function () { for (var row = 0; row < self.rows; row++) { self.grid[row] = []; for (var col = 0; col < self.cols; col++) { var gem = new Gem(); gem.x = gem.targetX = col * self.gemSize; gem.y = gem.targetY = row * self.gemSize; gem.col = col; gem.row = row; self.grid[row][col] = gem; self.addChild(gem); } } }; // Method to swap two gems self.swapGems = function (gem1, gem2) { var temp = self.grid[gem1.row][gem1.col]; self.grid[gem1.row][gem1.col] = self.grid[gem2.row][gem2.col]; self.grid[gem2.row][gem2.col] = temp; var tempX = gem1.targetX; var tempY = gem1.targetY; var tempRow = gem1.row; var tempCol = gem1.col; gem1.targetX = gem2.targetX; gem1.targetY = gem2.targetY; gem1.row = gem2.row; gem1.col = gem2.col; gem2.targetX = tempX; gem2.targetY = tempY; gem2.row = tempRow; gem2.col = tempCol; }; self.findMatches = function () { var matches = []; for (var row = 0; row < self.rows; row++) { for (var col = 0; col < self.cols; col++) { var match = self.checkMatch(row, col); if (match) { matches = matches.concat(match); } } } return matches; }; self.resolve = function () { var matches = board.findMatches(); // Loop over all the gems on the board for (var row = 0; row < board.rows; row++) { for (var col = 0; col < board.cols; col++) { var gem = board.grid[row][col]; // Check if the gem is part of a match if (matches.some(function (match) { return match.includes(gem); })) { // If it is, set its alpha to half opacity self.grid[gem.row][gem.col] = undefined; gem.destroy(); } else { // If it's not, set its alpha to full opacity //gem.alpha = 1; } } } //Move down all gems into available spaces here. //Implement that the below code is repeated until no changes occur var changes; do { changes = false; for (var row = self.rows - 1; row >= 0; row--) { for (var col = 0; col < self.cols; col++) { if (self.grid[row][col] === undefined) { for (var aboveRow = row - 1; aboveRow >= 0; aboveRow--) { if (self.grid[aboveRow][col] !== undefined) { self.grid[aboveRow][col].targetY += self.gemSize; self.grid[row][col] = self.grid[aboveRow][col]; self.grid[aboveRow][col] = undefined; changes = true; break; } } } } } } while (changes); }; self.checkMatch = function (row, col) { var gem = self.grid[row][col]; var horizontalMatch = [gem]; var verticalMatch = [gem]; // Check for horizontal match for (var i = col + 1; i < self.cols; i++) { if (self.grid[row][i].type === gem.type) { horizontalMatch.push(self.grid[row][i]); } else { break; } } // Check for vertical match for (var i = row + 1; i < self.rows; i++) { if (self.grid[i][col].type === gem.type) { verticalMatch.push(self.grid[i][col]); } else { break; } } // Return match if 3 or more gems of the same type are in a row if (horizontalMatch.length >= 3 || verticalMatch.length >= 3) { var toReturn = []; if (horizontalMatch.length >= 3) { toReturn.push(horizontalMatch); } if (verticalMatch.length >= 3) { toReturn.push(verticalMatch); } return toReturn; } else { return null; } }; }); //<Assets used in the game will automatically appear here> // Class for a single gem var Gem = Container.expand(function () { var self = Container.call(this); self.type = Math.floor(Math.random() * 5); // Random type for the gem self.row = 0; self.col = 0; var gemGraphics; switch (self.type) { case 0: gemGraphics = self.attachAsset('gem1', { anchorX: 0.5, anchorY: 0.5 }); break; case 1: gemGraphics = self.attachAsset('gem2', { anchorX: 0.5, anchorY: 0.5 }); break; case 2: gemGraphics = self.attachAsset('gem3', { anchorX: 0.5, anchorY: 0.5 }); break; case 3: gemGraphics = self.attachAsset('gem4', { anchorX: 0.5, anchorY: 0.5 }); break; case 4: gemGraphics = self.attachAsset('gem5', { anchorX: 0.5, anchorY: 0.5 }); break; } self.targetX = 0; self.targetY = 0; self.update = function () { // Update logic for the gem if needed }; self.down = function (x, y, obj) { // Logic for selecting the gem game.selectGem(self, x, y); }; self.update = function () { if (currentDragNode != self) { self.x += (self.targetX - self.x) / 7; self.y += (self.targetY - self.y) / 7; } }; self.setTargetPosition = function () { self.targetX = self.col * board.gemSize; self.targetY = self.row * board.gemSize; }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ var selectedGem = null; var board = new Board(); board.x = (2048 - board.cols * board.gemSize + board.gemSize + board.spacing) / 2; board.y = (2732 - board.rows * board.gemSize + board.gemSize + board.spacing) / 2; board.init(); game.addChild(board); var currentDragNode = undefined; var currentDragNodeX = 0; var currentDragNodeY = 0; game.selectGem = function (gem, offsetX, offsetY) { currentDragNode = gem; currentDragNodeX = offsetX; currentDragNodeY = offsetY; board.addChild(gem); }; game.move = function (x, y) { if (currentDragNode) { currentDragNode.x = x - board.x - currentDragNodeX; currentDragNode.y = y - board.y - currentDragNodeY; var offsetX = currentDragNode.targetX - currentDragNode.x; var offsetY = currentDragNode.targetY - currentDragNode.y; if (Math.abs(offsetX) > 100) { console.log("Should swap X"); if (offsetX > 0) { if (currentDragNode.col > 0) { var leftGem = board.grid[currentDragNode.row][currentDragNode.col - 1]; board.swapGems(currentDragNode, leftGem); currentDragNode = undefined; return; } } else { if (currentDragNode.col < board.cols - 1) { var rightGem = board.grid[currentDragNode.row][currentDragNode.col + 1]; board.swapGems(currentDragNode, rightGem); currentDragNode = undefined; return; } } } if (Math.abs(offsetY) > 100) { console.log("Should swap Y"); if (offsetY > 0) { if (currentDragNode.row > 0) { var upGem = board.grid[currentDragNode.row - 1][currentDragNode.col]; board.swapGems(currentDragNode, upGem); currentDragNode = undefined; return; } } else { if (currentDragNode.row < board.rows - 1) { var downGem = board.grid[currentDragNode.row + 1][currentDragNode.col]; board.swapGems(currentDragNode, downGem); currentDragNode = undefined; return; } } } } }; game.up = function () { currentDragNode = undefined; }; //Debug method game.down = function () { board.resolve(); };
===================================================================
--- original.js
+++ change.js
@@ -184,8 +184,12 @@
self.x += (self.targetX - self.x) / 7;
self.y += (self.targetY - self.y) / 7;
}
};
+ self.setTargetPosition = function () {
+ self.targetX = self.col * board.gemSize;
+ self.targetY = self.row * board.gemSize;
+ };
});
/****
* Initialize Game