xxxxxxxxxx
163
let scene = 1
let introbg
let secondbg
let thirdbg
let sudokuGrid = [];
let symbols = ["✽", "✿", "✤", "★", "♡", "✄", "☭", "ﷲ" , "ء"];
let names = ["ﷲ" ," ٱللَّٰهِ","ٱلرَّحِيمِ","ٱلرَّحْمَٰنِ","بِسْمِ"]; //array/list of allah's names
let colors = ["#f9ec00", "#f47920", "#e21d3c", "#583f99", "#9a258f", "#17479e", "#00addc", "#ed1846", "#ffffff"];
let selectedSymbols = [];
let selectedColors = [];
let btn;
let inpt; //holds the textbox
let name; //will hold the chosen name
function preload(){
introbg = loadImage("m_bg1i.png")
secondbg = loadImage("m_bg1.png")
thirdbg = loadImage("m_bg1g.png")
}
function setup() {
createCanvas(450, 450);
textAlign(CENTER, CENTER);
textSize(27);
rectMode(CENTER)
inpt = createInput("What would you like to attract?");
// Button for generating a new Sudoku
btn = createButton('************************');
// btn.position(width/2 - 100, height - 130);
btn.mousePressed(generateNewSudoku);
}
function draw(){
if(scene == 1){
image(introbg, 0, 0, 450, 450)
fill(255)
noStroke()
rect(width-25, height/2, 50, 50)
// rect(25, height/2, 50, 50)
} else if(scene == 2){
image(secondbg, 0, 0, 450, 450)
fill(255)
noStroke()
rect(width-25, height/2, 50, 50)
rect(25, height/2, 50, 50)
drawSudoku();
} else if(scene == 3){
image(thirdbg, 0, 0, 450, 450)
fill(255)
noStroke()
rect(width-25, height/2, 50, 50)
rect(25, height/2, 50, 50)
}
}
function mousePressed(){
if(scene == 1 && dist(mouseX, mouseY, width-25, height/2) < 25){
scene = 2
} else if(scene == 2 && dist(mouseX, mouseY, 25, height/2) < 25){
scene = 1
} else if(scene == 2 && dist(mouseX, mouseY, width-25, height/2) < 25){
scene = 3
} else if(scene == 3 && dist(mouseX, mouseY, 25, height/2) < 25){
scene = 2
} else if(scene == 3 && dist(mouseX, mouseY, width-25, height/2) < 25){
scene = 1
}
}
function generateNewSudoku() {
btn.hide();
inpt.hide();
selectedSymbols = shuffle(symbols).slice(0, 9); // Pick 9 random symbols
selectedColors = shuffle(colors).slice(0, 9); // Pick 9 random colors
sudokuGrid = generateSolvedSudoku(); // Generate a new solved grid
scene = 2
}
function drawSudoku() {
fill(0);
rect(width/2, height/2, 300, 300 )
textSize(18)
for (let row = 0; row < 9; row++) {
for (let col = 0; col < 9; col++) {
let x = col * 50;
let y = row * 50;
let num = sudokuGrid[row][col];
fill(selectedColors[num - 1]);
text(selectedSymbols[num - 1], x + 25, y + 25);
}
}
name = random(names); //choosing a 'random'
textSize(50)
text(name, width/2, height/2);
}
function generateSolvedSudoku() {
// Initialize empty 9x9 grid
let grid = Array.from({ length: 9 }, () => Array(9).fill(0));
// Fill the diagonal 3x3 blocks
for (let i = 0; i < 9; i += 3) fillDiagonalBlock(grid, i, i);
// Backtrack to fill the entire grid
solveSudoku(grid);
return grid;
}
function fillDiagonalBlock(grid, row, col) {
let nums = shuffle([1, 2, 3, 4, 5, 6, 7, 8, 9]);
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
grid[row + i][col + j] = nums.pop();
}
}
}
function solveSudoku(grid) {
let emptyPos = findEmpty(grid);
if (!emptyPos) return true; // Solved
let [row, col] = emptyPos;
for (let num = 1; num <= 9; num++) {
if (isSafe(grid, row, col, num)) {
grid[row][col] = num;
if (solveSudoku(grid)) return true;
grid[row][col] = 0; // Backtrack
}
}
return false;
}
function findEmpty(grid) {
for (let i = 0; i < 9; i++) {
for (let j = 0; j < 9; j++) {
if (grid[i][j] === 0) return [i, j];
}
}
return null;
}
function isSafe(grid, row, col, num) {
for (let x = 0; x < 9; x++) {
if (grid[row][x] === num || grid[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 (grid[i + startRow][j + startCol] === num) return false;
}
}
return true;
}