Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 53ef63f25f | |||
| 9b869bdb5f |
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "vue-nonograms-solid",
|
"name": "vue-nonograms-solid",
|
||||||
"version": "1.8.4",
|
"version": "1.8.5",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "vue-nonograms-solid",
|
"name": "vue-nonograms-solid",
|
||||||
"version": "1.8.4",
|
"version": "1.8.5",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"fireworks-js": "^2.10.8",
|
"fireworks-js": "^2.10.8",
|
||||||
"flag-icons": "^7.5.0",
|
"flag-icons": "^7.5.0",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "vue-nonograms-solid",
|
"name": "vue-nonograms-solid",
|
||||||
"version": "1.8.4",
|
"version": "1.8.5",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "vite",
|
"dev": "vite",
|
||||||
|
|||||||
@@ -44,6 +44,7 @@ export function useNonogram() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const stopDrag = () => {
|
const stopDrag = () => {
|
||||||
|
store.endInteraction();
|
||||||
isDragging.value = false;
|
isDragging.value = false;
|
||||||
dragMode.value = null;
|
dragMode.value = null;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -75,6 +75,7 @@ export const usePuzzleStore = defineStore('puzzle', () => {
|
|||||||
|
|
||||||
// History for undo
|
// History for undo
|
||||||
const history = ref([]);
|
const history = ref([]);
|
||||||
|
const currentTransaction = ref(null);
|
||||||
|
|
||||||
// Progress State
|
// Progress State
|
||||||
const totalCellsToFill = computed(() => {
|
const totalCellsToFill = computed(() => {
|
||||||
@@ -150,18 +151,39 @@ export const usePuzzleStore = defineStore('puzzle', () => {
|
|||||||
playerGrid.value = Array(size.value).fill().map(() => Array(size.value).fill(0));
|
playerGrid.value = Array(size.value).fill().map(() => Array(size.value).fill(0));
|
||||||
moves.value = 0;
|
moves.value = 0;
|
||||||
history.value = [];
|
history.value = [];
|
||||||
|
currentTransaction.value = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function pushHistory() {
|
function startInteraction() {
|
||||||
const gridCopy = playerGrid.value.map(row => [...row]);
|
currentTransaction.value = [];
|
||||||
history.value.push(gridCopy);
|
}
|
||||||
|
|
||||||
|
function endInteraction() {
|
||||||
|
if (currentTransaction.value && currentTransaction.value.length > 0) {
|
||||||
|
history.value.push(currentTransaction.value);
|
||||||
if (history.value.length > 50) history.value.shift();
|
if (history.value.length > 50) history.value.shift();
|
||||||
|
saveState();
|
||||||
|
}
|
||||||
|
currentTransaction.value = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function undo() {
|
function undo() {
|
||||||
if (history.value.length === 0 || isGameWon.value) return;
|
if (history.value.length === 0 || isGameWon.value) return;
|
||||||
const previousState = history.value.pop();
|
|
||||||
playerGrid.value = previousState;
|
const transaction = history.value.pop();
|
||||||
|
|
||||||
|
// Handle legacy history (full grid snapshot)
|
||||||
|
if (!Array.isArray(transaction) || (transaction.length > 0 && Array.isArray(transaction[0]))) {
|
||||||
|
playerGrid.value = transaction;
|
||||||
|
} else {
|
||||||
|
// Handle new history (list of changes)
|
||||||
|
// Revert changes in reverse order
|
||||||
|
for (let i = transaction.length - 1; i >= 0; i--) {
|
||||||
|
const { r, c, oldVal } = transaction[i];
|
||||||
|
playerGrid.value[r][c] = oldVal;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
moves.value++;
|
moves.value++;
|
||||||
saveState();
|
saveState();
|
||||||
}
|
}
|
||||||
@@ -169,8 +191,6 @@ export const usePuzzleStore = defineStore('puzzle', () => {
|
|||||||
function toggleCell(r, c, isRightClick = false) {
|
function toggleCell(r, c, isRightClick = false) {
|
||||||
if (isGameWon.value) return;
|
if (isGameWon.value) return;
|
||||||
|
|
||||||
pushHistory();
|
|
||||||
|
|
||||||
const currentState = playerGrid.value[r][c];
|
const currentState = playerGrid.value[r][c];
|
||||||
let newState;
|
let newState;
|
||||||
|
|
||||||
@@ -182,20 +202,48 @@ export const usePuzzleStore = defineStore('puzzle', () => {
|
|||||||
newState = currentState === 1 ? 0 : 1;
|
newState = currentState === 1 ? 0 : 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
playerGrid.value[r][c] = newState; // This triggers reactivity
|
if (currentState === newState) return;
|
||||||
|
|
||||||
|
// Apply change
|
||||||
|
playerGrid.value[r][c] = newState;
|
||||||
|
|
||||||
|
// Record history
|
||||||
|
const change = { r, c, oldVal: currentState, newVal: newState };
|
||||||
|
if (currentTransaction.value) {
|
||||||
|
currentTransaction.value.push(change);
|
||||||
|
} else {
|
||||||
|
// Atomic change if no interaction started
|
||||||
|
history.value.push([change]);
|
||||||
|
if (history.value.length > 50) history.value.shift();
|
||||||
|
saveState();
|
||||||
|
}
|
||||||
|
|
||||||
moves.value++;
|
moves.value++;
|
||||||
checkWin();
|
checkWin();
|
||||||
saveState();
|
// saveState(); // Moved to endInteraction or atomic block
|
||||||
}
|
}
|
||||||
|
|
||||||
function setCell(r, c, state) {
|
function setCell(r, c, state) {
|
||||||
if (isGameWon.value) return;
|
if (isGameWon.value) return;
|
||||||
if (playerGrid.value[r][c] !== state) {
|
const currentState = playerGrid.value[r][c];
|
||||||
pushHistory();
|
|
||||||
|
if (currentState !== state) {
|
||||||
|
// Apply change
|
||||||
playerGrid.value[r][c] = state;
|
playerGrid.value[r][c] = state;
|
||||||
|
|
||||||
|
// Record history
|
||||||
|
const change = { r, c, oldVal: currentState, newVal: state };
|
||||||
|
if (currentTransaction.value) {
|
||||||
|
currentTransaction.value.push(change);
|
||||||
|
} else {
|
||||||
|
history.value.push([change]);
|
||||||
|
if (history.value.length > 50) history.value.shift();
|
||||||
|
saveState();
|
||||||
|
}
|
||||||
|
|
||||||
moves.value++;
|
moves.value++;
|
||||||
checkWin();
|
checkWin();
|
||||||
saveState();
|
// saveState(); // Moved to endInteraction or atomic block
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -343,7 +391,9 @@ export const usePuzzleStore = defineStore('puzzle', () => {
|
|||||||
hasUsedGuide,
|
hasUsedGuide,
|
||||||
guideUsageCount,
|
guideUsageCount,
|
||||||
currentDensity,
|
currentDensity,
|
||||||
markGuideUsed
|
markGuideUsed,
|
||||||
|
startInteraction,
|
||||||
|
endInteraction
|
||||||
};
|
};
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user