//CHECKJS  C:\temp\sudoku\blocks.js 11/6/2005 12:30:02 PM
//blocks.js hansonr@stolaf.edu 7:18 AM 11/16/2005
// handles all 3x3 block colset/rowset checking -- somewhat improved
// this is the only part not easily generalizable to nxn Sudoku -- this is tricky

function checkRanges(icontinue){

	// B-type block row/col check. Relatively brute force here;
	// this could be recast in analyzeX format as well
	
	var slist=getBlockRanges()
	if (slist==0)return 0
//docWrite(slist)
	var p=0
	var pt=0
	var b=0
	var s=""
	var st=""
	var D=new Array()
	var isOK=1
	// p is the row-block subset 0, 3, 6
	// R3.3_6  means row 4 subset 3 k=6 -- if this is there, then we have to skip it, as it is this candidate that
	// is in the pair
	kgreen = -1
	for(var i=0;i<9 && isOK;i++)for(var j=0;j<9 && isOK;j++)if(!Data[i][j].N){
		D=Data[i][j]
		b = D.block
		p=j-j%3
		s="R"+i+"."+p+"_"
		for(var k=0;k<9;k++)if(D.Allowed[k] && !D.Impossible[k] && slist.indexOf(s+k)<0) {
			if ((pt=slist.indexOf(k+"_R"+i))>=0){
				eliminateK(D,k,"candidate "+(k+1)+" is locked in another subset of row "+(i+1))
				greenlist+=slist.substring(pt,slist.indexOf(";",pt))+"#"+(k+1);
				if (iseliminated){
					kgreen=k
					addAutoMsg("L","")
				}
				isOK=(!iseliminated||icontinue)
			} else if ((pt=slist.indexOf(k+"_B"+b))>=0 
			  && (st=slist.substring(pt,slist.indexOf(";",pt))).indexOf(D.showinfo)<0){
				eliminateK(D,k,"candidate "+(k+1)+" is locked in another subset of block "+(b+1))
				greenlist+=st+"#"+(k+1)
				if (iseliminated){
					kgreen=k
					addAutoMsg("L","")
				}
				isOK=(!iseliminated||icontinue)
			}
		}
		if(!isOK)break
		p=i-i%3
		s="C"+j+"."+p+"_"
		for(var k=0;k<9;k++)if(D.Allowed[k] && !D.Impossible[k] && slist.indexOf(s+k)<0) {
			if ((pt=slist.indexOf(k+"_C"+j))>=0) {
				eliminateK(D,k,"candidate "+(k+1)+" is locked in another subset of column "+(j+1))
				greenlist+=slist.substring(pt,slist.indexOf(";",pt))+"#"+(k+1)
				if (iseliminated){
					kgreen=k
					addAutoMsg("L","")
				}
				isOK=(!iseliminated||icontinue)
			} else if ((pt=slist.indexOf(k+"_B"+b))>=0 
			  && (st=slist.substring(pt,slist.indexOf(";",pt))).indexOf(D.showinfo)<0){
				eliminateK(D,k,"candidate "+(k+1)+" is locked in another subset of block "+(b+1))
				greenlist+=st+"#"+(k+1)
				if (iseliminated){
					kgreen=k
					addAutoMsg("L","")
				}
				isOK=(!iseliminated||icontinue)
			}
		}
	}

	return !isOK

//	if(!isOK){
//		for(var ii=0;ii<List.length;ii++)getNPossible(List[ii])
//		return !isOK
//	}
//	return !isOK
}

function getBlockRanges(){
	//numbers restricted to a single row of a given block may not be in the same row in another block
	//or a different row of the same block
	//looking for single rows or columns of each block where a number can be found
	//for each number 1-9, this generates a list of row/column subsets that a number MUST be in for given rows or columns

	var slist=""
	var s = ""
	var x=0
	var D={}
	var P={}

	for(var p=0;p<9;p++)for(var k=0;k<9;k++){
		if(xNum(P=Possible.XBlockRow[p][k])==1){ //in only one row of this block
			x=xVal(P)
			s=k+"_R"+x+"."+((p%3)*3)+"_"+k+"="
			x=(x%3)*3
			for(var i=0;i<3;i++){
				D=Blocks[p][i+x]  //this row
				s+=D.showinfo+" "
			}
			slist+=k + "_B" + D.block + "," + s + ";"
		}
		if(xNum(P=Possible.XBlockCol[p][k])==1){ //in only one column of this block
			x=xVal(P)
			s=k+"_C"+x+"."+(Math.floor(p/3)*3)+"_"+k+"="
			x=x%3
			for(var i=0;i<3;i++){
				D=Blocks[p][i*3+x]
				s+=D.showinfo+" "
			}
			slist+=k + "_B" + D.block + "," + s + ";"
		}
		if(xNum(P=Possible.XRowSubset[p][k])==1){ //in only one subset of this row
			x=xVal(P)//first column in this set
			s=k+"_R"+p+"."+x+"_"+k+"="
			for(var i=0;i<3;i++){
				D=Data[p][x+i]  //this row subset
				s+=D.showinfo+" "
			}
			slist+=k + "_B" + D.block + "," + s + ";"
		}
		if(xNum(P=Possible.XColSubset[p][k])==1){ //in only one subset of this column
			x=xVal(P)
			s=k+"_C"+p+"."+x+"_"+k+"="
			for(var i=0;i<3;i++){
				D=Data[x+i][p]  //this col subset
				s+=D.showinfo+" "
			}
			slist+=k + "_B" + D.block + "," + s + ";"
		}

	}
	return slist
}

