📄 sudoku.js
字号:
timer_paused = 0;
timer();
//Remove the edit mask
var edit_mask = $("pause-hider");
edit_mask.style.display="none";
}
}
//Check all the Rows/Cols for a repeated number
function checker() {
var found = 0;
show("Checking game...","#a84efa");
loop:
for(var a=0;a<9;a++) {
for(var b=0;b<9;b++) {
var ele = cells[a][b];
var id = "c" + (a+1) + (b+1);
var value = ele.value
if(isNaN(value) || value<1 || value>9) {
if(value == "") {
show("Empty cells found.","#d78601");
} else {
show("Invalid entries found.","#d78601");
}
found = 1;
ele.style.background = "red"; //Problem Cells found here.
setTimeout("discolorCells('"+id+"')",2000);
break loop;
}
else if(value) {
if(!checkForUnique(a,b,false)) {
ele.style.background = "red"; //Repated number found here.
document.getElementById(repeated_at).style.background = "red"; //and here.
show("Repeated numbers were found.","#d78601");
setTimeout("discolorCells('"+repeated_at+"','"+id+"')",2000);//Change background back to white after 2 secs
found++;
break loop;
}
} else {
show("Empty cells were found. Please complete the puzzle.","#d78601");
ele.style.background = "red"; //Empty
setTimeout("discolorCells('"+id+"')",2000);
found = 1;
break loop;
}
}
}
if(!found) {
if(compleated)
alert("Sorry - you can't win after giving up. But it is solved.");
else
alert("Congratulations - You have completed the puzzle.");
stopTimer();
show("Game Over","#000000");
}
}
//Clear all the unwanted cells.
function reloadGame() {
str2pos(orginal_game); //Use that to rebuild the game
}
//Solve the game
function solve() {
show("Finding the solution to the game...");
if(confirm("This will automaticaly solve the puzzle for you.\nAre you sure you want to do this?\n")) {
str2pos(this_puzzle);
compleated = 1;
show("Solved the puzzle.");
stopTimer();
}
}
//This is called when the 'How am I doing' Button is clicked. Sees if any repetition is made
// and reports back to the user how many more cells must be filled.
function checkStatus() {
var invalid_cells = 0;
var empty_cells = 0;
for(var a=0;a<9;a++) {
for(var b=0;b<9;b++) {
var ele = cells[a][b];
var value = ele.value
//Find the Invalid cells.
if(isNaN(value) || value<1 || value>9) {
if(value == "") {
empty_cells ++;
} else {
invalid_cells ++;
}
}
//Valid items.
else if(value) {
if(!checkForUnique(a,b,true)) {
ele.style.background = "red"; //Repated number found here.
var id = "c" + (a+1) + (b+1);
$(repeated_at).style.background = "red"; //and here.
setTimeout("discolorCells('"+repeated_at+"','"+id+"')",2000);//Change background back to white after 2 secs
return;
}
} else {
empty_cells ++;
}
}
}
//Show the results on the screen
show("You are doing fine so far. You have "+(invalid_cells+empty_cells) + " more cells to fill.","#006600");
fadeColor("display_area","#ffff00","#ffffff",30,100,"b");
}
////////////////////////////// Pen Based Gaming ///////////////////////////
var pen_selected_digit = 1;
//A cell was clicked - just inserted the selected number into it.
// :EVENT: Called on the onclick event after the pen is picked
function putSelectedDigit(e) {
var cell = findTarget(e);
cell.value = pen_selected_digit;
checkCell();
cell.blur();
}
//Watch the keypresses - if it is a 1-9 digit, use that number as the pen picked number
// :EVENT: called on the onkeypress event.
function monitorKeyboard(e) {
if (!e) var e = window.event;
if (e.keyCode) code = e.keyCode;
else if (e.which) code = e.which;
var character = String.fromCharCode(code);
//If the pressed key IS a number
if(!isNaN(character)) {
var number = Number(character);
if(number) pickPenDigit(number);
}
}
//Pick up the pen. Now when the user clicks on a cell the chosen digit will be inserted there.
function pickPen() {
for(var i=0;i<9;i++) {
for(var j=0;j<9;j++) {
var this_cell = cells[i][j];
if(this_cell.className.indexOf("pen-picked") == -1) this_cell.className+= " pen-picked";//Change the cursor
addEvent(this_cell,"click",putSelectedDigit); //Attach a click event to all the cells
}
}
addEvent(document,"keypress",monitorKeyboard);
$("pen-picks").style.display="inline";
}
//Drop the pen - restore manual editing capability.
function downPen() {
for(var i=0;i<9;i++) {
for(var j=0;j<9;j++) {
var this_cell = cells[i][j];
if(this_cell.className) {
var classes = this_cell.className.toString();
this_cell.className = classes.replace(/pen\-picked/g,"");
}
removeEvent(this_cell,"click",putSelectedDigit);
}
}
removeEvent(document,"keypress",monitorKeyboard);
$("pen-picks").style.display="none";
}
//If the user clicks on any rumber in the keypad, make it the pen digit.
function pickPenDigit(number) {
pen_selected_digit = number;
$("selected-pen-number").firstChild.nodeValue = number;
}
////////////////////////////// Cheats ////////////////////////////
// :EVENT: Happens on the onclick event for all cells at all times.
function cellClicked(e) {
selected_cell = findTarget(e);
}
//Show the number of the selected cell.
function revealCell() {
if(!selected_cell) return;
var str = this_puzzle.replace(/\./g,"");//Remove the '.' in the active puzzle.
var c_xy = selected_cell.id.split(""); //The id will be split - if the id is 'c13' the resulting array will be c,1,3
var box = c_xy[1]-1;
var cell= c_xy[2]-1;
var rest = str.substr(box*9);//Remove the numbers of all the boxes before it.
var number = rest.charAt(cell); //Now get the cell.
selected_cell.value = number;
fadeColor(selected_cell.id,"#ffffff","#000000",10,100,"c");
}
//This will make new puzzles but replacing all numbers in a base game with other numbers - in effect creating
// a entirely new game. This function is sneaky - Big Time
function makeNewOrder(str) {
//Inits
var alpha = " abcdefghijklmnopqrstuvwxyz";
var new_order = "";
//Get random numbers for all alphas - and store it in a array.
numbers = new Array("0");
for(j=0;j<9;j++) {
new_numbers = uniqueRand(numbers); //Give random position for the numbers
numbers.push(new_numbers);
}
//Now change all the alphas back to numbers - with new digits
for(i=0;i<str.length;i++) {
if(str.charAt(i)=="." || str.charAt(i)=="*" || str.charAt(i)=="x" ||
str.charAt(i)=="_" || str.charAt(i)=="-" || str.charAt(i)=="+") { //It is a special char
new_order += "."
} else {
new_order += numbers[alpha.indexOf(str.charAt(i))]
}
}
return new_order;
}
//Creates the puzzle
function newGame() {
chosen = Math.floor((Math.random()*10) / (10/puzzles.length)); //'chosen' puzzle should be random
this_puzzle = makeNewOrder(puzzles[chosen]);
show("");//Clear the display area.
//Clear the existing numbers first
compleated = 0;
clearer();
var last_box_ended_at = 0;
var extra_number_count = 0;
default_number_per_box = $("difficulty").value;
for(var i=0;i<9;i++) {
//Initialisations
var b=0,location_of_fixed_number=0;
var arr_b = new Array();
//Get the numbers for this box from the 'this_puzzle' varaible
var limit = 9;
var this_box = "";
var dot_count = 0;
for(var j=last_box_ended_at; j<last_box_ended_at+limit; j++) {
if(this_puzzle.charAt(j) == ".") {
limit++;
dot_count++;
}
this_box = this_box + this_puzzle.charAt(j);
}
last_box_ended_at = last_box_ended_at + limit;
//Decide how much numbers must appear in this box
number_of_numbers = default_number_per_box - extra_number_count;
extra_number_count = 0;
// 'extra_number_count' is used for reducing the number of populated cells in the next box if
// the current box has more than 4 numbers.
//If the number of dots are more than 4, use it
if (dot_count > number_of_numbers) {
if(rand() > 5) {
extra_number_count = dot_count - number_of_numbers;
}
number_of_numbers = dot_count;
}
//Empty the array.
arr_b = new Array();
//Get the positions in the box and insert the numbers there
for(b=0;b<number_of_numbers;b++) {
location_of_fixed_number = this_box.indexOf(".");
if(location_of_fixed_number + 1) {//If there are dots...
//Remove this dot
this_box = this_box.substring(0,location_of_fixed_number) +
this_box.substring(location_of_fixed_number+1,this_box.length);
} else { //No more dots - get some random locations
location_of_fixed_number = uniqueRand(arr_b); //Give random position for the numbers
location_of_fixed_number--;//uniqueRand gives 1-9. We need it from 0
}
arr_b.push(location_of_fixed_number+1);//Put the number into the don't repeat array
//Get the numbers that should be inserted
insertion_number = this_box.charAt(location_of_fixed_number);
location_of_fixed_number++;//We need this to start from 1 - not from 0
id = "c" + (i+1) + location_of_fixed_number
insert(id,insertion_number); //Show the numbers
}
}
orginal_game = pos2str(2);//Save the game before beginning - for restarting the puzzle
//Initialize the timer.
stopTimer();
timer_seconds = 0;
$("timer").innerHTML="00:00";
//Start the timer
timer();
timer_started = 1;
}
function init() {
//First get the references of all the cells
var cell_ref;
for(var i=1;i<=9;i++) {
for(var j=1;j<=9;j++) {
cell_ref = document.getElementById("c"+i+j);
addEvent(cell_ref,"change",checkCell);//Call the checkCell() function when the user changes the value of a cell.
addEvent(cell_ref,"click",cellClicked);
cells[i-1][j-1] = cell_ref;// :OPTIMIZATION:
}
}
//Load last selected stylesheet.
var new_style_sheet = getCookie("sudoku_options_stylesheet");
if($(new_style_sheet)) {
$(active_stylesheet).disabled=true;
active_stylesheet = new_style_sheet;
$(active_stylesheet).disabled=false;
$("style-selector").value = new_style_sheet;
}
var hinting = getCookie("sudoku_options_live_hint");
if(hinting) {
$("live-hints").checked = hinting;
}
timer_element = $("timer");
newGame();
}
//Saves the chosen options when the user leaves the page.
function exit() {
setCookie("sudoku_options_stylesheet",active_stylesheet);
if($("live-hints").checked) {
setCookie("sudoku_options_live_hint",true);
} else {
setCookie("sudoku_options_live_hint",false);
}
}
window.onload=init;
window.onunload=exit;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -