⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sudoku.js

📁 sudoku game for javascript
💻 JS
📖 第 1 页 / 共 3 页
字号:
/*
Name    : Online Sudoku
Author  : Binny V A
Version : 2.00.A Beta
Website : http://www.geocities.com/binnyva
This code is Copyright (c) 2005 Binny V Abraham
License is granted to user to reuse this code on other Web site
if, and only if, this entire copyright notice is included. The Web Site
containing this script must be a not-for-profit(non-commercial) web site 
unless I gave permission for the use of the script.
End copyright - This must be retained and posted as is to use this script
along with a link to the original location.
*/

//////////////////////////////////// Global Variables /////////////////////////////////////////
var debug = 0;

var horizondal_line_width = 350;
var vertical_line_height = 370;
var default_number_per_box = 4;
var repeated_at = ""; //The ID of the location where number was repeated.
var compleated = 0;   //This will be one when the User hits the 'I give up' Button

var active_stylesheet="classic-style"; //The used stylesheet
var selected_cell; //The cell that was last clicked on.

//Timer Stuff
var timer_started = 0;
var timer_clock;
var timer_element;
var timer_seconds = 0;
var timer_paused = 0;

//Create a 9x9 2 dimentional Array - Cotains all the values of all the cells
var xy = new Array(9);
for(var i=0;i<9;i++) {
	xy[i] = new Array(9);
}
//Create a 9x9 2 dimentional Array - Contains the references to all the cells
//		- eg. cells[0][0] = $("c11"); etc. :OPTIMIZATION: 
var cells = new Array(9);
for(var i=0;i<9;i++) {
	cells[i] = new Array(9);
}

//Base Games
var puzzles = new Array(
"abc.gdefihdeif.hagcbfgh.cibade.bf.iehdcg.ae.acbf.gid.hghdiace.bficgdabhefh.b.f.cg.eaidde.ahfibcg", //15
"a.bfhiec.g.ddcg.fbai.heiehdg.cabfdegi.hc.fabai.cb.df.eghhf.be.agcidbfi.edhgcac.adgf.iheb.g.hebcaf.di", //19
"bfi.edhgcac.adgf.iheb.g.hebcaf.dia.b.fhiec.g.ddcg.fbai.heiehdg.cabfdegi.hc.fabai.cb.df.eghhf.be.agcid", //20
"ib.ad.c.g.efhe.hc.fbiagd.fg.daehcibbhcgd.faiegf.ei.ab.dchdai.hcebfgfgdcai.he.bbeahd.gc.ifih.c.e.bf.gda", //21
"aeh.dfi.cgbcf.b.e.hgiad.gidc.abfeh.fc.g.b.aeihdb.eifdha.gchda.ig.cebfg.bfhd.aeic.dcegi.f.hbaah.ibce.dfg",
"dah.iegfcbbigfd.cehae.fchab.gi.dbh.fe.gacidcedi.bhgaf.ag.i.d.cfbh.egb.ch.dea.f.ihf.e.aci.d.gbid.a.fbgceh",
"cf.b.ag.eidhhd.eif.cbaga.g.ibdh.fceg.acb.ifhe.d.dehgcafb.i.ifbe.hdg.acdc.iebg.f.haegf.ahd.cibhba.ci.f.deg",
"chfgeai.d.be.gdib.hcf.ab.iacf.dh.gefa.d.hcg.ebigic.bd.eahfeb.hfa.i.gdcd.gh.bica.fe.fei.hagd.cb.a.cbdefihg",
"adbfheig.c.icfba.gdeh.he.gcidfa.bcb.ide.ah.fgag.dh.fbei.cefhigcbd.abi.he.cd.gafg.de.fbach.i.acf.g.hid.b.e",
"ied.a.bfghchcfgeia.b.dagbh.cdi.efb.ied.ca.f.ghcga.ef.hdib.fd.hbig.cae.hdie.fg.cabb.a.e.ihcf.dggfcd.b.aehi",
"h.g.bc.ea.idfa.ci.bf.dgehfe.dih.gcbab.fi.ehg.acdh.dcfabi.geag.edc.ib.fhdah.gbe.ficebf.ci.ad.hggi.ch.df.e.ab",
".e.cadbhfgi.fbhegiad.cg.di.afch.bei.h.fba.g.cd.e.badhceif.g.ec.g.dif.b.hag.ibhe.da.fc.chagifde.bfedcabi.g.h",
"ge.c.h.i.fdabaif.bg.d.echdh.bae.cf.gic.he.fbdig.a.dbageifh.c.ifgca.hb.deb.dh.efg.acicf.e.ia.bhdggia.h.c.d.ebf",
"deb.cg.a.ihff.ah.dbi.egccgi.h.fe.bda.gcdf.bihae.a.fb.hd.ec.i.geihg.acfb.dbi.ca.d.hefgge.dbc.fi.haah.f.ie.gdcb"
);

