787 lines
27 KiB
HTML
787 lines
27 KiB
HTML
<html>
|
|
<head>
|
|
<title>Sudoku</title>
|
|
<style>
|
|
:root {
|
|
--primary-color: dodgerblue;
|
|
--bg-color: whitesmoke;
|
|
--board-bg: white;
|
|
--border-color: lightgray;
|
|
--thick-border: darkslategray;
|
|
--text-color: black;
|
|
--same-highlight: lightblue;
|
|
--selected-border: blue;
|
|
--error-bg: pink;
|
|
--error-text: red;
|
|
}
|
|
|
|
body {
|
|
font-family: 'Roboto', 'Segoe UI', sans-serif;
|
|
background-color: var(--bg-color);
|
|
color: var(--text-color);
|
|
margin: 0;
|
|
padding: 0;
|
|
height: 100vh;
|
|
display: flex;
|
|
justify-content: center;
|
|
align-items: center;
|
|
overflow: hidden;
|
|
}
|
|
|
|
.screen {
|
|
display: none;
|
|
flex-direction: column;
|
|
align-items: center;
|
|
justify-content: center;
|
|
width: 100%;
|
|
height: 100%;
|
|
position: absolute;
|
|
background-color: var(--bg-color);
|
|
animation: fadeIn 0.3s ease-in-out;
|
|
}
|
|
|
|
.screen.active { display: flex; }
|
|
|
|
@keyframes fadeIn {
|
|
from { opacity: 0; transform: translateY(10px); }
|
|
to { opacity: 1; transform: translateY(0); }
|
|
}
|
|
|
|
.logo-img {
|
|
width: 120px;
|
|
height: 120px;
|
|
border-radius: 20px;
|
|
margin-bottom: 15px;
|
|
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
|
|
}
|
|
|
|
.app-title {
|
|
font-size: 32px;
|
|
font-weight: bold;
|
|
color: darkslategray;
|
|
margin-bottom: 40px;
|
|
}
|
|
|
|
.btn-main {
|
|
background-color: var(--primary-color);
|
|
color: white;
|
|
font-size: 18px;
|
|
font-weight: bold;
|
|
padding: 15px 0;
|
|
width: 250px;
|
|
border: none;
|
|
border-radius: 30px;
|
|
cursor: pointer;
|
|
box-shadow: 0 4px 10px lightgray;
|
|
transition: 0.2s;
|
|
margin-bottom: 15px;
|
|
text-align: center;
|
|
}
|
|
.btn-main:hover { transform: scale(1.05); }
|
|
|
|
.level-title {
|
|
font-size: 24px;
|
|
margin-bottom: 30px;
|
|
color: black;
|
|
}
|
|
|
|
.btn-level {
|
|
width: 250px;
|
|
padding: 15px;
|
|
margin-bottom: 15px;
|
|
border: none;
|
|
border-radius: 15px;
|
|
font-size: 18px;
|
|
font-weight: bold;
|
|
color: white;
|
|
cursor: pointer;
|
|
transition: 0.2s;
|
|
box-shadow: 0 4px 6px lightgray;
|
|
}
|
|
.btn-level:hover { transform: translateY(-3px); }
|
|
|
|
.lvl-easy { background-color: limegreen; }
|
|
.lvl-medium { background-color: dodgerblue; }
|
|
.lvl-hard { background-color: red; }
|
|
|
|
.btn-cancel {
|
|
margin-top: 20px;
|
|
background: none;
|
|
border: 2px solid gray;
|
|
color: gray;
|
|
padding: 10px 40px;
|
|
border-radius: 20px;
|
|
cursor: pointer;
|
|
font-weight: bold;
|
|
}
|
|
|
|
.game-header {
|
|
width: 100%;
|
|
max-width: 380px;
|
|
display: flex;
|
|
justify-content: space-between;
|
|
align-items: center;
|
|
margin-bottom: 15px;
|
|
padding: 0 10px;
|
|
box-sizing: border-box;
|
|
}
|
|
|
|
.btn-back-icon {
|
|
background: none;
|
|
border: none;
|
|
font-size: 28px;
|
|
cursor: pointer;
|
|
color: dimgray;
|
|
padding: 0;
|
|
}
|
|
|
|
.header-center {
|
|
display: flex;
|
|
flex-direction: column;
|
|
align-items: center;
|
|
}
|
|
|
|
#difficulty-label {
|
|
font-size: 16px;
|
|
font-weight: bold;
|
|
color: var(--primary-color);
|
|
text-transform: uppercase;
|
|
}
|
|
|
|
#timer-label {
|
|
font-size: 20px;
|
|
font-weight: bold;
|
|
color: #333;
|
|
font-family: monospace;
|
|
margin-top: 2px;
|
|
}
|
|
|
|
#mistake-label {
|
|
font-size: 14px;
|
|
font-weight: bold;
|
|
color: red;
|
|
}
|
|
|
|
#board-container {
|
|
box-shadow: 0 4px 15px lightgray;
|
|
border-radius: 8px;
|
|
overflow: hidden;
|
|
margin-bottom: 20px;
|
|
}
|
|
|
|
#board {
|
|
width: 350px;
|
|
height: 350px;
|
|
background-color: var(--board-bg);
|
|
border: 2px solid var(--thick-border);
|
|
display: flex;
|
|
flex-wrap: wrap;
|
|
}
|
|
|
|
.tile {
|
|
width: 38.4px;
|
|
height: 38.4px;
|
|
border: 1px solid var(--border-color);
|
|
font-size: 20px;
|
|
font-weight: 500;
|
|
display: flex;
|
|
justify-content: center;
|
|
align-items: center;
|
|
cursor: pointer;
|
|
user-select: none;
|
|
box-sizing: border-box;
|
|
}
|
|
|
|
.tile-border-right { border-right: 2px solid var(--thick-border); }
|
|
.tile-border-bottom { border-bottom: 2px solid var(--thick-border); }
|
|
|
|
.tile-start {
|
|
color: black;
|
|
background-color: whitesmoke;
|
|
cursor: default;
|
|
font-weight: bold;
|
|
}
|
|
|
|
.tile-user { color: var(--primary-color); }
|
|
.tile-highlight-same { background-color: var(--same-highlight) !important; }
|
|
.tile-selected { border: 3px solid var(--selected-border) !important; z-index: 10; }
|
|
.tile-error { background-color: var(--error-bg) !important; color: var(--error-text) !important; }
|
|
|
|
#numpad-row {
|
|
display: flex;
|
|
flex-wrap: nowrap;
|
|
justify-content: space-between;
|
|
width: 100%;
|
|
max-width: 350px;
|
|
gap: 5px;
|
|
}
|
|
|
|
.number {
|
|
flex: 1;
|
|
height: 55px;
|
|
border-radius: 8px;
|
|
display: flex;
|
|
flex-direction: column;
|
|
justify-content: center;
|
|
align-items: center;
|
|
cursor: pointer;
|
|
background-color: white;
|
|
color: var(--primary-color);
|
|
box-shadow: 0 2px 5px lightgray;
|
|
transition: transform 0.1s;
|
|
}
|
|
.number:active { transform: scale(0.95); background-color: aliceblue; }
|
|
.main-num { font-size: 22px; font-weight: bold; }
|
|
.count-num { font-size: 10px; color: gray; margin-top: 2px; }
|
|
.hidden-key { opacity: 0; pointer-events: none; }
|
|
|
|
#tools-row {
|
|
margin-top: 15px;
|
|
width: 100%;
|
|
max-width: 350px;
|
|
display: flex;
|
|
justify-content: center;
|
|
gap: 15px;
|
|
}
|
|
|
|
.tool-btn {
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
gap: 8px;
|
|
padding: 12px 20px;
|
|
border-radius: 30px;
|
|
font-weight: bold;
|
|
font-size: 14px;
|
|
cursor: pointer;
|
|
border: 1px solid lightgray;
|
|
background-color: white;
|
|
box-shadow: 0 2px 4px lightgray;
|
|
transition: 0.2s;
|
|
flex: 1;
|
|
}
|
|
.tool-btn:active { transform: scale(0.95); }
|
|
.btn-delete { color: red; border-color: pink; }
|
|
.btn-hint { color: darkorange; border-color: orange; }
|
|
.btn-disabled {
|
|
background-color: lightgray !important;
|
|
color: gray !important;
|
|
border-color: darkgray !important;
|
|
cursor: not-allowed;
|
|
box-shadow: none;
|
|
}
|
|
|
|
#game-msg {
|
|
height: 20px;
|
|
margin-bottom: 5px;
|
|
}
|
|
|
|
.modal-overlay {
|
|
display: none;
|
|
position: fixed;
|
|
top: 0;
|
|
left: 0;
|
|
width: 100%;
|
|
height: 100%;
|
|
background-color: rgba(0, 0, 0, 0.6);
|
|
z-index: 100;
|
|
justify-content: center;
|
|
align-items: center;
|
|
animation: fadeIn 0.3s;
|
|
}
|
|
|
|
.modal-content {
|
|
background-color: white;
|
|
width: 80%;
|
|
max-width: 300px;
|
|
padding: 30px;
|
|
border-radius: 20px;
|
|
text-align: center;
|
|
box-shadow: 0 10px 25px rgba(0,0,0,0.3);
|
|
animation: popIn 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
|
|
}
|
|
|
|
.modal-icon {
|
|
font-size: 50px;
|
|
margin-bottom: 10px;
|
|
}
|
|
|
|
.modal-title {
|
|
font-size: 24px;
|
|
font-weight: bold;
|
|
color: #333;
|
|
margin-bottom: 10px;
|
|
}
|
|
|
|
.modal-desc {
|
|
font-size: 14px;
|
|
color: gray;
|
|
margin-bottom: 25px;
|
|
line-height: 1.5;
|
|
}
|
|
|
|
.win-stats {
|
|
display: flex;
|
|
justify-content: space-around;
|
|
margin-bottom: 25px;
|
|
background-color: #f0f8ff;
|
|
padding: 10px;
|
|
border-radius: 10px;
|
|
}
|
|
.stat-item { display: flex; flex-direction: column; }
|
|
.stat-label { font-size: 10px; color: gray; text-transform: uppercase; }
|
|
.stat-value { font-size: 18px; font-weight: bold; color: var(--primary-color); }
|
|
|
|
.btn-modal {
|
|
display: block;
|
|
width: 100%;
|
|
padding: 12px 0;
|
|
border-radius: 25px;
|
|
font-size: 16px;
|
|
font-weight: bold;
|
|
border: none;
|
|
cursor: pointer;
|
|
margin-bottom: 10px;
|
|
transition: 0.2s;
|
|
}
|
|
|
|
.btn-primary-modal {
|
|
background-color: var(--primary-color);
|
|
color: white;
|
|
box-shadow: 0 4px 10px rgba(30, 144, 255, 0.4);
|
|
}
|
|
.btn-secondary-modal {
|
|
background-color: white;
|
|
color: var(--primary-color);
|
|
border: 2px solid var(--primary-color);
|
|
}
|
|
.btn-grey-modal {
|
|
background-color: #e0e0e0;
|
|
color: #555;
|
|
}
|
|
|
|
@keyframes popIn {
|
|
from { transform: scale(0.8); opacity: 0; }
|
|
to { transform: scale(1); opacity: 1; }
|
|
}
|
|
|
|
</style>
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<div id="screen-menu" class="screen active">
|
|
<img src="Sudoku icon.jpg" alt="Logo" class="logo-img">
|
|
<div class="app-title">Sudoku</div>
|
|
<button class="btn-main" onclick="showLevelScreen()">Permainan Baru</button>
|
|
</div>
|
|
|
|
<div id="screen-level" class="screen">
|
|
<div class="level-title">Pilih Tingkat Kesulitan</div>
|
|
<button class="btn-level lvl-easy" onclick="startLevel('easy')">MUDAH<div style="font-size: 12px; font-weight: normal; margin-top: 5px;">Santai & Cepat</div></button>
|
|
<button class="btn-level lvl-medium" onclick="startLevel('medium')">MEDIUM<div style="font-size: 12px; font-weight: normal; margin-top: 5px;">Tantangan Standar</div></button>
|
|
<button class="btn-level lvl-hard" onclick="startLevel('hard')">SULIT<div style="font-size: 12px; font-weight: normal; margin-top: 5px;">Butuh Logika Ekstra</div></button>
|
|
<button class="btn-cancel" onclick="showMenuScreen()">Batal</button>
|
|
</div>
|
|
|
|
<div id="screen-game" class="screen">
|
|
<div class="game-header">
|
|
<button class="btn-back-icon" onclick="backToMenu()">←</button>
|
|
|
|
<div class="header-center">
|
|
<div id="difficulty-label">MEDIUM</div>
|
|
<div id="timer-label">00:00</div>
|
|
</div>
|
|
|
|
<div id="mistake-label">Salah: 0/3</div>
|
|
</div>
|
|
|
|
<div id="game-msg"></div>
|
|
|
|
<div id="board-container">
|
|
<div id="board"></div>
|
|
</div>
|
|
|
|
<div id="numpad-row">
|
|
<div class="number" data-num="1"><span class="main-num">1</span><span class="count-num">9</span></div>
|
|
<div class="number" data-num="2"><span class="main-num">2</span><span class="count-num">9</span></div>
|
|
<div class="number" data-num="3"><span class="main-num">3</span><span class="count-num">9</span></div>
|
|
<div class="number" data-num="4"><span class="main-num">4</span><span class="count-num">9</span></div>
|
|
<div class="number" data-num="5"><span class="main-num">5</span><span class="count-num">9</span></div>
|
|
<div class="number" data-num="6"><span class="main-num">6</span><span class="count-num">9</span></div>
|
|
<div class="number" data-num="7"><span class="main-num">7</span><span class="count-num">9</span></div>
|
|
<div class="number" data-num="8"><span class="main-num">8</span><span class="count-num">9</span></div>
|
|
<div class="number" data-num="9"><span class="main-num">9</span><span class="count-num">9</span></div>
|
|
</div>
|
|
|
|
<div id="tools-row">
|
|
<div class="tool-btn btn-delete" onclick="deleteNumber()"><span>🗑</span> HAPUS</div>
|
|
<div id="btn-hint" class="tool-btn btn-hint" onclick="getHint()"><span>💡</span> BANTUAN (3)</div>
|
|
</div>
|
|
|
|
<div id="modal-game-over" class="modal-overlay">
|
|
<div class="modal-content">
|
|
<div class="modal-icon">😢</div>
|
|
<div class="modal-title">Permainan Berakhir</div>
|
|
<div class="modal-desc">
|
|
Kesempatan habis! Anda telah melakukan 3 kesalahan.
|
|
</div>
|
|
<button class="btn-modal btn-secondary-modal" onclick="restartCurrentLevel()">Coba Lagi (Acak Ulang)</button>
|
|
<button class="btn-modal btn-grey-modal" onclick="showLevelScreen()">Menu Utama</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div id="modal-win" class="modal-overlay">
|
|
<div class="modal-content">
|
|
<div class="modal-icon">🏆</div>
|
|
<div class="modal-title">Luar Biasa!</div>
|
|
<div class="modal-desc">
|
|
Anda berhasil menyelesaikan puzzle ini.
|
|
</div>
|
|
|
|
<div class="win-stats">
|
|
<div class="stat-item">
|
|
<span class="stat-label">Tingkat</span>
|
|
<span class="stat-value" id="win-difficulty">MEDIUM</span>
|
|
</div>
|
|
<div class="stat-item">
|
|
<span class="stat-label">Waktu</span>
|
|
<span class="stat-value" id="win-time">05:30</span>
|
|
</div>
|
|
</div>
|
|
|
|
<button class="btn-modal btn-primary-modal" onclick="showLevelScreen()">Permainan Baru</button>
|
|
</div>
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<script>
|
|
var numSelected = null;
|
|
var tileSelected = null;
|
|
var board = [];
|
|
var solution = [];
|
|
var hintsRemaining = 3;
|
|
var mistakes = 0;
|
|
var currentDifficulty = 'easy';
|
|
|
|
var timerInterval;
|
|
var secondsElapsed = 0;
|
|
|
|
function switchScreen(screenId) {
|
|
document.querySelectorAll('.screen').forEach(s => s.classList.remove('active'));
|
|
document.getElementById(screenId).classList.add('active');
|
|
|
|
document.querySelectorAll('.modal-overlay').forEach(m => m.style.display = 'none');
|
|
}
|
|
|
|
function showMenuScreen() { switchScreen('screen-menu'); stopTimer(); }
|
|
function showLevelScreen() { switchScreen('screen-level'); stopTimer(); }
|
|
|
|
function backToMenu() {
|
|
stopTimer();
|
|
showMenuScreen();
|
|
}
|
|
|
|
function startLevel(difficulty) {
|
|
currentDifficulty = difficulty;
|
|
let holes = 30;
|
|
let label = "MUDAH";
|
|
if (difficulty === 'medium') { holes = 40; label = "MEDIUM"; }
|
|
else if (difficulty === 'hard') { holes = 50; label = "SULIT"; }
|
|
|
|
document.getElementById('difficulty-label').innerText = label;
|
|
switchScreen('screen-game');
|
|
newGame(holes);
|
|
}
|
|
|
|
function newGame(holesCount) {
|
|
solution = generateFullBoard();
|
|
board = JSON.parse(JSON.stringify(solution));
|
|
removeNumbers(board, holesCount);
|
|
|
|
hintsRemaining = 3;
|
|
updateHintButton();
|
|
|
|
mistakes = 0;
|
|
document.getElementById("mistake-label").innerText = "Salah: 0/3";
|
|
|
|
document.getElementById("tools-row").style.display = "flex";
|
|
document.getElementById("numpad-row").style.display = "flex";
|
|
document.querySelectorAll('.modal-overlay').forEach(m => m.style.display = 'none');
|
|
|
|
document.getElementById("game-msg").innerText = "";
|
|
|
|
startTimer();
|
|
|
|
setGame();
|
|
updateRemainingCounts();
|
|
}
|
|
|
|
function restartCurrentLevel() {
|
|
let holes = 30;
|
|
if (currentDifficulty === 'medium') { holes = 40; }
|
|
else if (currentDifficulty === 'hard') { holes = 50; }
|
|
newGame(holes);
|
|
}
|
|
|
|
function startTimer() {
|
|
stopTimer();
|
|
secondsElapsed = 0;
|
|
document.getElementById("timer-label").innerText = "00:00";
|
|
|
|
timerInterval = setInterval(function() {
|
|
secondsElapsed++;
|
|
document.getElementById("timer-label").innerText = formatTime(secondsElapsed);
|
|
}, 1000);
|
|
}
|
|
|
|
function stopTimer() {
|
|
clearInterval(timerInterval);
|
|
}
|
|
|
|
function formatTime(totalSeconds) {
|
|
let m = Math.floor(totalSeconds / 60);
|
|
let s = totalSeconds % 60;
|
|
let mStr = m < 10 ? "0" + m : m;
|
|
let sStr = s < 10 ? "0" + s : s;
|
|
return mStr + ":" + sStr;
|
|
}
|
|
// --------------------
|
|
|
|
function setGame() {
|
|
document.getElementById("board").innerHTML = "";
|
|
tileSelected = null;
|
|
clearAllHighlights();
|
|
|
|
let digits = document.querySelectorAll(".number");
|
|
digits.forEach(digit => {
|
|
digit.removeEventListener("click", selectNumber);
|
|
digit.addEventListener("click", selectNumber);
|
|
});
|
|
|
|
for (let r = 0; r < 9; r++) {
|
|
for (let c = 0; c < 9; c++) {
|
|
let tile = document.createElement("div");
|
|
tile.id = r.toString() + "-" + c.toString();
|
|
if (board[r][c] != 0) {
|
|
tile.innerText = board[r][c];
|
|
tile.classList.add("tile-start");
|
|
} else { tile.classList.add("tile-user"); }
|
|
if (r == 2 || r == 5) tile.classList.add("tile-border-bottom");
|
|
if (c == 2 || c == 5) tile.classList.add("tile-border-right");
|
|
tile.classList.add("tile");
|
|
tile.addEventListener("click", selectTile);
|
|
document.getElementById("board").appendChild(tile);
|
|
}
|
|
}
|
|
}
|
|
|
|
function updateHintButton() {
|
|
let btnHint = document.getElementById("btn-hint");
|
|
btnHint.innerHTML = `<span>💡</span> BANTUAN (${hintsRemaining})`;
|
|
if(hintsRemaining <= 0) btnHint.classList.add("btn-disabled");
|
|
else btnHint.classList.remove("btn-disabled");
|
|
}
|
|
|
|
function updateRemainingCounts() {
|
|
let counts = Array(10).fill(0);
|
|
for (let r = 0; r < 9; r++) {
|
|
for (let c = 0; c < 9; c++) {
|
|
let val = board[r][c];
|
|
if (val !== 0) counts[val]++;
|
|
}
|
|
}
|
|
let digitButtons = querySelectorAll(".number");
|
|
digitButtons.forEach(btn => {
|
|
let num = parseInt(btn.getAttribute("data-num"));
|
|
let remaining = 9 - counts[num];
|
|
let countSpan = btn.querySelector(".count-num");
|
|
if (countSpan) countSpan.innerText = remaining > 0 ? remaining : 0;
|
|
if (remaining <= 0) btn.classList.add("hidden-key");
|
|
else btn.classList.remove("hidden-key");
|
|
});
|
|
}
|
|
|
|
function clearAllHighlights() {
|
|
let sameNumTiles = document.querySelectorAll('.tile-highlight-same');
|
|
sameNumTiles.forEach(t => t.classList.remove('tile-highlight-same'));
|
|
}
|
|
|
|
function highlightSameNumbers(numStr) {
|
|
clearAllHighlights();
|
|
if (numStr === "") return;
|
|
let allTiles = document.querySelectorAll('.tile');
|
|
allTiles.forEach(tile => {
|
|
if (tile.innerText === numStr && !tile.classList.contains("tile-error")) {
|
|
tile.classList.add('tile-highlight-same');
|
|
}
|
|
});
|
|
}
|
|
|
|
function selectTile() {
|
|
if (document.querySelector(".modal-overlay[style*='flex']")) return;
|
|
if (document.getElementById("game-msg").innerText.includes("MENANG")) return;
|
|
|
|
if (tileSelected != null) { tileSelected.classList.remove("tile-selected"); }
|
|
tileSelected = this;
|
|
tileSelected.classList.add("tile-selected");
|
|
highlightSameNumbers(tileSelected.innerText);
|
|
}
|
|
|
|
function selectNumber() {
|
|
if (document.querySelector(".modal-overlay[style*='flex']")) return;
|
|
|
|
let mainNumSpan = this.querySelector(".main-num");
|
|
let numberValue = mainNumSpan.innerText;
|
|
fillTile(numberValue);
|
|
}
|
|
|
|
function fillTile(numberValue) {
|
|
if (tileSelected && !tileSelected.classList.contains("tile-start")) {
|
|
let coords = tileSelected.id.split("-");
|
|
let r = parseInt(coords[0]);
|
|
let c = parseInt(coords[1]);
|
|
let val = parseInt(numberValue);
|
|
|
|
tileSelected.innerText = numberValue;
|
|
board[r][c] = val;
|
|
|
|
if (val !== solution[r][c]) {
|
|
tileSelected.classList.add("tile-error");
|
|
mistakes++;
|
|
document.getElementById("mistake-label").innerText = "Salah: " + mistakes + "/3";
|
|
|
|
if (mistakes >= 3) {
|
|
gameOver();
|
|
}
|
|
} else {
|
|
tileSelected.classList.remove("tile-error");
|
|
highlightSameNumbers(numberValue);
|
|
updateRemainingCounts();
|
|
checkIfWin();
|
|
}
|
|
}
|
|
}
|
|
|
|
function deleteNumber() {
|
|
if (document.querySelector(".modal-overlay[style*='flex']")) return;
|
|
|
|
if (tileSelected && !tileSelected.classList.contains("tile-start")) {
|
|
tileSelected.innerText = "";
|
|
let coords = tileSelected.id.split("-");
|
|
board[parseInt(coords[0])][parseInt(coords[1])] = 0;
|
|
tileSelected.classList.remove("tile-error");
|
|
clearAllHighlights();
|
|
updateRemainingCounts();
|
|
}
|
|
}
|
|
|
|
function getHint() {
|
|
if(hintsRemaining <= 0 || document.querySelector(".modal-overlay[style*='flex']")) return;
|
|
|
|
if (!tileSelected) return;
|
|
if (tileSelected.classList.contains("tile-start")) return;
|
|
|
|
let coords = tileSelected.id.split("-");
|
|
let r = parseInt(coords[0]);
|
|
let c = parseInt(coords[1]);
|
|
let correctNum = solution[r][c];
|
|
|
|
board[r][c] = correctNum;
|
|
tileSelected.innerText = correctNum;
|
|
tileSelected.classList.remove("tile-error");
|
|
|
|
hintsRemaining--;
|
|
updateHintButton();
|
|
|
|
highlightSameNumbers(correctNum.toString());
|
|
updateRemainingCounts();
|
|
checkIfWin();
|
|
}
|
|
|
|
function gameOver() {
|
|
stopTimer();
|
|
document.getElementById("modal-game-over").style.display = "flex";
|
|
document.getElementById("tools-row").style.display = "none";
|
|
document.getElementById("numpad-row").style.display = "none";
|
|
}
|
|
|
|
function checkIfWin() {
|
|
for (let r = 0; r < 9; r++) {
|
|
for (let c = 0; c < 9; c++) {
|
|
if (board[r][c] !== solution[r][c]) return false;
|
|
}
|
|
}
|
|
|
|
stopTimer();
|
|
|
|
let diffText = document.getElementById('difficulty-label').innerText;
|
|
let timeText = document.getElementById("timer-label").innerText;
|
|
|
|
document.getElementById("win-difficulty").innerText = diffText;
|
|
document.getElementById("win-time").innerText = timeText;
|
|
|
|
document.getElementById("modal-win").style.display = "flex";
|
|
|
|
document.getElementById("tools-row").style.display = "none";
|
|
document.getElementById("numpad-row").style.display = "none";
|
|
return true;
|
|
}
|
|
|
|
function generateFullBoard() {
|
|
let newBoard = Array.from({ length: 9 }, () => Array(9).fill(0));
|
|
fillDiagonal(newBoard);
|
|
solveSudoku(newBoard);
|
|
return newBoard;
|
|
}
|
|
function fillDiagonal(b) { for (let i = 0; i < 9; i = i + 3) fillBox(b, i, i); }
|
|
function fillBox(b, row, col) {
|
|
let num;
|
|
for (let i = 0; i < 3; i++) {
|
|
for (let j = 0; j < 3; j++) {
|
|
do { num = Math.floor(Math.random() * 9) + 1; } while (!isSafeBox(b, row, col, num));
|
|
b[row + i][col + j] = num;
|
|
}
|
|
}
|
|
}
|
|
function isSafeBox(b, rowStart, colStart, num) {
|
|
for (let i = 0; i < 3; i++) for (let j = 0; j < 3; j++) if (b[rowStart + i][colStart + j] == num) return false;
|
|
return true;
|
|
}
|
|
function isSafe(b, row, col, num) {
|
|
for (let x = 0; x < 9; x++) if (b[row][x] == num) return false;
|
|
for (let x = 0; x < 9; x++) if (b[x][col] == num) return false;
|
|
let startRow = row - row % 3, startCol = col - col % 3;
|
|
for (let i = 0; i < 3; i++) for (let j = 0; j < 3; j++) if (b[i + startRow][j + startCol] == num) return false;
|
|
return true;
|
|
}
|
|
function solveSudoku(b) {
|
|
let row = -1, col = -1, isEmpty = true;
|
|
for (let i = 0; i < 9; i++) {
|
|
for (let j = 0; j < 9; j++) {
|
|
if (b[i][j] == 0) { row = i; col = j; isEmpty = false; break; }
|
|
}
|
|
if (!isEmpty) break;
|
|
}
|
|
if (isEmpty) return true;
|
|
for (let num = 1; num <= 9; num++) {
|
|
if (isSafe(b, row, col, num)) {
|
|
b[row][col] = num;
|
|
if (solveSudoku(b)) return true;
|
|
b[row][col] = 0;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
function removeNumbers(b, count) {
|
|
while (count > 0) {
|
|
let cellId = Math.floor(Math.random() * 81);
|
|
let i = Math.floor(cellId / 9);
|
|
let j = cellId % 9;
|
|
if (b[i][j] != 0) { b[i][j] = 0; count--; }
|
|
}
|
|
}
|
|
</script>
|
|
</body>
|
|
</html> |