Initial commit

This commit is contained in:
2026-02-08 01:06:19 +01:00
commit 235fd3022f
25 changed files with 3339 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
import { computed } from 'vue';
import { calculateHints } from '@/utils/puzzleUtils';
export function useHints(solutionGrid) {
const hints = computed(() => calculateHints(solutionGrid.value));
const rowHints = computed(() => hints.value.rowHints);
const colHints = computed(() => hints.value.colHints);
return {
rowHints,
colHints
};
}

View File

@@ -0,0 +1,126 @@
import { ref } from 'vue';
import { usePuzzleStore } from '@/stores/puzzle';
import confetti from 'canvas-confetti';
export function useNonogram() {
const store = usePuzzleStore();
const isDragging = ref(false);
const dragMode = ref(null); // 'fill', 'empty', 'cross'
const startCellState = ref(null);
const startDrag = (r, c, isRightClick = false) => {
if (store.isGameWon) return;
isDragging.value = true;
const current = store.playerGrid[r][c];
if (isRightClick) {
// Right click logic
// If current is 1 (filled), do nothing usually? Or ignore?
// Standard: Toggle 0 <-> 2
if (current === 1) {
dragMode.value = null; // invalid drag start
return;
}
dragMode.value = (current === 2) ? 0 : 2;
} else {
// Left click logic
// Toggle 0 <-> 1. Ignore 2 usually or overwrite?
// Standard: If 2, usually safe to overwrite or ignore. Let's say we toggle 0->1, 1->0.
// If starting on 2, maybe clear it?
if (current === 2) {
dragMode.value = 0; // Clear cross
} else {
dragMode.value = (current === 1) ? 0 : 1;
}
}
// Apply to start cell
applyDrag(r, c);
};
const onMouseEnter = (r, c) => {
if (isDragging.value) {
applyDrag(r, c);
}
};
const stopDrag = () => {
isDragging.value = false;
dragMode.value = null;
checkWinEffect();
};
const applyDrag = (r, c) => {
if (dragMode.value === null) return;
const current = store.playerGrid[r][c];
// Validation:
// Don't overwrite filled (1) with cross (2) directly usually?
// Or don't overwrite cross (2) with filled (1)?
// Simple logic:
// If dragMode is 1 (filling): only fill 0 or 2.
// If dragMode is 0 (clearing): clear 1 or 2.
// If dragMode is 2 (crossing): only cross 0.
let shouldApply = false;
if (dragMode.value === 1) {
if (current !== 1) shouldApply = true;
} else if (dragMode.value === 2) {
if (current === 0) shouldApply = true; // Only cross empty
if (current === 2 && dragMode.value === 0) shouldApply = true; // Clear cross
} else if (dragMode.value === 0) {
// Clearing
if (current !== 0) shouldApply = true;
}
// Simplification for UX: Just force set if valid transition
// But avoid overwriting 1 with 2 if unintended.
// Let's stick to: "Paint with dragMode"
// But protect existing "Opposite" marks if desired.
// For now, simple paint is fine.
store.setCell(r, c, dragMode.value);
};
const checkWinEffect = () => {
if (store.isGameWon) {
triggerConfetti();
}
};
const triggerConfetti = () => {
const duration = 3000;
const end = Date.now() + duration;
(function frame() {
confetti({
particleCount: 5,
angle: 60,
spread: 55,
origin: { x: 0 },
colors: ['#00f2ff', '#ff0055', '#ffffff']
});
confetti({
particleCount: 5,
angle: 120,
spread: 55,
origin: { x: 1 },
colors: ['#00f2ff', '#ff0055', '#ffffff']
});
if (Date.now() < end) {
requestAnimationFrame(frame);
}
}());
};
return {
startDrag,
onMouseEnter,
stopDrag
};
}

View File

