1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
background(color.hsb(197, 42, 92))
stroke(color.black, 2)
// Sudoku Board Implementation
const GRID_SIZE = 9;
const CELL_SIZE = 50;
const START_X = 50;
const START_Y = 50;
// Create a 2D array to store the board state
let board = Array(GRID_SIZE).fill(null).map(() => Array(GRID_SIZE).fill(0));
let cellButtons = Array(GRID_SIZE).fill(null).map(() => Array(GRID_SIZE).fill(null));
// Sample Sudoku puzzle (0 represents empty cells)
const initialPuzzle = puzzle.sudoku("medium")
// Initialize the board with the puzzle
board = initialPuzzle.map(row => [...row]);
// Selected cell tracking
let selectedRow = -1;
let selectedCol = -1;
// Helper function to create cell click handler
function createCellHandler(r, c) {
return () => {
selectedRow = r;
selectedCol = c;
print(`Selected cell: (${r}, ${c})`);
};
}
// Create the Sudoku grid
for (let row = 0; row < GRID_SIZE; row++) {
for (let col = 0; col < GRID_SIZE; col++) {
const x = START_X + col * CELL_SIZE;
const y = START_Y + row * CELL_SIZE;
const cellValue = board[row][col];
const displayText = cellValue === 0 ? "" : cellValue.toString();
cellButtons[row][col] = ui.button(displayText, x, y);
cellButtons[row][col].action(createCellHandler(row, col));
}
}
// Helper function to create number button handler
function createNumberHandler(n) {
return () => {
if (selectedRow >= 0 && selectedCol >= 0) {
// Only allow changes to cells that were initially empty
if (initialPuzzle[selectedRow][selectedCol] === 0) {
board[selectedRow][selectedCol] = n;
cellButtons[selectedRow][selectedCol].text = n.toString();
print(`Placed ${n} at (${selectedRow}, ${selectedCol})`);
} else {
print("Cannot modify initial puzzle cells!");
}
} else {
print("Please select a cell first!");
}
};
}
// Number input buttons (1-9)
const numberButtonY = START_Y + GRID_SIZE * CELL_SIZE + 30;
for (let num = 1; num <= 9; num++) {
const x = START_X + (num - 1) * CELL_SIZE;
const numBtn = ui.button(num.toString(), x, numberButtonY);
numBtn.action(createNumberHandler(num));
}
// Clear button
const clearBtn = ui.button("Clear", START_X + 9 * CELL_SIZE + 20, numberButtonY);
clearBtn.action(() => {
if (selectedRow >= 0 && selectedCol >= 0) {
if (initialPuzzle[selectedRow][selectedCol] === 0) {
board[selectedRow][selectedCol] = 0;
cellButtons[selectedRow][selectedCol].text = "";
print(`Cleared cell (${selectedRow}, ${selectedCol})`);
}
}
});
print("Sudoku board created! Click a cell, then click a number to fill it.");
background(color.hsb(197, 42, 92))
stroke(color.black, 2)
// Sudoku Board Implementation
const GRID_SIZE = 9;
const CELL_SIZE = 50;
const START_X = 50;
const START_Y = 50;
// Create a 2D array to store the board state
let board = Array(GRID_SIZE).fill(null).map(() => Array(GRID_SIZE).fill(0));
let cellButtons = Array(GRID_SIZE).fill(null).map(() => Array(GRID_SIZE).fill(null));
// Sample Sudoku puzzle (0 represents empty cells)
const initialPuzzle = puzzle.sudoku("medium")
// Initialize the board with the puzzle
board = initialPuzzle.map(row => [...row]);
// Selected cell tracking
let selectedRow = -1;
let selectedCol = -1;
// Helper function to create cell click handler
function createCellHandler(r, c) {
return () => {
selectedRow = r;
selectedCol = c;
print(`Selected cell: (${r}, ${c})`);
};
}
// Create the Sudoku grid
for (let row = 0; row < GRID_SIZE; row++) {
for (let col = 0; col < GRID_SIZE; col++) {
const x = START_X + col * CELL_SIZE;
const y = START_Y + row * CELL_SIZE;
const cellValue = board[row][col];
const displayText = cellValue === 0 ? "" : cellValue.toString();
cellButtons[row][col] = ui.button(displayText, x, y);
cellButtons[row][col].action(createCellHandler(row, col));
}
}
// Helper function to create number button handler
function createNumberHandler(n) {
return () => {
if (selectedRow >= 0 && selectedCol >= 0) {
// Only allow changes to cells that were initially empty
if (initialPuzzle[selectedRow][selectedCol] === 0) {
board[selectedRow][selectedCol] = n;
cellButtons[selectedRow][selectedCol].text = n.toString();
print(`Placed ${n} at (${selectedRow}, ${selectedCol})`);
} else {
print("Cannot modify initial puzzle cells!");
}
} else {
print("Please select a cell first!");
}
};
}
// Number input buttons (1-9)
const numberButtonY = START_Y + GRID_SIZE * CELL_SIZE + 30;
for (let num = 1; num <= 9; num++) {
const x = START_X + (num - 1) * CELL_SIZE;
const numBtn = ui.button(num.toString(), x, numberButtonY);
numBtn.action(createNumberHandler(num));
}
// Clear button
const clearBtn = ui.button("Clear", START_X + 9 * CELL_SIZE + 20, numberButtonY);
clearBtn.action(() => {
if (selectedRow >= 0 && selectedCol >= 0) {
if (initialPuzzle[selectedRow][selectedCol] === 0) {
board[selectedRow][selectedCol] = 0;
cellButtons[selectedRow][selectedCol].text = "";
print(`Cleared cell (${selectedRow}, ${selectedCol})`);
}
}
});
print("Sudoku board created! Click a cell, then click a number to fill it.");