new update

This commit is contained in:
aldo 2025-11-24 13:40:15 +07:00
parent 1ebe791f95
commit 40d5c3bbc7
2 changed files with 442 additions and 0 deletions

BIN
Sudoku icon.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

442
Sudoku.html Normal file
View File

@ -0,0 +1,442 @@
<html>
<head>
<title>Sudoku</title>
<style>
/*PENGATURAN WARNA DENGAN HURUF*/
:root {
/*Warna Utama*/
--primary-color: dodgerblue;
/*Warna Latar Belakang*/
--bg-color: whitesmoke;
/*Warna Papan*/
--board-bg: white;
/* Warna Garis */
--border-color: lightgray;
--thick-border: darkslategray;
/*Warna Teks*/
--text-color: black;
/*Warna Sorot Angka Sama*/
--same-highlight: lightblue;
/*Warna Kotak Terpilih*/
--selected-border: blue;
/*Warna Error/Salah*/
--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 CONTAINER*/
.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); }
}
/*MENU & LEVEL STYLES*/
.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); }
/*Warna Tombol Level menggunakan HURUF*/
.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 STYLES*/
.game-header { width: 100%; max-width: 380px; display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; padding: 0 10px; box-sizing: border-box; }
.btn-back-icon { background: none; border: none; font-size: 28px; cursor: pointer; color: dimgray; padding: 0; }
#difficulty-label { font-size: 18px; font-weight: bold; color: var(--primary-color); text-transform: uppercase; letter-spacing: 1px; }
#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; }
/*Garis Tebal menggunakan warna darkslategray*/
.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 & Tools*/
#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); }
/*Tombol Hapus*/
.btn-delete { color: red; border-color: pink; }
/*Tombol Bantuan*/
.btn-hint { color: darkorange; border-color: orange; }
#game-msg { height: 25px; color: green; font-weight: bold; margin-bottom: 10px; font-size: 16px; text-align: center;}
/*FITUR MENANG*/
#win-controls {
display: none;
margin-top: 15px;
width: 100%;
justify-content: center;
}
.btn-play-again {
background-color: var(--primary-color);
color: white;
padding: 15px 40px;
border: none;
border-radius: 30px;
font-size: 16px;
font-weight: bold;
cursor: pointer;
box-shadow: 0 4px 10px lightgray;
animation: popIn 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}
.btn-play-again:hover { background-color: deepskyblue; }
@keyframes popIn {
from { transform: scale(0); 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="showMenuScreen()">&#8592;</button>
<div id="difficulty-label">MEDIUM</div>
<div style="width: 28px;"></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 class="tool-btn btn-hint" onclick="getHint()"><span>💡</span> BANTUAN</div>
</div>
<div id="win-controls">
<button class="btn-play-again" onclick="showLevelScreen()">Main Lagi</button>
</div>
</div>
<script>
var numSelected = null;
var tileSelected = null;
var board = [];
var solution = [];
function switchScreen(screenId) {
document.querySelectorAll('.screen').forEach(s => s.classList.remove('active'));
document.getElementById(screenId).classList.add('active');
}
function showMenuScreen() { switchScreen('screen-menu'); }
function showLevelScreen() { switchScreen('screen-level'); }
function startLevel(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);
document.getElementById("tools-row").style.display = "flex";
document.getElementById("numpad-row").style.display = "flex";
document.getElementById("win-controls").style.display = "none";
document.getElementById("game-msg").innerText = "";
setGame();
updateRemainingCounts();
}
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 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 = document.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 (tileSelected != null) { tileSelected.classList.remove("tile-selected"); }
tileSelected = this;
tileSelected.classList.add("tile-selected");
highlightSameNumbers(tileSelected.innerText);
}
function selectNumber() {
let mainNumSpan = this.querySelector(".main-num");
let numberValue = mainNumSpan.innerText;
fillTile(numberValue);
}
function fillTile(numberValue) {
if (tileSelected && !tileSelected.classList.contains("tile-start")) {
tileSelected.innerText = numberValue;
let coords = tileSelected.id.split("-");
let r = parseInt(coords[0]);
let c = parseInt(coords[1]);
let val = parseInt(numberValue);
board[r][c] = val;
if (val !== solution[r][c]) { tileSelected.classList.add("tile-error"); }
else { tileSelected.classList.remove("tile-error"); }
highlightSameNumbers(numberValue);
updateRemainingCounts();
checkIfWin();
}
}
function deleteNumber() {
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 (!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");
highlightSameNumbers(correctNum.toString());
updateRemainingCounts();
checkIfWin();
}
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;
}
}
document.getElementById("game-msg").innerText = "SELAMAT! ANDA MENANG!";
document.getElementById("tools-row").style.display = "none";
document.getElementById("numpad-row").style.display = "none";
document.getElementById("win-controls").style.display = "flex";
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>