describe('Cartograhpic sudoku visualizer using graph theory. Each reload is a newly generated and mapped out sudoku game. See more at xladn0.rf.gd/gtsudoku/')
function drawSudokuGrid() {
const cellSize = width / 9;
for (let i = 0; i <= 9; i++) {
line(i * cellSize, 0, i * cellSize, height);
line(0, i * cellSize, width, i * cellSize);
for (let i = 0; i < 9; i++) {
for (let j = 0; j < 9; j++) {
const value = sudokuGrid[i][j];
rect(j * cellSize, i * cellSize, cellSize, cellSize);
function drawAlgorithmPath() {
const cellSize = width / 9;
for (let i = 0; i < path.length; i++) {
const [row, col] = path[i];
vertex((col + 0.5) * cellSize, (row + 0.5) * cellSize);
function drawCheckedCells() {
const cellSize = width / 9;
for (let i = 0; i < checkedCells.length; i++) {
const [row, col] = checkedCells[i];
const [prevRow, prevCol] = checkedCells[i - 1];
(prevCol + 0.5) * cellSize,
(prevRow + 0.5) * cellSize,
(prevCol + 0.5) * cellSize + 50,
(prevRow + 0.5) * cellSize,
(col + 0.5) * cellSize - 50,
function generateColors() {
for (let i = 1; i <= 9; i++) {
colors[i] = color(random(255), random(255), random(255));
function generateNewSudoku() {
sudokuGrid = Array.from({ length: 9 }, () =>
Array.from({ length: 9 }, () => 0)
for (let i = 0; i < 17; i++) {
const row = floor(random(9));
const col = floor(random(9));
const num = floor(random(1, 10));
if (isSafe(row, col, num)) {
sudokuGrid[row][col] = num;
console.table(sudokuGrid);
let empty = findEmptyCell();
const [row, col] = empty;
for (let num = 1; num <= 9; num++) {
if (isSafe(row, col, num)) {
checkedCells.push([row, col]);
sudokuGrid[row][col] = num;
sudokuGrid[row][col] = 0;
function findEmptyCell() {
for (let i = 0; i < 9; i++) {
for (let j = 0; j < 9; j++) {
if (sudokuGrid[i][j] === 0) {
function isSafe(row, col, num) {
!usedInSubgrid(row - (row % 3), col - (col % 3), num)
function usedInRow(row, num) {
checkedCells.push([row, num]);
for (let j = 0; j < 9; j++) {
if (sudokuGrid[row][j] === num) {
function usedInCol(col, num) {
checkedCells.push([num, col]);
for (let i = 0; i < 9; i++) {
if (sudokuGrid[i][col] === num) {
function usedInSubgrid(startRow, startCol, num) {
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
checkedCells.push([startRow + i, startCol + j]);
if (sudokuGrid[startRow + i][startCol + j] === num) {