From 805bc6e1c7b5519fde54fd5d840584db3f4dd7e9 Mon Sep 17 00:00:00 2001 From: truetype Date: Tue, 16 Jun 2026 18:01:43 +0200 Subject: [PATCH] Fix: separa startNewGame da initGame, flusso corretto per salvataggio e ripristino --- game.js | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/game.js b/game.js index 257606c..629dbc9 100644 --- a/game.js +++ b/game.js @@ -240,6 +240,31 @@ localStorage.removeItem(STORAGE_KEY_STATE); } + function startNewGame() { + // Genera sempre una nuova partita, ignorando stato salvato + clearGameState(); + const size = parseInt(gridSizeSelect.value); + state.size = size; + state.errors = state.maxErrors; + state.catsPlaced = 0; + state.totalCats = size; + state.gameOver = false; + state.won = false; + state.hints = 3; + state.levelStartTime = Date.now(); + state.levelTime = 0; + + const puzzle = generatePuzzle(size); + state.grid = puzzle.grid; + state.solution = puzzle.solution; + + updateUI(); + renderGrid(); + hideModal(); + startTimer(); + saveGameState(); + } + function initGame() { // Prima prova a caricare stato salvato const savedState = loadGameState(); @@ -418,7 +443,7 @@ state.gameOver = true; clearGameState(); showModal('Game Over!', 'Hai esaurito tutti i tentativi. Riprova!', 'Ricomincia', () => { - initGame(); + startNewGame(); }); } return; @@ -441,7 +466,7 @@ showModal('Vittoria!', `Hai completato il livello ${state.level - 1}!\nTempo: ${formatTime(state.levelTime)}\nPunteggio: ${state.score}`, 'Livello Successivo', () => { - initGame(); + startNewGame(); // Nuovo livello, mantiene il nome }); } } @@ -483,10 +508,13 @@ if (state.catsPlaced === state.totalCats) { state.won = true; + stopTimer(); state.score += state.errors * 10 + state.hints * 5; + saveLevelRecord(); + clearGameState(); state.level++; showModal('Vittoria!', 'Hai completato il livello! Punteggio: ' + state.score, 'Livello Successivo', () => { - initGame(); + startNewGame(); }); } return; @@ -641,7 +669,8 @@ state.score = 0; state.hints = 3; state.sessionRecords = []; - clearGameState(); // Cancella stato salvato per nuova partita + clearGameState(); + state.playerName = ''; showNameInput(); }); @@ -676,8 +705,7 @@ playerNameDisplay.textContent = name; } hideNameInput(); - initGame(); - saveGameState(); // Salva subito dopo aver inserito il nome + startNewGame(); // Nuova partita, non initGame che carica stato salvato } }); @@ -689,10 +717,12 @@ // Init - controlla se c'รจ una partita salvata const savedState = loadGameState(); - if (savedState && savedState.playerName && !savedState.gameOver && !savedState.won) { + if (savedState && savedState.playerName && savedState.grid && savedState.grid.length > 0 && !savedState.gameOver && !savedState.won) { // Ripristina partita in corso, salta input nome initGame(); } else { + // Prima partita o stato invalido + clearGameState(); showNameInput(); }