4 Commits

6 changed files with 87 additions and 30 deletions

4
package-lock.json generated
View File

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

View File

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

View File

@@ -1,5 +1,5 @@
<script setup>
import { ref, computed } from 'vue';
import { ref, computed, onMounted, watch } from 'vue';
import { usePuzzleStore } from '@/stores/puzzle';
import { useI18n } from '@/composables/useI18n';
import { calculateDifficulty } from '@/utils/puzzleUtils';
@@ -12,6 +12,26 @@ const customSize = ref(10);
const fillRate = ref(50);
const errorMsg = ref('');
onMounted(() => {
const savedSize = localStorage.getItem('nonograms_custom_size');
if (savedSize && !isNaN(savedSize)) {
customSize.value = Math.max(5, Math.min(80, Number(savedSize)));
}
const savedFillRate = localStorage.getItem('nonograms_custom_fill_rate');
if (savedFillRate && !isNaN(savedFillRate)) {
fillRate.value = Math.max(10, Math.min(90, Number(savedFillRate)));
}
});
watch(customSize, (newVal) => {
localStorage.setItem('nonograms_custom_size', newVal);
});
watch(fillRate, (newVal) => {
localStorage.setItem('nonograms_custom_fill_rate', newVal);
});
const snapToStep = (value, step) => {
const rounded = Math.round(value / step) * step;
return Math.max(5, Math.min(80, rounded));
@@ -21,12 +41,12 @@ const handleSnap = () => {
customSize.value = snapToStep(Number(customSize.value), 5);
};
const difficultyLevel = computed(() => {
return calculateDifficulty(fillRate.value / 100);
const difficultyInfo = computed(() => {
return calculateDifficulty(fillRate.value / 100, customSize.value);
});
const difficultyColor = computed(() => {
switch(difficultyLevel.value) {
switch(difficultyInfo.value.level) {
case 'extreme': return '#ff3333';
case 'hardest': return '#ff9933';
case 'harder': return '#ffff33';
@@ -77,7 +97,7 @@ const confirm = () => {
v-model="fillRate"
min="10"
max="90"
step="5"
step="1"
/>
<div class="range-scale">
<span>10%</span>
@@ -86,10 +106,9 @@ const confirm = () => {
</div>
<div class="difficulty-indicator">
<span class="label">{{ t('custom.difficulty') }}:</span>
<span class="value" :style="{ color: difficultyColor }">
{{ t(`difficulty.${difficultyLevel}`) }}
</span>
<div class="label">{{ t('custom.difficulty') }}</div>
<div class="level" :style="{ color: difficultyColor }">{{ t(`difficulty.${difficultyInfo.level}`) }}</div>
<div class="percentage" :style="{ color: difficultyColor }">{{ difficultyInfo.value }}%</div>
</div>
<p v-if="errorMsg" class="error">{{ errorMsg }}</p>
@@ -199,12 +218,30 @@ input[type="range"]::-moz-range-thumb {
justify-content: space-between;
color: var(--text-muted);
font-size: 0.85rem;
.difficulty-indicator {
margin: 20px 0 30px 0;
display: flex;
flex-direction: column;
align-items: center;
gap: 5px;
}
.difficulty-indicator {
margin: 20px 0;
font-size: 1.2rem;
display: flex;
.label {
font-size: 1rem;
color: var(--text-muted);
}
.level {
font-size: 1.4rem;
font-weight: bold;
text-transform: uppercase;
line-height: 1.2;
}
.percentage {
font-size: 1rem;
font-weight: bold;
} display: flex;
justify-content: center;
gap: 10px;
align-items: center;

View File

@@ -231,7 +231,8 @@ const buildShareSVG = () => {
// Difficulty Logic
const densityPercent = Math.round(store.currentDensity * 100);
const difficultyKey = calculateDifficulty(store.currentDensity);
const diffInfo = calculateDifficulty(store.currentDensity, store.size);
const difficultyKey = diffInfo.level;
let diffColor = '#33ff33';
if (difficultyKey === 'extreme') diffColor = '#ff3333';
else if (difficultyKey === 'hardest') diffColor = '#ff9933';

View File

@@ -52,24 +52,36 @@ export function generateRandomGrid(size, density = 0.5) {
return grid;
}
export function calculateDifficulty(density) {
export function calculateDifficulty(density, size = 10) {
// 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';
if (density <= 0 || density >= 1) return { level: 'easy', value: 0 };
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)
// Difficulty score combines entropy (complexity) and size (scale)
// We use sqrt(size) to dampen the effect of very large grids,
// ensuring that density still plays a major role.
// Normalized against max size (80)
const sizeFactor = Math.sqrt(size / 80);
const score = entropy * sizeFactor * 100;
const value = Math.round(score);
// Thresholds
let level = 'easy';
if (value >= 80) level = 'extreme';
else if (value >= 60) level = 'hardest';
else if (value >= 40) level = 'harder';
else if (value >= 20) level = 'medium'; // Using 'medium' key if available, or we need to add it?
// Wait, useI18n only has: easy, harder, hardest, extreme.
// Let's stick to those keys but adjust ranges.
if (value >= 75) level = 'extreme';
else if (value >= 50) level = 'hardest';
else if (value >= 25) level = 'harder';
else level = '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';
return { level, value };
}

View File

@@ -180,7 +180,14 @@ const solveLineLogic = (lineState, hints) => {
const len = hints[i];
const starts = [];
for (let start = 0; start <= n - len; start++) {
if (!canPlacePrefix(start, i)) continue;
if (i === 0) {
if (!canPlacePrefix(start, 0)) continue;
} else {
if (start === 0) continue;
if (lineState[start - 1] === 1) continue;
if (!canPlacePrefix(start - 1, i)) continue;
}
if (hasCross(start, start + len)) continue;
if (start + len < n && lineState[start + len] === 1) continue;
const nextPos = start + len < n ? start + len + 1 : start + len;