@@ -0,0 +1,211 @@
import { ref, computed } from 'vue';
import { usePuzzleStore } from '@/stores/puzzle';
import { calculateHints } from '@/utils/puzzleUtils';
export function useSolver() {
const store = usePuzzleStore();
const isPlaying = ref(false);
const speedIndex = ref(0);
const speeds = [1000, 500, 250, 125];
const speedLabels = ['x1', 'x2', 'x3', 'x4'];
const statusText = ref('Oczekiwanie...');
let intervalId = null;
// --- Core Solver Logic (Human-Like) ---
// Generate all valid line permutations
function getPermutations(length, hints) {
const results = [];
function recurse(index, hintIndex, currentLine) {
// Base case: all hints placed
if (hintIndex === hints.length) {
// Fill rest with 0
while (currentLine.length < length) {
currentLine.push(0);
}
results.push(currentLine);
return;
}
const currentHint = hints[hintIndex];
// Calculate remaining space needed for other hints + gaps
let remainingHintsLen = 0;
for (let i = hintIndex + 1; i < hints.length; i++) remainingHintsLen += hints[i] + 1;
// Available space for current hint start
// Must leave enough space for current hint + remaining
const maxStart = length - remainingHintsLen - currentHint;
for (let start = index; start <= maxStart; start++) {
const newLine = [...currentLine];
// Add padding 0s before hint
for (let k = index; k < start; k++) newLine.push(0);
// Add hint 1s
for (let k = 0; k < currentHint; k++) newLine.push(1);
// Add gap 0 if not last hint
if (hintIndex < hints.length - 1) newLine.push(0);
recurse(newLine.length, hintIndex + 1, newLine);
}
}
recurse(0, 0, []);
return results;
}
// Filter permutations that match current known state (0=empty, 1=filled, 2=cross/empty-known)
// Note: In our store: 0=empty(unknown), 1=filled, 2=cross(known empty).
// In logic: 0=empty, 1=filled.
// So if board has 1, perm must have 1. If board has 2, perm must have 0.
function isValidPermutation(perm, currentLineState) {
for (let i = 0; i < perm.length; i++) {
const boardVal = currentLineState[i];
const permVal = perm[i];
if (boardVal === 1 && permVal !== 1) return false; // Must be filled
if (boardVal === 2 && permVal !== 0) return false; // Must be empty
}
return true;
}
function solveLineLogic(lineState, hints, size) {
// 1. Get all permutations for these hints and size
const allPerms = getPermutations(size, hints);
// 2. Filter by current board state
const validPerms = allPerms.filter(p => isValidPermutation(p, lineState));
if (validPerms.length === 0) return { index: -1 }; // Conflict or error
// 3. Find Intersection
for (let i = 0; i < size; i++) {
// If already known, skip
if (lineState[i] !== 0) continue;
let allOne = true;
let allZero = true;
for (const p of validPerms) {
if (p[i] === 0) allOne = false;
if (p[i] === 1) allZero = false;
if (!allOne && !allZero) break;
}
if (allOne) return { index: i, state: 1 }; // Must be filled
if (allZero) return { index: i, state: 2 }; // Must be empty (cross)
}
return { index: -1 };
}
function step() {
if (store.isGameWon) {
pause();
statusText.value = "Rozwiązane!";
return;
}
const size = store.size;
const { rowHints, colHints } = calculateHints(store.solution);
let madeMove = false;
// Try Rows
for (let r = 0; r < size; r++) {
const rowLine = store.playerGrid[r];
const hints = rowHints[r];
const result = solveLineLogic(rowLine, hints, size);
if (result.index !== -1) {
store.setCell(r, result.index, result.state);
statusText.value = `Logika: Wiersz ${r+1}, Kolumna ${result.index+1} -> ${result.state === 1 ? 'Pełne' : 'Puste'}`;
return;
}
}
// Try Cols
for (let c = 0; c < size; c++) {
const colLine = [];
for(let r=0; r<size; r++) colLine.push(store.playerGrid[r][c]);
const hints = colHints[c];
const result = solveLineLogic(colLine, hints, size);
if (result.index !== -1) {
store.setCell(result.index, c, result.state);
statusText.value = `Logika: Kolumna ${c+1}, Wiersz ${result.index+1} -> ${result.state === 1 ? 'Pełne' : 'Puste'}`;
return;
}
}
// Smart Guess (Fallback)
// Find a cell that is 1 in solution but 0 in grid (or 0 in solution and 0 in grid)
// This is "Cheating" but ensures progress if logic gets stuck (or for large grids where logic is slow)
// Real solver would branch, but for this app we use "Oracle Guess" if logic fails.
if (!madeMove) {
for (let r = 0; r < size; r++) {
for (let c = 0; c < size; c++) {
const current = store.playerGrid[r][c];
const target = store.solution[r][c];
// Check if incorrect or unknown
let isCorrect = false;
if (target === 1 && current === 1) isCorrect = true;
if (target === 0 && current === 2) isCorrect = true;
if (target === 0 && current === 0) isCorrect = false; // Unknown, should be empty
if (target === 1 && current === 0) isCorrect = false; // Unknown, should be filled
if (!isCorrect) {
const newState = (target === 1) ? 1 : 2;
store.setCell(r, c, newState);
statusText.value = `Zgadywanie: Wiersz ${r+1}, Kolumna ${c+1}`;
return;
}
}
}
// If here, puzzle is solved
statusText.value = "Koniec!";
pause();
}
}
function togglePlay() {
if (isPlaying.value) {
pause();
} else {
play();
}
}
function play() {
isPlaying.value = true;
step(); // Immediate step
intervalId = setInterval(step, speeds[speedIndex.value]);
}
function pause() {
isPlaying.value = false;
if (intervalId) clearInterval(intervalId);
intervalId = null;
}
function changeSpeed() {
speedIndex.value = (speedIndex.value + 1) % speeds.length;
if (isPlaying.value) {
pause();
play();
}
}
return {
isPlaying,
speedIndex,
speedLabel: computed(() => speedLabels[speedIndex.value]),
statusText,
step,
togglePlay,
changeSpeed
};
}

View File

@@ -0,0 +1,49 @@
import { ref, onUnmounted } from 'vue';
export function useTimer() {
const time = ref(0);
const timerInterval = ref(null);
const isRunning = ref(false);
const formatTime = (seconds) => {
const m = Math.floor(seconds / 60).toString().padStart(2, '0');
const s = (seconds % 60).toString().padStart(2, '0');
return `${m}:${s}`;
};
const start = () => {
if (isRunning.value) return;
isRunning.value = true;
const startTime = Date.now() - (time.value * 1000);
timerInterval.value = setInterval(() => {
time.value = Math.floor((Date.now() - startTime) / 1000);
}, 1000);
};
const stop = () => {
if (timerInterval.value) {
clearInterval(timerInterval.value);
timerInterval.value = null;
}
isRunning.value = false;
};
const reset = () => {
stop();
time.value = 0;
};
onUnmounted(() => {
stop();
});
return {
time,
isRunning,
start,
stop,
reset,
formatTime
};
}