Commit 4b85df29 authored by Nguyễn Quang Sáng's avatar Nguyễn Quang Sáng

05/10/23 commit

parent 630b55b0
This diff is collapsed.
...@@ -269,6 +269,7 @@ export default class GameController extends cc.Component { ...@@ -269,6 +269,7 @@ export default class GameController extends cc.Component {
this.currentBlock.setPosition(cc.Vec2.ZERO); this.currentBlock.setPosition(cc.Vec2.ZERO);
cellToMerge.removeAllChildren(); cellToMerge.removeAllChildren();
cellToMerge.addChild(this.currentBlock); cellToMerge.addChild(this.currentBlock);
this.currentBlock.getComponent(Block).isPlace = true;
this.blocksData.setBlockValue( this.blocksData.setBlockValue(
this.currentBlockRow, this.currentBlockRow,
...@@ -306,7 +307,7 @@ export default class GameController extends cc.Component { ...@@ -306,7 +307,7 @@ export default class GameController extends cc.Component {
.call(() => { .call(() => {
nodeClone.destroy(); nodeClone.destroy();
nodeClone.parent.removeAllChildren(); nodeClone.parent.removeAllChildren();
cellToMerge.children[0].getComponent(Block).setSpriteBlock(index); this.currentBlock.getComponent(Block).setSpriteBlock(index);
this.cachedBlocksToMerge.length = 0; this.cachedBlocksToMerge.length = 0;
if (!hasCheckedEmptyCell) { if (!hasCheckedEmptyCell) {
...@@ -340,7 +341,7 @@ export default class GameController extends cc.Component { ...@@ -340,7 +341,7 @@ export default class GameController extends cc.Component {
newRow = currentRow; newRow = currentRow;
newCol = col; newCol = col;
return this.updateBoardChildren(newRow, newCol); return this.checkMergeability(newRow, newCol);
} else { } else {
break; break;
} }
...@@ -355,39 +356,35 @@ export default class GameController extends cc.Component { ...@@ -355,39 +356,35 @@ export default class GameController extends cc.Component {
} }
} }
updateBoardChildren(row: number, col: number) { // updateBoardChildren() {
const data = this.blocksData.blockData; // const data = this.blocksData.blockData;
const numRows = data.length; // const childrens = this.board.children;
const numCols = data[0].length;
const children = this.board.children;
for (let row = 0; row < numRows; row++) {
for (let col = 0; col < numCols; col++) {
const index = row * numCols + col;
const cell = children[index];
const value = data[row][col];
cell.removeAllChildren(); // for (let row = 0; row < 6; row++) {
// for (let col = 0; col < 5; col++) {
// const index = row * 5 + col;
// const cell = childrens[index];
// const value = data[row][col];
if (value !== 0) { // cell.removeAllChildren();
const newValueNode = this.createValueNode(value);
cell.addChild(newValueNode);
if (cell.children.length > 1) cell.children.shift();
}
}
}
this.checkMergeability(row, col); // if (value !== 0) {
} // const newValueNode = this.createValueNode(value);
// cell.addChild(newValueNode);
// if (cell.children.length > 1) cell.children.shift();
// }
// }
// }
// }
createValueNode(value: number) { // createValueNode(value: number) {
const newNode = new cc.Node(); // const newNode = new cc.Node();
newNode.addComponent(cc.Sprite).spriteFrame = // newNode.addComponent(cc.Sprite).spriteFrame =
this.blockPrefab.data.getComponent(Block).listSpriteBlocks[ // this.blockPrefab.data.getComponent(Block).listSpriteBlocks[
Math.log(value) / Math.log(2) - 1 // Math.log(value) / Math.log(2) - 1
]; // ];
return newNode; // return newNode;
} // }
} }
import Utils from "../Tools/Utils";
const { ccclass, property } = cc._decorator; const { ccclass, property } = cc._decorator;
@ccclass @ccclass
...@@ -39,27 +37,35 @@ export default class BlockData extends cc.Component { ...@@ -39,27 +37,35 @@ export default class BlockData extends cc.Component {
} }
generateNumber(): number { generateNumber(): number {
if (this.blockData.length == 0) return Utils.random(0, 4); if (this.blockData.length == 0) return 0;
const allValues: number[] = [];
for (let row = 0; row < 6; row++) {
for (let col = 0; col < 5; col++) {
allValues.push(this.blockData[row][col]);
}
}
const smallNumberProbability = 0.7; const higherValueCount = allValues.filter((value) => value >= 32).length;
const largeNumberProbability = 0.3; const totalValueCount = allValues.length;
const higherValueProbability = higherValueCount / totalValueCount;
let randomNumber = 0; const random = Math.random();
const randomValue = Math.random();
if (randomValue < smallNumberProbability) { if (random < higherValueProbability) {
const smallNumbers = [2, 4, 8, 16, 32]; const minExponent = 5;
const randomIndex = Math.floor(Math.random() * smallNumbers.length); const maxExponent = 9;
randomNumber = smallNumbers[randomIndex];
} else if (randomValue < smallNumberProbability + largeNumberProbability) { const exponent =
const largeNumbers = [64, 128, 256, 512]; Math.floor(Math.random() * (maxExponent - minExponent + 1)) +
const randomIndex = Math.floor(Math.random() * largeNumbers.length); minExponent;
randomNumber = largeNumbers[randomIndex]; const randomValue = Math.pow(2, exponent);
return Math.log(randomValue) / Math.log(2) - 1;
} else { } else {
const averageNumbers = [16, 32, 64]; const possibleValues = [2, 4, 8, 16];
const randomIndex = Math.floor(Math.random() * averageNumbers.length); const randomValue =
randomNumber = averageNumbers[randomIndex]; possibleValues[Math.floor(Math.random() * possibleValues.length)];
return Math.log(randomValue) / Math.log(2) - 1;
} }
return Math.log(randomNumber) / Math.log(2) - 1;
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment