User prompt
add music
User prompt
Please fix the bug: 'Uncaught TypeError: Cannot set properties of undefined (setting 'fill')' in or related to this line: 'self.buttonText.style.fill = selected ? 0xffffff : 0x333333;' Line Number: 79
User prompt
Please fix the bug: 'Uncaught TypeError: Cannot set properties of undefined (setting 'fill')' in or related to this line: 'self.buttonText.style.fill = selected ? 0xffffff : 0x333333;' Line Number: 79
User prompt
add easy, medium, hard modes
User prompt
Please fix the bug: 'Cannot read properties of undefined (reading '0')' in or related to this line: 'var cell = new SudokuCell(row, col, puzzleData[row][col]);' Line Number: 435
User prompt
add easy medium hard modes and create a menu before the game starts to select the mode
User prompt
Please fix the bug: 'Cannot read properties of undefined (reading '0')' in or related to this line: 'var cell = new SudokuCell(row, col, puzzleData[row][col]);' Line Number: 381
User prompt
Please fix the bug: 'Cannot read properties of undefined (reading '0')' in or related to this line: 'var cell = new SudokuCell(row, col, puzzleData[row][col]);' Line Number: 381
User prompt
add easy medium hard modes and create a menu before the game starts to select the mode
User prompt
generate random games every time it refreshes
User prompt
put a button to delete the wrong number
User prompt
put a button to delete the wrong number
User prompt
make each 3x3 area very distinct
User prompt
make each 3x3 area distinct
User prompt
move the time up a bit
Code edit (1 edits merged)
Please save this source code
User prompt
Sudoku Master
Initial prompt
make a sudoku game
/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); /**** * Classes ****/ var DeleteButton = Container.expand(function () { var self = Container.call(this); var background = self.attachAsset('numberButton', { anchorX: 0.5, anchorY: 0.5 }); var deleteText = new Text2('CLEAR', { size: 35, fill: 0x333333 }); deleteText.anchor.set(0.5, 0.5); self.addChild(deleteText); self.down = function (x, y, obj) { if (selectedCell && !selectedCell.isInitial && selectedCell.value > 0) { selectedCell.setValue(0); checkForErrors(); LK.getSound('select').play(); } }; return self; }); var HintButton = Container.expand(function () { var self = Container.call(this); var background = self.attachAsset('hintButton', { anchorX: 0.5, anchorY: 0.5 }); var hintText = new Text2('HINT', { size: 40, fill: 0x333333 }); hintText.anchor.set(0.5, 0.5); self.addChild(hintText); self.down = function (x, y, obj) { if (selectedCell && !selectedCell.isInitial && selectedCell.value === 0) { var correctValue = getCorrectValueForCell(selectedCell.row, selectedCell.col); selectedCell.setValue(correctValue); checkWinCondition(); LK.getSound('correct').play(); } }; return self; }); var NumberButton = Container.expand(function (number) { var self = Container.call(this); self.number = number; var background = self.attachAsset('numberButton', { anchorX: 0.5, anchorY: 0.5 }); var numberText = new Text2(number.toString(), { size: 60, fill: 0x333333 }); numberText.anchor.set(0.5, 0.5); self.addChild(numberText); self.down = function (x, y, obj) { if (selectedCell && !selectedCell.isInitial) { selectedCell.setValue(self.number); checkForErrors(); checkWinCondition(); LK.getSound('correct').play(); } }; return self; }); var SudokuCell = Container.expand(function (row, col, initialValue) { var self = Container.call(this); self.row = row; self.col = col; self.value = initialValue || 0; self.isInitial = initialValue > 0; self.isSelected = false; self.hasError = false; // Create border (thick for 3x3 box boundaries) var needsThickBorder = row % 3 === 0 || col % 3 === 0; var border = self.attachAsset(needsThickBorder ? 'thickBorder' : 'cellBorder', { anchorX: 0.5, anchorY: 0.5 }); // Create cell background self.background = self.attachAsset('cell', { anchorX: 0.5, anchorY: 0.5 }); // Create number text self.numberText = new Text2(self.value > 0 ? self.value.toString() : '', { size: 80, fill: self.isInitial ? "#000000" : "#0066cc" }); self.numberText.anchor.set(0.5, 0.5); self.addChild(self.numberText); self.setValue = function (newValue) { if (self.isInitial) return; self.value = newValue; self.numberText.setText(newValue > 0 ? newValue.toString() : ''); self.updateDisplay(); }; self.setSelected = function (selected) { self.isSelected = selected; self.updateDisplay(); }; self.setError = function (error) { self.hasError = error; self.updateDisplay(); }; self.updateDisplay = function () { if (self.hasError) { self.removeChild(self.background); self.background = self.attachAsset('errorCell', { anchorX: 0.5, anchorY: 0.5 }); self.addChildAt(self.background, 1); } else if (self.isSelected) { self.removeChild(self.background); self.background = self.attachAsset('selectedCell', { anchorX: 0.5, anchorY: 0.5 }); self.addChildAt(self.background, 1); } else { self.removeChild(self.background); self.background = self.attachAsset('cell', { anchorX: 0.5, anchorY: 0.5 }); self.addChildAt(self.background, 1); } }; self.down = function (x, y, obj) { if (!self.isInitial) { selectedCell = self; updateCellSelection(); LK.getSound('select').play(); } }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0xf0f0f0 }); /**** * Game Code ****/ // Game variables var grid = []; var selectedCell = null; var numberButtons = []; var startTime = Date.now(); var isComplete = false; // Sudoku generator functions function createEmptyGrid() { var grid = []; for (var i = 0; i < 9; i++) { grid[i] = []; for (var j = 0; j < 9; j++) { grid[i][j] = 0; } } return grid; } function isValidPlacement(grid, row, col, num) { // Check row for (var c = 0; c < 9; c++) { if (grid[row][c] === num) return false; } // Check column for (var r = 0; r < 9; r++) { if (grid[r][col] === num) return false; } // Check 3x3 box var boxRow = Math.floor(row / 3) * 3; var boxCol = Math.floor(col / 3) * 3; for (var r = boxRow; r < boxRow + 3; r++) { for (var c = boxCol; c < boxCol + 3; c++) { if (grid[r][c] === num) return false; } } return true; } function shuffleArray(array) { for (var i = array.length - 1; i > 0; i--) { var j = Math.floor(Math.random() * (i + 1)); var temp = array[i]; array[i] = array[j]; array[j] = temp; } return array; } function fillGrid(grid) { for (var row = 0; row < 9; row++) { for (var col = 0; col < 9; col++) { if (grid[row][col] === 0) { var numbers = shuffleArray([1, 2, 3, 4, 5, 6, 7, 8, 9]); for (var i = 0; i < numbers.length; i++) { var num = numbers[i]; if (isValidPlacement(grid, row, col, num)) { grid[row][col] = num; if (fillGrid(grid)) { return true; } grid[row][col] = 0; } } return false; } } } return true; } function createPuzzle(difficulty) { var grid = createEmptyGrid(); fillGrid(grid); // Create a copy for the solution var solution = []; for (var i = 0; i < 9; i++) { solution[i] = []; for (var j = 0; j < 9; j++) { solution[i][j] = grid[i][j]; } } // Remove cells based on difficulty var cellsToRemove = difficulty === 'easy' ? 40 : difficulty === 'medium' ? 45 : 50; var removed = 0; while (removed < cellsToRemove) { var row = Math.floor(Math.random() * 9); var col = Math.floor(Math.random() * 9); if (grid[row][col] !== 0) { grid[row][col] = 0; removed++; } } return { puzzle: grid, solution: solution }; } // Generate random puzzle var gameData = createPuzzle('easy'); var puzzleData = gameData.puzzle; var solution = gameData.solution; // Create grid background var gridBg = game.attachAsset('gridBackground', { anchorX: 0.5, anchorY: 0.5, x: 1024, y: 1100 }); // Create 3x3 box backgrounds for visual distinction for (var boxRow = 0; boxRow < 3; boxRow++) { for (var boxCol = 0; boxCol < 3; boxCol++) { var boxIndex = boxRow * 3 + boxCol; // Add thick border around each box var boxBorder = game.attachAsset('boxBorder', { anchorX: 0.5, anchorY: 0.5, x: 1024 + (boxCol - 1) * 600, y: 1100 + (boxRow - 1) * 600 }); // Add colored background var boxBg = game.attachAsset('boxBackground' + boxIndex, { anchorX: 0.5, anchorY: 0.5, x: 1024 + (boxCol - 1) * 600, y: 1100 + (boxRow - 1) * 600 }); } } // Create Sudoku grid for (var row = 0; row < 9; row++) { grid[row] = []; for (var col = 0; col < 9; col++) { var cell = new SudokuCell(row, col, puzzleData[row][col]); cell.x = 1024 + (col - 4) * 200; cell.y = 1100 + (row - 4) * 200; grid[row][col] = cell; game.addChild(cell); } } // Create number buttons var buttonStartX = 300; var buttonY = 2400; for (var i = 1; i <= 9; i++) { var button = new NumberButton(i); button.x = buttonStartX + (i - 1) * 180; button.y = buttonY; numberButtons.push(button); game.addChild(button); } // Create delete button var deleteBtn = new DeleteButton(); deleteBtn.x = buttonStartX + 9 * 180; deleteBtn.y = buttonY; game.addChild(deleteBtn); // Create hint button var hintBtn = new HintButton(); hintBtn.x = 1024; hintBtn.y = 2550; game.addChild(hintBtn); // Create timer display var timerText = new Text2('Time: 00:00', { size: 60, fill: 0x333333 }); timerText.anchor.set(0.5, 0); LK.gui.top.addChild(timerText); timerText.y = 50; // Helper functions function updateCellSelection() { for (var row = 0; row < 9; row++) { for (var col = 0; col < 9; col++) { grid[row][col].setSelected(grid[row][col] === selectedCell); } } } function checkForErrors() { // Clear all errors first for (var row = 0; row < 9; row++) { for (var col = 0; col < 9; col++) { grid[row][col].setError(false); } } // Check for conflicts for (var row = 0; row < 9; row++) { for (var col = 0; col < 9; col++) { var cell = grid[row][col]; if (cell.value > 0) { if (hasConflict(row, col, cell.value)) { cell.setError(true); } } } } } function hasConflict(row, col, value) { // Check row for (var c = 0; c < 9; c++) { if (c !== col && grid[row][c].value === value) { return true; } } // Check column for (var r = 0; r < 9; r++) { if (r !== row && grid[r][col].value === value) { return true; } } // Check 3x3 box var boxRow = Math.floor(row / 3) * 3; var boxCol = Math.floor(col / 3) * 3; for (var r = boxRow; r < boxRow + 3; r++) { for (var c = boxCol; c < boxCol + 3; c++) { if ((r !== row || c !== col) && grid[r][c].value === value) { return true; } } } return false; } function getCorrectValueForCell(row, col) { return solution[row][col]; } function checkWinCondition() { if (isComplete) return; var allFilled = true; var hasErrors = false; for (var row = 0; row < 9; row++) { for (var col = 0; col < 9; col++) { var cell = grid[row][col]; if (cell.value === 0) { allFilled = false; } if (cell.hasError) { hasErrors = true; } } } if (allFilled && !hasErrors) { isComplete = true; LK.getSound('complete').play(); LK.effects.flashScreen(0x00ff00, 1000); LK.setTimeout(function () { LK.showYouWin(); }, 1500); } } function formatTime(seconds) { var mins = Math.floor(seconds / 60); var secs = seconds % 60; return 'Time: ' + (mins < 10 ? '0' : '') + mins + ':' + (secs < 10 ? '0' : '') + secs; } // Start background music LK.playMusic('bgmusic'); game.update = function () { if (!isComplete) { var currentTime = Math.floor((Date.now() - startTime) / 1000); timerText.setText(formatTime(currentTime)); } };
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var DeleteButton = Container.expand(function () {
var self = Container.call(this);
var background = self.attachAsset('numberButton', {
anchorX: 0.5,
anchorY: 0.5
});
var deleteText = new Text2('CLEAR', {
size: 35,
fill: 0x333333
});
deleteText.anchor.set(0.5, 0.5);
self.addChild(deleteText);
self.down = function (x, y, obj) {
if (selectedCell && !selectedCell.isInitial && selectedCell.value > 0) {
selectedCell.setValue(0);
checkForErrors();
LK.getSound('select').play();
}
};
return self;
});
var HintButton = Container.expand(function () {
var self = Container.call(this);
var background = self.attachAsset('hintButton', {
anchorX: 0.5,
anchorY: 0.5
});
var hintText = new Text2('HINT', {
size: 40,
fill: 0x333333
});
hintText.anchor.set(0.5, 0.5);
self.addChild(hintText);
self.down = function (x, y, obj) {
if (selectedCell && !selectedCell.isInitial && selectedCell.value === 0) {
var correctValue = getCorrectValueForCell(selectedCell.row, selectedCell.col);
selectedCell.setValue(correctValue);
checkWinCondition();
LK.getSound('correct').play();
}
};
return self;
});
var NumberButton = Container.expand(function (number) {
var self = Container.call(this);
self.number = number;
var background = self.attachAsset('numberButton', {
anchorX: 0.5,
anchorY: 0.5
});
var numberText = new Text2(number.toString(), {
size: 60,
fill: 0x333333
});
numberText.anchor.set(0.5, 0.5);
self.addChild(numberText);
self.down = function (x, y, obj) {
if (selectedCell && !selectedCell.isInitial) {
selectedCell.setValue(self.number);
checkForErrors();
checkWinCondition();
LK.getSound('correct').play();
}
};
return self;
});
var SudokuCell = Container.expand(function (row, col, initialValue) {
var self = Container.call(this);
self.row = row;
self.col = col;
self.value = initialValue || 0;
self.isInitial = initialValue > 0;
self.isSelected = false;
self.hasError = false;
// Create border (thick for 3x3 box boundaries)
var needsThickBorder = row % 3 === 0 || col % 3 === 0;
var border = self.attachAsset(needsThickBorder ? 'thickBorder' : 'cellBorder', {
anchorX: 0.5,
anchorY: 0.5
});
// Create cell background
self.background = self.attachAsset('cell', {
anchorX: 0.5,
anchorY: 0.5
});
// Create number text
self.numberText = new Text2(self.value > 0 ? self.value.toString() : '', {
size: 80,
fill: self.isInitial ? "#000000" : "#0066cc"
});
self.numberText.anchor.set(0.5, 0.5);
self.addChild(self.numberText);
self.setValue = function (newValue) {
if (self.isInitial) return;
self.value = newValue;
self.numberText.setText(newValue > 0 ? newValue.toString() : '');
self.updateDisplay();
};
self.setSelected = function (selected) {
self.isSelected = selected;
self.updateDisplay();
};
self.setError = function (error) {
self.hasError = error;
self.updateDisplay();
};
self.updateDisplay = function () {
if (self.hasError) {
self.removeChild(self.background);
self.background = self.attachAsset('errorCell', {
anchorX: 0.5,
anchorY: 0.5
});
self.addChildAt(self.background, 1);
} else if (self.isSelected) {
self.removeChild(self.background);
self.background = self.attachAsset('selectedCell', {
anchorX: 0.5,
anchorY: 0.5
});
self.addChildAt(self.background, 1);
} else {
self.removeChild(self.background);
self.background = self.attachAsset('cell', {
anchorX: 0.5,
anchorY: 0.5
});
self.addChildAt(self.background, 1);
}
};
self.down = function (x, y, obj) {
if (!self.isInitial) {
selectedCell = self;
updateCellSelection();
LK.getSound('select').play();
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0xf0f0f0
});
/****
* Game Code
****/
// Game variables
var grid = [];
var selectedCell = null;
var numberButtons = [];
var startTime = Date.now();
var isComplete = false;
// Sudoku generator functions
function createEmptyGrid() {
var grid = [];
for (var i = 0; i < 9; i++) {
grid[i] = [];
for (var j = 0; j < 9; j++) {
grid[i][j] = 0;
}
}
return grid;
}
function isValidPlacement(grid, row, col, num) {
// Check row
for (var c = 0; c < 9; c++) {
if (grid[row][c] === num) return false;
}
// Check column
for (var r = 0; r < 9; r++) {
if (grid[r][col] === num) return false;
}
// Check 3x3 box
var boxRow = Math.floor(row / 3) * 3;
var boxCol = Math.floor(col / 3) * 3;
for (var r = boxRow; r < boxRow + 3; r++) {
for (var c = boxCol; c < boxCol + 3; c++) {
if (grid[r][c] === num) return false;
}
}
return true;
}
function shuffleArray(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
function fillGrid(grid) {
for (var row = 0; row < 9; row++) {
for (var col = 0; col < 9; col++) {
if (grid[row][col] === 0) {
var numbers = shuffleArray([1, 2, 3, 4, 5, 6, 7, 8, 9]);
for (var i = 0; i < numbers.length; i++) {
var num = numbers[i];
if (isValidPlacement(grid, row, col, num)) {
grid[row][col] = num;
if (fillGrid(grid)) {
return true;
}
grid[row][col] = 0;
}
}
return false;
}
}
}
return true;
}
function createPuzzle(difficulty) {
var grid = createEmptyGrid();
fillGrid(grid);
// Create a copy for the solution
var solution = [];
for (var i = 0; i < 9; i++) {
solution[i] = [];
for (var j = 0; j < 9; j++) {
solution[i][j] = grid[i][j];
}
}
// Remove cells based on difficulty
var cellsToRemove = difficulty === 'easy' ? 40 : difficulty === 'medium' ? 45 : 50;
var removed = 0;
while (removed < cellsToRemove) {
var row = Math.floor(Math.random() * 9);
var col = Math.floor(Math.random() * 9);
if (grid[row][col] !== 0) {
grid[row][col] = 0;
removed++;
}
}
return {
puzzle: grid,
solution: solution
};
}
// Generate random puzzle
var gameData = createPuzzle('easy');
var puzzleData = gameData.puzzle;
var solution = gameData.solution;
// Create grid background
var gridBg = game.attachAsset('gridBackground', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 1100
});
// Create 3x3 box backgrounds for visual distinction
for (var boxRow = 0; boxRow < 3; boxRow++) {
for (var boxCol = 0; boxCol < 3; boxCol++) {
var boxIndex = boxRow * 3 + boxCol;
// Add thick border around each box
var boxBorder = game.attachAsset('boxBorder', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024 + (boxCol - 1) * 600,
y: 1100 + (boxRow - 1) * 600
});
// Add colored background
var boxBg = game.attachAsset('boxBackground' + boxIndex, {
anchorX: 0.5,
anchorY: 0.5,
x: 1024 + (boxCol - 1) * 600,
y: 1100 + (boxRow - 1) * 600
});
}
}
// Create Sudoku grid
for (var row = 0; row < 9; row++) {
grid[row] = [];
for (var col = 0; col < 9; col++) {
var cell = new SudokuCell(row, col, puzzleData[row][col]);
cell.x = 1024 + (col - 4) * 200;
cell.y = 1100 + (row - 4) * 200;
grid[row][col] = cell;
game.addChild(cell);
}
}
// Create number buttons
var buttonStartX = 300;
var buttonY = 2400;
for (var i = 1; i <= 9; i++) {
var button = new NumberButton(i);
button.x = buttonStartX + (i - 1) * 180;
button.y = buttonY;
numberButtons.push(button);
game.addChild(button);
}
// Create delete button
var deleteBtn = new DeleteButton();
deleteBtn.x = buttonStartX + 9 * 180;
deleteBtn.y = buttonY;
game.addChild(deleteBtn);
// Create hint button
var hintBtn = new HintButton();
hintBtn.x = 1024;
hintBtn.y = 2550;
game.addChild(hintBtn);
// Create timer display
var timerText = new Text2('Time: 00:00', {
size: 60,
fill: 0x333333
});
timerText.anchor.set(0.5, 0);
LK.gui.top.addChild(timerText);
timerText.y = 50;
// Helper functions
function updateCellSelection() {
for (var row = 0; row < 9; row++) {
for (var col = 0; col < 9; col++) {
grid[row][col].setSelected(grid[row][col] === selectedCell);
}
}
}
function checkForErrors() {
// Clear all errors first
for (var row = 0; row < 9; row++) {
for (var col = 0; col < 9; col++) {
grid[row][col].setError(false);
}
}
// Check for conflicts
for (var row = 0; row < 9; row++) {
for (var col = 0; col < 9; col++) {
var cell = grid[row][col];
if (cell.value > 0) {
if (hasConflict(row, col, cell.value)) {
cell.setError(true);
}
}
}
}
}
function hasConflict(row, col, value) {
// Check row
for (var c = 0; c < 9; c++) {
if (c !== col && grid[row][c].value === value) {
return true;
}
}
// Check column
for (var r = 0; r < 9; r++) {
if (r !== row && grid[r][col].value === value) {
return true;
}
}
// Check 3x3 box
var boxRow = Math.floor(row / 3) * 3;
var boxCol = Math.floor(col / 3) * 3;
for (var r = boxRow; r < boxRow + 3; r++) {
for (var c = boxCol; c < boxCol + 3; c++) {
if ((r !== row || c !== col) && grid[r][c].value === value) {
return true;
}
}
}
return false;
}
function getCorrectValueForCell(row, col) {
return solution[row][col];
}
function checkWinCondition() {
if (isComplete) return;
var allFilled = true;
var hasErrors = false;
for (var row = 0; row < 9; row++) {
for (var col = 0; col < 9; col++) {
var cell = grid[row][col];
if (cell.value === 0) {
allFilled = false;
}
if (cell.hasError) {
hasErrors = true;
}
}
}
if (allFilled && !hasErrors) {
isComplete = true;
LK.getSound('complete').play();
LK.effects.flashScreen(0x00ff00, 1000);
LK.setTimeout(function () {
LK.showYouWin();
}, 1500);
}
}
function formatTime(seconds) {
var mins = Math.floor(seconds / 60);
var secs = seconds % 60;
return 'Time: ' + (mins < 10 ? '0' : '') + mins + ':' + (secs < 10 ? '0' : '') + secs;
}
// Start background music
LK.playMusic('bgmusic');
game.update = function () {
if (!isComplete) {
var currentTime = Math.floor((Date.now() - startTime) / 1000);
timerText.setText(formatTime(currentTime));
}
};