“Sudoku Maker” by Alex Flowers
https://openprocessing.org/sketch/2045054
License CreativeCommons Attribution NonCommercial ShareAlike
https://creativecommons.org/licenses/by-nc-sa/3.0
{{filePath}}
{{width}} x {{height}}
Report Sketch
Oh, that naughty sketch! Please let us know what the issue is below.
Apply Template
Applying this template will reset your sketch and remove all your changes. Are you sure you would like to continue?
Report Sketch
Report Comment
Please confirm that you would like to report the comment below.
We will review your submission and take any actions necessary per our Community Guidelines. In addition to reporting this comment, you can also block the user to prevent any future interactions.
Please report comments only when necessary. Unnecessary or abusive use of this tool may result in your own account being suspended.
Are you sure you want to delete your sketch?
Any files uploaded will be deleted as well.
Delete Comment?
This will also delete all the replies to this comment.
Delete this tab? Any code in it will be deleted as well.
Select a collection to submit your sketch
We Need Your Support
Since 2008, OpenProcessing has provided tools for creative coders to learn, create, and share over a million open source projects in a friendly environment.
Niche websites like ours need your continued support for future development and maintenance, while keeping it an ad-free platform that respects your data and privacy!
Please consider subscribing below to show your support with a "Plus" badge on your profile and get access to many other features!
CC Attribution NonCommercial ShareAlike
Sudoku Maker
Flowers
xxxxxxxxxx
// TODO: update certain/exclusive data gathering (there's a bug where the empty cells are negative)
// this data gathering is pretty messy. but this was a fun project to work on!!!
// i worked on this during oct15 and oct16 /2023. gonna get back to it later!
// thanks for checking it out :-)
var puzzle = [];
var slotData = {
empty: 81,
filled: 0,
certain: 0,
exclusive: 0
};
var slotSize = 50;
var margin = 10;
var showHints = false;
function setup() {
createCanvas(windowWidth, windowHeight);
margin = (height-100)/40;
slotSize = ((height-100)/9) - margin;
for(let row = 0; row < 9; row++){
puzzle[row] = [];
for(let col = 0; col < 9; col++){
puzzle[row][col] = new Slot(row, col);
}
}
}
function draw() {
background("#4085c9");
fill("#1e5994"); noStroke(); textSize(height/14); textAlign(LEFT, TOP); textLeading(height/14);
textStyle(BOLD); text("Data:", ((width-(slotSize*9+margin*8))/2)+(slotSize*10+margin*8), 50); textStyle(NORMAL);
text(slotData.empty+" uncertain slots\n"+slotData.filled+" filled slots\n"+slotData.certain+" certain slots\n"+slotData.exclusive+" exclusive slots", ((width-(slotSize*9+margin*8))/2)+(slotSize*10+margin*8), 50+height/12);
textSize(height/16); text("Show Hints", ((width-(slotSize*9+margin*8))/2)+(slotSize*10+margin*8)+slotSize+margin, height-50-height/16-margin);
fill("white"); rect(((width-(slotSize*9+margin*8))/2)+(slotSize*10+margin*8), height-50-slotSize-margin, slotSize, slotSize);
if(showHints) {
noFill(); stroke("#1e5994"); strokeWeight(margin);
beginShape();
vertex(((width-(slotSize*9+margin*8))/2)+(slotSize*10+margin*8), height-50-slotSize/2-margin);
vertex(((width-(slotSize*9+margin*8))/2)+(slotSize*10+margin*8)+slotSize/2, height-50-margin*1.5);
vertex(((width-(slotSize*9+margin*8))/2)+(slotSize*10+margin*8)+slotSize, height-50-slotSize-margin);
endShape();
}
for(let row = 0; row < 9; row++){
for(let col = 0; col < 9; col++){
let thisSlot = puzzle[row][col];
if(mouseX > thisSlot.px && mouseY > thisSlot.py && mouseX < thisSlot.px + slotSize && mouseY < thisSlot.py + slotSize) {
thisSlot.hover = true;
} else thisSlot.hover = false;
if(thisSlot.canBe[thisSlot.fill-1] || thisSlot.fill == 0) {
thisSlot.duplicata = false;
} else {
thisSlot.duplicata = true;
}
noStroke();
if(thisSlot.duplicata) fill("#ffd29a");
else if(thisSlot.select) fill("#5fa0e1");
else if(thisSlot.hover) fill("#7db5ed");
else if(thisSlot.danger) fill("#a7f7ec");
else fill ("#92c2f2");
rect(thisSlot.px, thisSlot.py, slotSize, slotSize);
fill("#ff9af8");
if(showHints && thisSlot.exclusive) circle(thisSlot.px+slotSize/2, thisSlot.py+slotSize/2, slotSize);
if(thisSlot.fill != 0) {
fill("#1e5994"); noStroke(); textSize(height/16); textAlign(CENTER, CENTER);
text(thisSlot.fill, thisSlot.px+slotSize/2, thisSlot.py+slotSize/1.8);
} else if(showHints) {
let possibilities = [];
for(let i = 0; i < 9; i++) {
if(thisSlot.canBe[i] == true) {
possibilities.push(i+1);
}
}
possibilities = String(possibilities).replaceAll(",", " ");
fill("#1e599499"); noStroke(); textSize(height/50); textAlign(CENTER, CENTER); rectMode(CENTER); textLeading(height/50);
text(possibilities, thisSlot.px+slotSize/2, thisSlot.py+slotSize/1.8, slotSize*0.8, slotSize*1.5);
rectMode(CORNER);
}
}
}
stroke("#1e5994");
strokeCap(SQUARE);
strokeWeight(margin+1);
line(((width-(slotSize*9+margin*8))/2), 50+slotSize*3+margin*2.5, ((width-(slotSize*9+margin*8))/2)+slotSize*9+margin*8, 50+slotSize*3+margin*2.5);
line(((width-(slotSize*9+margin*8))/2), 50+slotSize*6+margin*5.5, ((width-(slotSize*9+margin*8))/2)+slotSize*9+margin*8, 50+slotSize*6+margin*5.5);
line(((width-(slotSize*9+margin*8))/2)+slotSize*3+margin*2.5, 50, ((width-(slotSize*9+margin*8))/2)+slotSize*3+margin*2.5, height-50-margin);
line(((width-(slotSize*9+margin*8))/2)+slotSize*6+margin*5.5, 50, ((width-(slotSize*9+margin*8))/2)+slotSize*6+margin*5.5, height-50-margin);
noFill();
rect(((width-(slotSize*9+margin*8))/2)-margin*0.5, 50-margin*0.5, slotSize*9+margin*9, slotSize*9+margin*9);
}
function mousePressed() {
if(mouseX > ((width-(slotSize*9+margin*8))/2)+(slotSize*10+margin*8) && mouseX < ((width-(slotSize*9+margin*8))/2)+(slotSize*10+margin*8)+slotSize && mouseY > height-50-slotSize-margin && mouseY < height-50-slotSize-margin+slotSize) {
showHints = !showHints;
}
for(let row = 0; row < 9; row++){
for(let col = 0; col < 9; col++){
let thisSlot = puzzle[row][col];
if(mouseX > thisSlot.px && mouseY > thisSlot.py && mouseX < thisSlot.px + slotSize && mouseY < thisSlot.py + slotSize) {
if(!thisSlot.select) {
for(let rowC = 0; rowC < 9; rowC++){
for(let colC = 0; colC < 9; colC++){
puzzle[rowC][colC].select = false;
if(row == rowC || col == colC || thisSlot.group == puzzle[rowC][colC].group) {
puzzle[rowC][colC].danger = true;
} else puzzle[rowC][colC].danger = false;
}
}
thisSlot.select = true;
} else {
for(let rowC = 0; rowC < 9; rowC++){
for(let colC = 0; colC < 9; colC++){
puzzle[rowC][colC].danger = false;
}
}
thisSlot.select = false;
}
}
}
}
}
function onlyHasOne(arr, value) {
var foundOne = false;
for(var i = 0; i < arr.length; ++i) {
if(arr[i] == value)
if(foundOne) return false;
else foundOne = true;
}
return foundOne;
}
function keyPressed() {
// find the selected slot and fill it
for(let row = 0; row < 9; row++){
for(let col = 0; col < 9; col++){
let thisSlot = puzzle[row][col];
if(key != ' ' && key == 0 || key == 1 || key == 2 || key == 3 || key == 4 || key == 5 || key == 6 || key == 7 || key == 8 || key == 9) {
if(thisSlot.select) {
thisSlot.fill = key;
thisSlot.select = false;
}
}
}
}
// clear data to be updated
slotData = {
empty: 81,
filled: 0,
certain: 0,
exclusive: 0
};
// update the possibilities for all 81 slots (doing this before the filling would not update all slots)
for(let row = 0; row < 9; row++){
for(let col = 0; col < 9; col++){
let thisSlot = puzzle[row][col];
if(key != ' ' && key == 0 || key == 1 || key == 2 || key == 3 || key == 4 || key == 5 || key == 6 || key == 7 || key == 8 || key == 9) {
let numbersFilled = [];
for(let rowC = 0; rowC < 9; rowC++){
for(let colC = 0; colC < 9; colC++){
let checkSlot = puzzle[rowC][colC];
if(!(row == rowC && col == colC) && (row == rowC || col == colC || thisSlot.group == checkSlot.group)) {
if(checkSlot.fill != 0) {
numbersFilled.push(int(checkSlot.fill));
}
}
puzzle[rowC][colC].danger = false;
}
}
if(numbersFilled.length == 0) {
for (let n = 0; n < 9; n++) {
thisSlot.canBe[n] = true;
}
}
for (let i = 0; i < numbersFilled.length; i++) {
let number = numbersFilled[i];
for (let n = 0; n < 9; n++) {
if (numbersFilled.includes(n+1)) {
thisSlot.canBe[n] = false; // loop for every slot
} else {
thisSlot.canBe[n] = true;
}
}
}
}
// update the data based on thisSlot
if(thisSlot.fill != 0) {
slotData.filled++;
slotData.empty--;
} else if(onlyHasOne(thisSlot.canBe, true)) {
slotData.certain++;
slotData.empty--;
}
}
}
// calculates specific certainty after possibilities
for(let row = 0; row < 9; row++){
for(let col = 0; col < 9; col++){
let thisSlot = puzzle[row][col];
let rowOptions = [];
let colOptions = [];
let groupOptions = [];
for(let rowC = 0; rowC < 9; rowC++){
for(let colC = 0; colC < 9; colC++){
let checkSlot = puzzle[rowC][colC];
if(/*!(row == rowC && col == colC) &&*/ (checkSlot.fill == 0)) {
if(row == rowC) {
checkSlot.canBe.forEach((element, index) => element ? rowOptions.push(index+1) : element = element);
}
if(col == colC) {
checkSlot.canBe.forEach((element, index) => element ? colOptions.push(index+1) : element = element);
}
if(thisSlot.group == checkSlot.group) {
checkSlot.canBe.forEach((element, index) => element ? groupOptions.push(index+1) : element = element);
}
}
}
}
// update the data based on thisSlot
for(let i = 0; i < 9; i++) {
if(thisSlot.fill == 0 && onlyHasOne(rowOptions, i) && onlyHasOne(colOptions, i) && onlyHasOne(groupOptions, i)) {
thisSlot.exclusive = true;
slotData.exclusive++;
//slotData.empty--;
break;
} else thisSlot.exclusive = false;
}
}
}
}
class Slot {
constructor(ix, iy) {
this.ix = ix;
this.iy = iy;
this.px = ((width-(slotSize*9+margin*8))/2)+ix*(slotSize+margin);
this.py = 50+iy*(slotSize+margin);
if((ix >= 0 && ix <= 2) & (iy >= 0 && iy <= 2)) this.group = 0;
if((ix >= 3 && ix <= 5) & (iy >= 0 && iy <= 2)) this.group = 1;
if((ix >= 6 && ix <= 8) & (iy >= 0 && iy <= 2)) this.group = 2;
if((ix >= 0 && ix <= 2) & (iy >= 3 && iy <= 5)) this.group = 3;
if((ix >= 3 && ix <= 5) & (iy >= 3 && iy <= 5)) this.group = 4;
if((ix >= 6 && ix <= 8) & (iy >= 3 && iy <= 5)) this.group = 5;
if((ix >= 0 && ix <= 2) & (iy >= 6 && iy <= 8)) this.group = 6;
if((ix >= 3 && ix <= 5) & (iy >= 6 && iy <= 8)) this.group = 7;
if((ix >= 6 && ix <= 8) & (iy >= 6 && iy <= 8)) this.group = 8;
this.fill = 0;
this.hover = false;
this.select = false;
this.danger = false;
this.duplicata = false;
this.exclusive = false;
this.can = [1, 2, 3, 4, 5, 6, 7, 8, 9];
this.canBe = [true, true, true, true, true, true, true, true, true];
}
}
Examples: Play - Synthesis - Microphone
See More Shortcuts
Please verify your email to comment
Verify Email