var chosen = 0;
var this_puzzle = ""; //The currently played game's fingerprint
var orginal_game = ""; //The current puzzle

//////////////////////////////////// Cookie Functions ////////////////////////////////
var sudoku_fingerprint = getCookie("sudoku_fingerprint"); 	  // retrieve all the values
if (sudoku_fingerprint == null || sudoku_fingerprint == "") sudoku_fingerprint=""; //or define default values
function getCookie(name) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}
function setCookie(name, value) {
	var today = new Date();
	today.setTime( today.getTime() );
	var expires = 28;
	expires = expires * 1000 * 60 * 60 * 24;
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+"="+escape( value ) +
		( ( expires ) ? ";expires="+expires_date.toGMTString() : "" )
}

////////////////////////////// Non Game Functions ///////////////////////////////////
//Changes the style sheet that is in use.
function selectStyleSheet() {
	$(active_stylesheet).disabled=true;
	active_stylesheet = $("style-selector").value;
	$(active_stylesheet).disabled=false;
}

function $(id) { return document.getElementById(id); }
function addEvent(elm, evType, fn) {
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, false);
		return true;
	}
	else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	}
	else {
		elm['on' + evType] = fn;
	}
}
function removeEvent(elm, evType, fn) {
	if (elm.removeEventListener) {
		elm.removeEventListener(evType, fn, false);
		return true;
	}
	else if (elm.detachEvent) {
		var r = elm.detachEvent('on' + evType, fn);
		return r;
	}
	else {
		elm['on' + evType] = '';
	}
}
//Returns the target of the event.
function findTarget(e) {
	var element;
	if (!e) var e = window.event;
	if (e.target) element = e.target;
	else if (e.srcElement) element = e.srcElement;
	if (element.nodeType == 3) element = element.parentNode;// defeat Safari bug
	return element;
}


////////////////////////////// Colour Functions /////////////////////////////////////
//Convert a hex value to its decimal value - the inputed hex must be in the
//	format of a hex triplet - the kind we use for HTML colours. The function
//	will return an array with three values.
function hex2num(hex) {
	if(hex.charAt(0) == "#") {	
		hex = hex.slice(1);
	}
	hex = hex.toUpperCase();
	var hex_alphabets = "0123456789ABCDEF";
	var value = new Array(3);
	var k = 0;
	var int1,int2;
	for(var i=0;i<6;i+=2) {
		int1 = hex_alphabets.indexOf(hex.charAt(i));
		int2 = hex_alphabets.indexOf(hex.charAt(i+1)); 
		value[k] = (int1 * 16) + int2;
		k++;
	}
	return(value);
}
//Give a array with three values as the argument and the function will return
//	the corresponding hex triplet.
function num2hex(triplet) {
	var hex_alphabets = "0123456789ABCDEF";
	var hex = "#";
	var int1,int2;
	for(var i=0;i<3;i++) {
		int1 = triplet[i] / 16;
		int2 = triplet[i] % 16;

		hex += hex_alphabets.charAt(int1) + hex_alphabets.charAt(int2); 
	}
	return(hex);
}

function fadeColor(id,start_hex,stop_hex,difference,delay,color_background) {
	//Default values...
	if(!difference) difference = 20;
	if(!delay) delay = 100;
	if(!start_hex) start_hex = "#FFFFFF";
	if(!stop_hex) stop_hex = "#000000";
	if(!color_background) color_background = "c";
	
	var ele = document.getElementById(id);
	if(!ele) return;
	var start= hex2num(start_hex);
	var stop = hex2num(stop_hex);
	
	//Make it numbers rather than strings.
	for(var i=0;i<3;i++) {
		start[i] = Number(start[i]);
		stop[i] = Number(stop[i]);
	}

	//Morph one colour to the other. If the start color is greater than the stop colour, start color will
	//	be decremented till it reaches the stop color. If it is lower, it will incremented.
	for(var i=0;i<3;i++) {
		if (start[i] < stop[i]) {
			start[i] += difference;
			if(start[i] > stop[i]) start[i] = stop[i];//If we have overshot our target, make it equal - or it won't stop.
		}
		else if(start[i] > stop[i]) {
			start[i] -= difference;
			if(start[i] < stop[i]) start[i] = stop[i];
		}
	}

	//Change the color(or the background color).
	var color = "rgb("+start[0]+","+start[1]+","+start[2]+")";
	if(color_background == "b") {
		ele.style.backgroundColor = color;
	} else {
		ele.style.color = color;
	}

	//Stop if we have reached the target.
	if((start[0] == stop[0]) && (start[1] == stop[1]) && (start[2] == stop[2])) return;

	start_hex = num2hex(start);
	//Keep calling this function
	window.setTimeout("fadeColor('"+id+"','"+start_hex+"','"+stop_hex+"',"+difference+","+delay+",'"+color_background+"')",delay);
}

