Fix: celle senza colore nel generatore e persistenza stato gioco in localStorage
This commit is contained in:
1 parent
102f74c84a
commit
a5432f7f95
1 file changed
+98
@@ -155,6 +155,30 @@
|
||||
if (!expanded) break;
|
||||
}
|
||||
|
||||
// Fallback: colora eventuali celle rimaste scoperte (-1)
|
||||
// Assegna il colore di una cella adiacente già colorata
|
||||
if (totalFilled < totalCells) {
|
||||
for (let r = 0; r < size; r++) {
|
||||
for (let c = 0; c < size; c++) {
|
||||
if (grid[r][c] === -1) {
|
||||
// Cerca una cella adiacente colorata
|
||||
const dirs = [[0,1],[0,-1],[1,0],[-1,0]];
|
||||
for (const [dr, dc] of dirs) {
|
||||
const nr = r + dr, nc = c + dc;
|
||||
if (nr >= 0 && nr < size && nc >= 0 && nc < size && grid[nr][nc] !== -1) {
|
||||
grid[r][c] = grid[nr][nc];
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Se ancora -1, assegna un colore casuale
|
||||
if (grid[r][c] === -1) {
|
||||
grid[r][c] = colorOrder[Math.floor(Math.random() * colorCount)];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return grid;
|
||||
}
|
||||
|
||||
@@ -180,7 +204,76 @@
|
||||
|
||||
// --- Game Logic ---
|
||||
|
||||
const STORAGE_KEY_STATE = 'catdoku_game_state';
|
||||
|
||||
function saveGameState() {
|
||||
const stateToSave = {
|
||||
size: state.size,
|
||||
grid: state.grid,
|
||||
solution: state.solution,
|
||||
errors: state.errors,
|
||||
catsPlaced: state.catsPlaced,
|
||||
totalCats: state.totalCats,
|
||||
level: state.level,
|
||||
score: state.score,
|
||||
hints: state.hints,
|
||||
gameOver: state.gameOver,
|
||||
won: state.won,
|
||||
playerName: state.playerName,
|
||||
levelStartTime: state.levelStartTime,
|
||||
levelTime: state.levelTime
|
||||
};
|
||||
localStorage.setItem(STORAGE_KEY_STATE, JSON.stringify(stateToSave));
|
||||
}
|
||||
|
||||
function loadGameState() {
|
||||
const saved = localStorage.getItem(STORAGE_KEY_STATE);
|
||||
if (!saved) return null;
|
||||
try {
|
||||
return JSON.parse(saved);
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function clearGameState() {
|
||||
localStorage.removeItem(STORAGE_KEY_STATE);
|
||||
}
|
||||
|
||||
function initGame() {
|
||||
// Prima prova a caricare stato salvato
|
||||
const savedState = loadGameState();
|
||||
|
||||
if (savedState && !savedState.gameOver && !savedState.won) {
|
||||
// Ripristina stato salvato
|
||||
state.size = savedState.size;
|
||||
state.grid = savedState.grid;
|
||||
state.solution = savedState.solution;
|
||||
state.errors = savedState.errors;
|
||||
state.catsPlaced = savedState.catsPlaced;
|
||||
state.totalCats = savedState.totalCats;
|
||||
state.level = savedState.level;
|
||||
state.score = savedState.score;
|
||||
state.hints = savedState.hints;
|
||||
state.gameOver = savedState.gameOver;
|
||||
state.won = savedState.won;
|
||||
state.playerName = savedState.playerName;
|
||||
state.levelStartTime = savedState.levelStartTime;
|
||||
state.levelTime = savedState.levelTime;
|
||||
|
||||
// Ripristina UI
|
||||
if (playerNameDisplay) {
|
||||
playerNameDisplay.textContent = state.playerName;
|
||||
}
|
||||
updateUI();
|
||||
renderGrid();
|
||||
hideModal();
|
||||
hideNameInput();
|
||||
startTimer();
|
||||
return;
|
||||
}
|
||||
|
||||
// Nuova partita
|
||||
const size = parseInt(gridSizeSelect.value);
|
||||
state.size = size;
|
||||
state.errors = state.maxErrors;
|
||||
@@ -310,6 +403,7 @@
|
||||
state.catsPlaced--;
|
||||
renderGrid();
|
||||
updateUI();
|
||||
saveGameState();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -318,9 +412,11 @@
|
||||
state.errors--;
|
||||
flashError(row, col);
|
||||
updateUI();
|
||||
saveGameState();
|
||||
|
||||
if (state.errors <= 0) {
|
||||
state.gameOver = true;
|
||||
clearGameState();
|
||||
showModal('Game Over!', 'Hai esaurito tutti i tentativi. Riprova!', 'Ricomincia', () => {
|
||||
initGame();
|
||||
});
|
||||
@@ -332,6 +428,7 @@
|
||||
state.catsPlaced++;
|
||||
renderGrid();
|
||||
updateUI();
|
||||
saveGameState();
|
||||
|
||||
// Check win
|
||||
if (state.catsPlaced === state.totalCats) {
|
||||
@@ -339,6 +436,7 @@
|
||||
stopTimer();
|
||||
state.score += state.errors * 10 + state.hints * 5;
|
||||
saveLevelRecord();
|
||||
clearGameState();
|
||||
state.level++;
|
||||
showModal('Vittoria!',
|
||||
`Hai completato il livello ${state.level - 1}!\nTempo: ${formatTime(state.levelTime)}\nPunteggio: ${state.score}`,
|
||||
|
||||
Reference in new issue
Block a user