6 Commits

8 changed files with 125 additions and 58 deletions

4
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{
"name": "vue-nonograms-solid",
"version": "1.1.0",
"version": "1.3.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "vue-nonograms-solid",
"version": "1.1.0",
"version": "1.3.0",
"dependencies": {
"fireworks-js": "^2.10.8",
"flag-icons": "^7.5.0",

View File

@@ -1,6 +1,6 @@
{
"name": "vue-nonograms-solid",
"version": "1.1.0",
"version": "1.3.0",
"type": "module",
"scripts": {
"dev": "vite",

View File

@@ -2,6 +2,7 @@
import { ref, computed } from 'vue';
import { usePuzzleStore } from '@/stores/puzzle';
import { useI18n } from '@/composables/useI18n';
import { calculateDifficulty } from '@/utils/puzzleUtils';
const emit = defineEmits(['close']);
const store = usePuzzleStore();
@@ -21,13 +22,7 @@ const handleSnap = () => {
};
const difficultyLevel = computed(() => {
const rate = fillRate.value;
const dist = Math.abs(rate - 50);
if (dist <= 5) return 'extreme';
if (dist <= 15) return 'hardest';
if (dist <= 25) return 'harder';
return 'easy';
return calculateDifficulty(fillRate.value / 100);
});
const difficultyColor = computed(() => {

View File

@@ -7,6 +7,7 @@ import { useTimer } from '@/composables/useTimer';
import xIcon from '@/assets/brands/x.svg';
import facebookIcon from '@/assets/brands/facebook.svg';
import whatsappIcon from '@/assets/brands/whatsapp.svg';
import { calculateDifficulty } from '@/utils/puzzleUtils';
const store = usePuzzleStore();
const { t } = useI18n();
@@ -113,12 +114,11 @@ const buildShareCanvas = () => {
// Difficulty & Density Info
const densityPercent = Math.round(store.currentDensity * 100);
const dist = Math.abs(densityPercent - 50);
let difficultyKey = 'easy';
const difficultyKey = calculateDifficulty(store.currentDensity);
let diffColor = '#33ff33';
if (dist <= 5) { difficultyKey = 'extreme'; diffColor = '#ff3333'; }
else if (dist <= 15) { difficultyKey = 'hardest'; diffColor = '#ff9933'; }
else if (dist <= 25) { difficultyKey = 'harder'; diffColor = '#ffff33'; }
if (difficultyKey === 'extreme') diffColor = '#ff3333';
else if (difficultyKey === 'hardest') diffColor = '#ff9933';
else if (difficultyKey === 'harder') diffColor = '#ffff33';
const difficultyText = t(`difficulty.${difficultyKey}`);
ctx.font = '600 14px "Segoe UI", sans-serif';
@@ -212,7 +212,7 @@ const buildShareUrl = (target, text, url) => {
const encodedText = encodeURIComponent(text);
const encodedUrl = encodeURIComponent(url);
if (target === 'x') {
return `https://twitter.com/intent/tweet?text=${encodedText}&url=${encodedUrl}`;
return `https://x.com/intent/tweet?text=${encodedText}&url=${encodedUrl}`;
}
if (target === 'facebook') {
return `https://www.facebook.com/sharer/sharer.php?u=${encodedUrl}&quote=${encodedText}`;
@@ -226,29 +226,48 @@ const buildShareUrl = (target, text, url) => {
const shareTo = async (target) => {
if (shareInProgress.value) return;
shareInProgress.value = true;
const text = shareText.value;
const url = window.location.href;
const shareUrl = buildShareUrl(target, text, url);
try {
const blob = await createShareBlob();
if (!blob) return;
const file = new File([blob], `nonogram-${store.size}x${store.size}.png`, { type: 'image/png' });
const text = shareText.value;
const url = window.location.href;
if (navigator.share && navigator.canShare && navigator.canShare({ files: [file] })) {
await navigator.share({
files: [file],
text,
title: t('app.title'),
url
});
return;
// Try native share first if available (supports images)
if (navigator.share && navigator.canShare) {
const blob = await createShareBlob();
if (blob) {
const file = new File([blob], `nonogram-${store.size}x${store.size}.png`, { type: 'image/png' });
if (navigator.canShare({ files: [file] })) {
await navigator.share({
files: [file],
text,
title: t('app.title'),
url
});
return;
}
}
}
await downloadShareImage();
const shareUrl = buildShareUrl(target, text, url);
if (shareUrl) {
window.open(shareUrl, '_blank', 'noopener');
} catch (error) {
if (error.name === 'AbortError') {
return; // User cancelled native share, do nothing
}
// Other errors -> fall through to fallback
} finally {
shareInProgress.value = false;
}
// Fallback: Direct Link + Download
// Open window immediately if possible (though we awaited above, so it might be blocked,
// but we can't do much about it if we want to try native share first).
// Ideally, for Desktop, navigator.share is undefined so we skip the await above.
if (shareUrl) {
window.open(shareUrl, '_blank', 'noopener');
}
// Trigger download as "screenshot support"
downloadShareImage();
};
onMounted(() => {

View File

@@ -295,9 +295,17 @@ const messages = {
'custom.cancel': '取消',
'custom.start': '开始',
'custom.sizeError': '尺寸必须在 5 到 80 之间!',
'custom.fillRate': '填充率',
'custom.difficulty': '难度',
'difficulty.easy': '简单',
'difficulty.harder': '较难',
'difficulty.hardest': '最难',
'difficulty.extreme': '极限',
'win.title': '恭喜!',
'win.message': '你解开了谜题!',
'win.time': '时间:',
'win.difficulty': '难度:',
'win.usedGuide': '使用指南: {count}',
'win.playAgain': '再玩一次',
'win.shareTitle': '分享你的结果',
'win.shareText': '我在 {time} 内解开了 {size}x{size} 的数织!',
@@ -495,9 +503,17 @@ const messages = {
'custom.cancel': 'Cancelar',
'custom.start': 'Empezar',
'custom.sizeError': '¡El tamaño debe estar entre 5 y 80!',
'custom.fillRate': 'Relleno',
'custom.difficulty': 'Dificultad',
'difficulty.easy': 'Fácil',
'difficulty.harder': 'Más difícil',
'difficulty.hardest': 'El más difícil',
'difficulty.extreme': 'Extremo',
'win.title': '¡FELICIDADES!',
'win.message': '¡Has resuelto el rompecabezas!',
'win.time': 'Tiempo:',
'win.difficulty': 'Dificultad:',
'win.usedGuide': 'Guía usada: {count}',
'win.playAgain': 'Jugar de nuevo',
'win.shareTitle': 'Comparte tu resultado',
'win.shareText': '¡Resolví un nonograma de {size}x{size} en {time}!',
@@ -561,9 +577,17 @@ const messages = {
'custom.cancel': 'Annuler',
'custom.start': 'Démarrer',
'custom.sizeError': 'La taille doit être entre 5 et 80 !',
'custom.fillRate': 'Remplissage',
'custom.difficulty': 'Difficulté',
'difficulty.easy': 'Facile',
'difficulty.harder': 'Plus difficile',
'difficulty.hardest': 'Le plus difficile',
'difficulty.extreme': 'Extrême',
'win.title': 'FÉLICITATIONS !',
'win.message': 'Vous avez résolu le puzzle !',
'win.time': 'Temps:',
'win.difficulty': 'Difficulté :',
'win.usedGuide': 'Guide utilisé : {count}',
'win.playAgain': 'Rejouer',
'win.shareTitle': 'Partagez votre résultat',
'win.shareText': 'Jai résolu un nonogramme {size}x{size} en {time} !',
@@ -627,9 +651,17 @@ const messages = {
'custom.cancel': 'إلغاء',
'custom.start': 'ابدأ',
'custom.sizeError': 'يجب أن يكون الحجم بين 5 و80!',
'custom.fillRate': 'معدل الملء',
'custom.difficulty': 'الصعوبة',
'difficulty.easy': 'سهل',
'difficulty.harder': 'أصعب',
'difficulty.hardest': 'الأصعب',
'difficulty.extreme': 'أقصى',
'win.title': 'تهانينا!',
'win.message': 'لقد حللت اللغز!',
'win.time': 'الوقت:',
'win.difficulty': 'الصعوبة:',
'win.usedGuide': 'تم استخدام الدليل: {count}',
'win.playAgain': 'العب مرة أخرى',
'win.shareTitle': 'شارك نتيجتك',
'win.shareText': 'حللت نونوغرام {size}x{size} في {time}!',
@@ -759,9 +791,17 @@ const messages = {
'custom.cancel': 'Отмена',
'custom.start': 'Старт',
'custom.sizeError': 'Размер должен быть от 5 до 80!',
'custom.fillRate': 'Заполнение',
'custom.difficulty': 'Сложность',
'difficulty.easy': 'Легкий',
'difficulty.harder': 'Сложный',
'difficulty.hardest': 'Очень сложный',
'difficulty.extreme': 'Экстремальный',
'win.title': 'ПОЗДРАВЛЯЕМ!',
'win.message': 'Вы решили головоломку!',
'win.time': 'Время:',
'win.difficulty': 'Сложность:',
'win.usedGuide': 'Подсказок использовано: {count}',
'win.playAgain': 'Сыграть снова',
'win.shareTitle': 'Поделитесь результатом',
'win.shareText': 'Я решил(а) нонограмму {size}x{size} за {time}!',
@@ -958,9 +998,17 @@ const messages = {
'custom.cancel': 'Abbrechen',
'custom.start': 'Start',
'custom.sizeError': 'Die Größe muss zwischen 5 und 80 liegen!',
'custom.fillRate': 'Füllrate',
'custom.difficulty': 'Schwierigkeit',
'difficulty.easy': 'Einfach',
'difficulty.harder': 'Schwerer',
'difficulty.hardest': 'Am schwersten',
'difficulty.extreme': 'Extrem',
'win.title': 'HERZLICHEN GLÜCKWUNSCH!',
'win.message': 'Sie haben das Rätsel gelöst!',
'win.time': 'Zeit:',
'win.difficulty': 'Schwierigkeit:',
'win.usedGuide': 'Hilfe benutzt: {count}',
'win.playAgain': 'Erneut spielen',
'win.shareTitle': 'Teilen Sie Ihr Ergebnis',
'win.shareText': 'Ich habe ein {size}x{size} Nonogramm in {time} gelöst!',

View File

@@ -78,6 +78,9 @@ export function useSolver() {
} else if (type === 'done') {
isProcessing.value = false;
pause();
} else if (type === 'stuck') {
isProcessing.value = false;
pause();
} else {
isProcessing.value = false;
}

View File

@@ -51,3 +51,25 @@ export function generateRandomGrid(size, density = 0.5) {
}
return grid;
}
export function calculateDifficulty(density) {
// Shannon Entropy: H(x) = -x*log2(x) - (1-x)*log2(1-x)
// Normalized to 0-1 range (since max entropy at 0.5 is 1)
// Avoid log(0)
if (density <= 0 || density >= 1) return 'easy';
const entropy = -density * Math.log2(density) - (1 - density) * Math.log2(1 - density);
// Thresholds based on entropy
// 0.5 density -> entropy 1.0 (Extreme)
// 0.4/0.6 density -> entropy ~0.97 (Extreme)
// 0.3/0.7 density -> entropy ~0.88 (Hardest)
// 0.2/0.8 density -> entropy ~0.72 (Harder)
// <0.2/>0.8 density -> entropy <0.72 (Easy)
if (entropy >= 0.96) return 'extreme'; // approx 38% - 62%
if (entropy >= 0.85) return 'hardest'; // approx 28% - 38% & 62% - 72%
if (entropy >= 0.65) return 'harder'; // approx 17% - 28% & 72% - 83%
return 'easy';
}

View File

@@ -5,7 +5,7 @@ const messages = {
'worker.solved': 'Rozwiązane!',
'worker.logicRow': 'Logika: Wiersz {row}, Kolumna {col} -> {state}',
'worker.logicCol': 'Logika: Kolumna {col}, Wiersz {row} -> {state}',
'worker.guess': 'Zgadywanie: Wiersz {row}, Kolumna {col}',
'worker.stuck': 'Brak logicznego ruchu. Spróbuj zgadnąć lub cofnąć.',
'worker.done': 'Koniec!',
'worker.state.filled': 'Pełne',
'worker.state.empty': 'Puste'
@@ -14,7 +14,7 @@ const messages = {
'worker.solved': 'Solved!',
'worker.logicRow': 'Logic: Row {row}, Column {col} -> {state}',
'worker.logicCol': 'Logic: Column {col}, Row {row} -> {state}',
'worker.guess': 'Guessing: Row {row}, Column {col}',
'worker.stuck': 'No logical move found. Try guessing or undoing.',
'worker.done': 'Done!',
'worker.state.filled': 'Filled',
'worker.state.empty': 'Empty'
@@ -236,29 +236,9 @@ const handleStep = (playerGrid, solution, locale) => {
}
}
for (let r = 0; r < size; r++) {
for (let c = 0; c < size; c++) {
const current = playerGrid[r][c];
const target = solution[r][c];
let isCorrect = false;
if (target === 1 && current === 1) isCorrect = true;
if (target === 0 && current === 2) isCorrect = true;
if (target === 0 && current === 0) isCorrect = false;
if (target === 1 && current === 0) isCorrect = false;
if (!isCorrect) {
const newState = target === 1 ? 1 : 2;
return {
type: 'move',
r,
c,
state: newState,
statusText: t(locale, 'worker.guess', { row: r + 1, col: c + 1 })
};
}
}
}
return { type: 'done', statusText: t(locale, 'worker.done') };
// Check for guess logic - we want to avoid this unless strictly necessary
// If no logic move found, return 'stuck' instead of cheating
return { type: 'stuck', statusText: t(locale, 'worker.stuck') };
};
self.onmessage = (event) => {