//////////////////////////////////// Co-Ordinates Functions ///////////////////////////////
//Get the index from the given two x and y co-ordinates and return it.
//	  - Takes 2 and 3 and returns 6.
function getIndex(x,y) {
	var index = (y*3) + x - 3;
	return index;
}

//Returns the x and y co-ordinates based on the index given as argument.
//		- Takes 6 and returns 2 and 3 as an array
function getXY(index) {
	var x=1,y=1;
	switch (index) { 
		case 1: y=1; x=1; break;
		case 2: y=1; x=2; break;
		case 3: y=1; x=3; break;
		case 4: y=2; x=1; break;
		case 5: y=2; x=2; break;
		case 6: y=2; x=3; break;
		case 7: y=3; x=1; break;
		case 8: y=3; x=2; break;
		case 9: y=3; x=3; break;
	}
	return Array(x,y);
}

////////////////////////////////////// Random Number Genarators //////////////////////////////////////
//Returns a random number between 1 and 9(inclusive)
function rand() {
	var number = Math.round(Math.random()*10);
	while (number < 1 || number > 9) { //If the number is 0 or 10, get another number.
		number = Math.round(Math.random()*10);
	}
	return number; 
}

//Returns a random number(1-9) that is not in the list given as the argument
function uniqueRand(list) {
	var number = rand();

	for(var a=0;a<list.length;a++) {
		if(list[a] == number) { //If the random number was found in the list,
			number = rand(); //get a new number,
			a=-1; //and start over again.
		}
	}
	return number;
}

/////////////////////////////////////// GUI Functions ////////////////////////////////////
//Display the given data in the said colour 
function show(data,color) {
	document.getElementById("display_area").innerHTML = data + "<br />\n";
	document.getElementById("display_area").style.color = color;
}

//Display the Vertical help line
function helpLineV(line) {
	var item = document.getElementById("lv-"+line).style;
	if(item.position == "absolute") {
		item.position = "relative";
		item.height = "10px";
	} else {   
		item.position = "absolute";
		item.height = vertical_line_height + "px";
	}
}
//Display the Horizondal Help line
function helpLineH(line) {
	var item = document.getElementById("lh-"+line).style;
	if(item.position == "absolute") {
		item.position = "relative";
		item.width = "5px";
	} else {
		item.position = "absolute";
		item.width = horizondal_line_width + "px";
	}
}

//See if the box and the box that should be checked is horizondal to each other.
function isHorizondal(box,check_in) {
	if((box == 1 || box == 2 || box == 3) & (check_in == 1 || check_in == 2 || check_in == 3)) return true;
	if((box == 4 || box == 5 || box == 6) & (check_in == 4 || check_in == 5 || check_in == 6)) return true;
	if((box == 7 || box == 8 || box == 9) & (check_in == 7 || check_in == 8 || check_in == 9)) return true;
	return false;
}

//See if the box and the box that should be checked is verical to each other.
function isVertical(box,check_in) {
	if((box == 1 || box == 4 || box == 7) & (check_in == 1 || check_in == 4 || check_in == 7)) return true;
	if((box == 2 || box == 5 || box == 8) & (check_in == 2 || check_in == 5 || check_in == 8)) return true;
	if((box == 3 || box == 6 || box == 9) & (check_in == 3 || check_in == 6 || check_in == 9)) return true;
	return false;
}

//Returns false if the 'number' given as the argument appears anywhere in the previous row or column.
//		  Full_Check is done if it is called from the hinting system - then the all the cells must be
//		  checked instead of just the ones before the current cell. 
function checkForUnique(box,cell,full_check) {
	box ++;
	cell++;

	var number = cells[box-1][cell-1].value;

	//There is nothing before 1, so we don't have to check it - unless we are doing a full check.
	if(box>1 || full_check) {
	//All the boxes we will have to check for duplicates for this box
	// 	  If it is NOT a full search, we just need to check the boxes directly above or left of this box.
	var boxes_to_check = new Array(); 
	switch (box) {
		case 1 : if(full_check) {
				 	boxes_to_check.push(2,3,4,7);
				 }
			   	 break;
		case 2 : boxes_to_check.push(1);
			   	 if(full_check) {
				 	boxes_to_check.push(3,5,8);
				 }
			   	 break;
		case 3 : boxes_to_check.push(1,2);
			   	 if(full_check) {
				 	boxes_to_check.push(6,9);
				 }
				 break;
		case 4 : boxes_to_check.push(1);
			   	 if(full_check) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -