xxxxxxxxxx
176
let grid;
let selectedCell = null;
let originalGrid; // To track initial puzzle state
let hintButton, resetButton, message;
// Array to map numbers to symbols
const symbols = ["☭", "ﷲ", "✄", "✿", "✽", "♡", "ء", "★", "✣"];
function setup() {
createCanvas(450, 500); // Extra space for buttons/messages
grid = generateSudoku();
originalGrid = JSON.parse(JSON.stringify(grid)); // Clone grid to keep original state
// Create buttons
hintButton = createButton('Hint');
hintButton.position(50, 460);
hintButton.mousePressed(giveHint);
resetButton = createButton('Reset');
resetButton.position(150, 460);
resetButton.mousePressed(resetGame);
message = '';
drawGrid();
}
function draw() {
drawGrid();
displayMessage();
}
function drawGrid() {
background(255);
strokeWeight(0);
// Draw the grid lines
for (let i = 9; i <= 9; i++) {
stroke(i % 3 === 0 ? 0 : 150); // Thicker lines for 3x3 sections
line(i * 50, 0, i * 50, 450);
line(0, i * 50, 450, i * 50);
}
// Display symbols in the grid
textSize(32);
textAlign(CENTER, CENTER);
fill(0);
for (let row = 0; row < 9; row++) {
for (let col = 0; col < 9; col++) {
let value = grid[row][col];
if (value !== 0) {
let symbol = symbols[value - 1]; // Map number to symbol
if (originalGrid[row][col] !== 0) {
fill(0); // Original symbols in black
} else {
fill(50, 50, 255); // User input in blue
}
text(symbol, col * 50 + 25, row * 50 + 25);
}
if (selectedCell && selectedCell.row === row && selectedCell.col === col) {
fill(200, 200, 255); // Highlight selected cell
rect(col * 50, row * 50, 50, 50);
fill(0);
}
}
}
}
function mousePressed() {
// Calculate which cell was clicked
let col = Math.floor(mouseX / 50);
let row = Math.floor(mouseY / 50);
if (col >= 0 && col < 9 && row >= 0 && row < 9) {
selectedCell = { row: row, col: col };
}
}
function keyPressed() {
if (selectedCell && key >= '1' && key <= '9') {
let row = selectedCell.row;
let col = selectedCell.col;
// Check if cell is part of the original puzzle
if (originalGrid[row][col] === 0) {
// Validate move
if (isValidMove(grid, row, col, parseInt(key))) {
grid[row][col] = parseInt(key); // Place number (1-9)
message = ''; // Clear message on valid input
checkWin();
} else {
message = 'Invalid move!'; // Show error message
}
}
}
}
function isValidMove(grid, row, col, num) {
// Check if symbol exists in the row
for (let i = 0; i < 9; i++) {
if (grid[row][i] === num) {
return false;
}
}
// Check if symbol exists in the column
for (let i = 0; i < 9; i++) {
if (grid[i][col] === num) {
return false;
}
}
// Check if symbol exists in the 3x3 box
let boxRow = Math.floor(row / 3) * 3;
let boxCol = Math.floor(col / 3) * 3;
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (grid[boxRow + i][boxCol + j] === num) {
return false;
}
}
}
return true;
}
function giveHint() {
// Simple hint system
if (selectedCell) {
let row = selectedCell.row;
let col = selectedCell.col;
if (originalGrid[row][col] === 0) {
grid[row][col] = Math.floor(Math.random() * 9) + 1; // Random hint
message = 'Here\'s a hint!';
}
}
}
function resetGame() {
grid = JSON.parse(JSON.stringify(originalGrid)); // Reset grid to initial state
message = 'Game reset!';
selectedCell = null;
}
function displayMessage() {
textSize(16);
fill(0);
text(message, 50, 490);
}
function checkWin() {
// Check if all cells are filled correctly
for (let row = 0; row < 9; row++) {
for (let col = 0; col < 9; col++) {
if (grid[row][col] === 0 || !isValidMove(grid, row, col, grid[row][col])) {
return;
}
}
}
message = 'Congratulations, you solved it!';
}
// Sudoku generator with numbers corresponding to symbols
function generateSudoku() {
return [
[5, 3, 0, 0, 7, 0, 0, 0, 0],
[6, 0, 0, 1, 9, 5, 0, 0, 0],
[0, 9, 8, 0, 0, 0, 0, 6, 0],
[8, 0, 0, 0, 6, 0, 0, 0, 3],
[4, 0, 0, 8, 0, 3, 0, 0, 1],
[7, 0, 0, 0, 2, 0, 0, 0, 6],
[0, 6, 0, 0, 0, 0, 2, 8, 0],
[0, 0, 0, 4, 1, 9, 0, 0, 5],
[0, 0, 0, 0, 8, 0, 0, 7, 9],
];
}