// Copyright
// Rajko Zschiegner
// Reichenbacher Str. 20
// D-07973 Greiz

var sudoku = new Array(10);
var s_solving = new Array(10);

function init_arrays() {

	for (i=1;i<10;i++){
		sudoku[i] = new Array(10);
		s_solving[i] = new Array(10);
	}

	sudoku[1][1] = document.sudoku.f11.value;sudoku[1][2] = document.sudoku.f12.value;sudoku[1][3] = document.sudoku.f13.value;
	sudoku[1][4] = document.sudoku.f14.value;sudoku[1][5] = document.sudoku.f15.value;sudoku[1][6] = document.sudoku.f16.value;
	sudoku[1][7] = document.sudoku.f17.value;sudoku[1][8] = document.sudoku.f18.value;sudoku[1][9] = document.sudoku.f19.value;

	sudoku[2][1] = document.sudoku.f21.value;sudoku[2][2] = document.sudoku.f22.value;sudoku[2][3] = document.sudoku.f23.value;
	sudoku[2][4] = document.sudoku.f24.value;sudoku[2][5] = document.sudoku.f25.value;sudoku[2][6] = document.sudoku.f26.value;
	sudoku[2][7] = document.sudoku.f27.value;sudoku[2][8] = document.sudoku.f28.value;sudoku[2][9] = document.sudoku.f29.value;

	sudoku[3][1] = document.sudoku.f31.value;sudoku[3][2] = document.sudoku.f32.value;sudoku[3][3] = document.sudoku.f33.value;
	sudoku[3][4] = document.sudoku.f34.value;sudoku[3][5] = document.sudoku.f35.value;sudoku[3][6] = document.sudoku.f36.value;
	sudoku[3][7] = document.sudoku.f37.value;sudoku[3][8] = document.sudoku.f38.value;sudoku[3][9] = document.sudoku.f39.value;

	sudoku[4][1] = document.sudoku.f41.value;sudoku[4][2] = document.sudoku.f42.value;sudoku[4][3] = document.sudoku.f43.value;
	sudoku[4][4] = document.sudoku.f44.value;sudoku[4][5] = document.sudoku.f45.value;sudoku[4][6] = document.sudoku.f46.value;
	sudoku[4][7] = document.sudoku.f47.value;sudoku[4][8] = document.sudoku.f48.value;sudoku[4][9] = document.sudoku.f49.value;

	sudoku[5][1] = document.sudoku.f51.value;sudoku[5][2] = document.sudoku.f52.value;sudoku[5][3] = document.sudoku.f53.value;
	sudoku[5][4] = document.sudoku.f54.value;sudoku[5][5] = document.sudoku.f55.value;sudoku[5][6] = document.sudoku.f56.value;
	sudoku[5][7] = document.sudoku.f57.value;sudoku[5][8] = document.sudoku.f58.value;sudoku[5][9] = document.sudoku.f59.value;

	sudoku[6][1] = document.sudoku.f61.value;sudoku[6][2] = document.sudoku.f62.value;sudoku[6][3] = document.sudoku.f63.value;
	sudoku[6][4] = document.sudoku.f64.value;sudoku[6][5] = document.sudoku.f65.value;sudoku[6][6] = document.sudoku.f66.value;
	sudoku[6][7] = document.sudoku.f67.value;sudoku[6][8] = document.sudoku.f68.value;sudoku[6][9] = document.sudoku.f69.value;

	sudoku[7][1] = document.sudoku.f71.value;sudoku[7][2] = document.sudoku.f72.value;sudoku[7][3] = document.sudoku.f73.value;
	sudoku[7][4] = document.sudoku.f74.value;sudoku[7][5] = document.sudoku.f75.value;sudoku[7][6] = document.sudoku.f76.value;
	sudoku[7][7] = document.sudoku.f77.value;sudoku[7][8] = document.sudoku.f78.value;sudoku[7][9] = document.sudoku.f79.value;

	sudoku[8][1] = document.sudoku.f81.value;sudoku[8][2] = document.sudoku.f82.value;sudoku[8][3] = document.sudoku.f83.value;
	sudoku[8][4] = document.sudoku.f84.value;sudoku[8][5] = document.sudoku.f85.value;sudoku[8][6] = document.sudoku.f86.value;
	sudoku[8][7] = document.sudoku.f87.value;sudoku[8][8] = document.sudoku.f88.value;sudoku[8][9] = document.sudoku.f89.value;

	sudoku[9][1] = document.sudoku.f91.value;sudoku[9][2] = document.sudoku.f92.value;sudoku[9][3] = document.sudoku.f93.value;
	sudoku[9][4] = document.sudoku.f94.value;sudoku[9][5] = document.sudoku.f95.value;sudoku[9][6] = document.sudoku.f96.value;
	sudoku[9][7] = document.sudoku.f97.value;sudoku[9][8] = document.sudoku.f98.value;sudoku[9][9] = document.sudoku.f99.value;

	for (r=1;r<10;r++) {
		for (c=1;c<10;c++) {
			s_solving[r][c] = new Array(10);
			for (n=1;n<10;n++) {
				s_solving[r][c][n]=1;
			}
		}
	}
	
}

function play_back() {

	document.sudoku.f11.value = sudoku[1][1];document.sudoku.f12.value = sudoku[1][2];document.sudoku.f13.value = sudoku[1][3];
	document.sudoku.f14.value = sudoku[1][4];document.sudoku.f15.value = sudoku[1][5];document.sudoku.f16.value = sudoku[1][6];
	document.sudoku.f17.value = sudoku[1][7];document.sudoku.f18.value = sudoku[1][8];document.sudoku.f19.value = sudoku[1][9];

	document.sudoku.f21.value = sudoku[2][1];document.sudoku.f22.value = sudoku[2][2];document.sudoku.f23.value = sudoku[2][3];
	document.sudoku.f24.value = sudoku[2][4];document.sudoku.f25.value = sudoku[2][5];document.sudoku.f26.value = sudoku[2][6];
	document.sudoku.f27.value = sudoku[2][7];document.sudoku.f28.value = sudoku[2][8];document.sudoku.f29.value = sudoku[2][9];

	document.sudoku.f31.value = sudoku[3][1];document.sudoku.f32.value = sudoku[3][2];document.sudoku.f33.value = sudoku[3][3];
	document.sudoku.f34.value = sudoku[3][4];document.sudoku.f35.value = sudoku[3][5];document.sudoku.f36.value = sudoku[3][6];
	document.sudoku.f37.value = sudoku[3][7];document.sudoku.f38.value = sudoku[3][8];document.sudoku.f39.value = sudoku[3][9];

	document.sudoku.f41.value = sudoku[4][1];document.sudoku.f42.value = sudoku[4][2];document.sudoku.f43.value = sudoku[4][3];
	document.sudoku.f44.value = sudoku[4][4];document.sudoku.f45.value = sudoku[4][5];document.sudoku.f46.value = sudoku[4][6];
	document.sudoku.f47.value = sudoku[4][7];document.sudoku.f48.value = sudoku[4][8];document.sudoku.f49.value = sudoku[4][9];

	document.sudoku.f51.value = sudoku[5][1];document.sudoku.f52.value = sudoku[5][2];document.sudoku.f53.value = sudoku[5][3];
	document.sudoku.f54.value = sudoku[5][4];document.sudoku.f55.value = sudoku[5][5];document.sudoku.f56.value = sudoku[5][6];
	document.sudoku.f57.value = sudoku[5][7];document.sudoku.f58.value = sudoku[5][8];document.sudoku.f59.value = sudoku[5][9];

	document.sudoku.f61.value = sudoku[6][1];document.sudoku.f62.value = sudoku[6][2];document.sudoku.f63.value = sudoku[6][3];
	document.sudoku.f64.value = sudoku[6][4];document.sudoku.f65.value = sudoku[6][5];document.sudoku.f66.value = sudoku[6][6];
	document.sudoku.f67.value = sudoku[6][7];document.sudoku.f68.value = sudoku[6][8];document.sudoku.f69.value = sudoku[6][9];
	
	document.sudoku.f71.value = sudoku[7][1];document.sudoku.f72.value = sudoku[7][2];document.sudoku.f73.value = sudoku[7][3];
	document.sudoku.f74.value = sudoku[7][4];document.sudoku.f75.value = sudoku[7][5];document.sudoku.f76.value = sudoku[7][6];
	document.sudoku.f77.value = sudoku[7][7];document.sudoku.f78.value = sudoku[7][8];document.sudoku.f79.value = sudoku[7][9];
	
	document.sudoku.f81.value = sudoku[8][1];document.sudoku.f82.value = sudoku[8][2];document.sudoku.f83.value = sudoku[8][3];
	document.sudoku.f84.value = sudoku[8][4];document.sudoku.f85.value = sudoku[8][5];document.sudoku.f86.value = sudoku[8][6];
	document.sudoku.f87.value = sudoku[8][7];document.sudoku.f88.value = sudoku[8][8];document.sudoku.f89.value = sudoku[8][9];
	
	document.sudoku.f91.value = sudoku[9][1];document.sudoku.f92.value = sudoku[9][2];document.sudoku.f93.value = sudoku[9][3];
	document.sudoku.f94.value = sudoku[9][4];document.sudoku.f95.value = sudoku[9][5];document.sudoku.f96.value = sudoku[9][6];
	document.sudoku.f97.value = sudoku[9][7];document.sudoku.f98.value = sudoku[9][8];document.sudoku.f99.value = sudoku[9][9];

}

function clear_n_in_col_row_subfield() {
	for (r=1;r<10;r++) {
		for (c=1;c<10;c++) {
			if (sudoku[r][c] > 0) {
				square_r = Math.floor((r-1)/3);
				square_c = Math.floor((c-1)/3);
				for (n=1;n<10;n++) {
					s_solving[r][n][sudoku[r][c]]=0;
					s_solving[n][c][sudoku[r][c]]=0;
					s_solving[r][c][n]=0;
				}
				s_solving[square_r*3+1][square_c*3+1][sudoku[r][c]]=0;
				s_solving[square_r*3+1][square_c*3+2][sudoku[r][c]]=0;
				s_solving[square_r*3+1][square_c*3+3][sudoku[r][c]]=0;
				s_solving[square_r*3+2][square_c*3+1][sudoku[r][c]]=0;
				s_solving[square_r*3+2][square_c*3+2][sudoku[r][c]]=0;
				s_solving[square_r*3+2][square_c*3+3][sudoku[r][c]]=0;
				s_solving[square_r*3+3][square_c*3+1][sudoku[r][c]]=0;
				s_solving[square_r*3+3][square_c*3+2][sudoku[r][c]]=0;
				s_solving[square_r*3+3][square_c*3+3][sudoku[r][c]]=0;
			}
		}
	}
}

function only_1_row_or_col_possible_for_n() {
// rows
	for (n=1;n<10;n++) {
		for (r=0;r<3;r++) {
			for (c=0;c<3;c++) {
				if (((s_solving[r*3+1][c*3+1][n]==1) || (s_solving[r*3+1][c*3+2][n]==1) || (s_solving[r*3+1][c*3+3][n]==1)) && (s_solving[r*3+2][c*3+1][n]==0) && (s_solving[r*3+2][c*3+2][n]==0) && (s_solving[r*3+2][c*3+3][n]==0) && (s_solving[r*3+3][c*3+1][n]==0) && (s_solving[r*3+3][c*3+2][n]==0) && (s_solving[r*3+3][c*3+3][n]==0)) {
					if (c != 0) {s_solving[r*3+1][1][n]=0;s_solving[r*3+1][2][n]=0;s_solving[r*3+1][3][n]=0;}
					if (c != 1) {s_solving[r*3+1][4][n]=0;s_solving[r*3+1][5][n]=0;s_solving[r*3+1][6][n]=0;}
					if (c != 2) {s_solving[r*3+1][7][n]=0;s_solving[r*3+1][8][n]=0;s_solving[r*3+1][9][n]=0;}
				} 
				if (((s_solving[r*3+2][c*3+1][n]==1) || (s_solving[r*3+2][c*3+2][n]==1) || (s_solving[r*3+2][c*3+3][n]==1)) && (s_solving[r*3+1][c*3+1][n]==0) && (s_solving[r*3+1][c*3+2][n]==0) && (s_solving[r*3+1][c*3+3][n]==0) && (s_solving[r*3+3][c*3+1][n]==0) && (s_solving[r*3+3][c*3+2][n]==0) && (s_solving[r*3+3][c*3+3][n]==0)) {
					if (c != 0) {s_solving[r*3+2][1][n]=0;s_solving[r*3+2][2][n]=0;s_solving[r*3+2][3][n]=0;}
					if (c != 1) {s_solving[r*3+2][4][n]=0;s_solving[r*3+2][5][n]=0;s_solving[r*3+2][6][n]=0;}
					if (c != 2) {s_solving[r*3+2][7][n]=0;s_solving[r*3+2][8][n]=0;s_solving[r*3+2][9][n]=0;}
				} 
				if (((s_solving[r*3+3][c*3+1][n]==1) || (s_solving[r*3+3][c*3+2][n]==1) || (s_solving[r*3+3][c*3+3][n]==1)) && (s_solving[r*3+1][c*3+1][n]==0) && (s_solving[r*3+1][c*3+2][n]==0) && (s_solving[r*3+1][c*3+3][n]==0) && (s_solving[r*3+2][c*3+1][n]==0) && (s_solving[r*3+2][c*3+2][n]==0) && (s_solving[r*3+2][c*3+3][n]==0)) {
					if (c != 0) {s_solving[r*3+3][1][n]=0;s_solving[r*3+3][2][n]=0;s_solving[r*3+3][3][n]=0;}
					if (c != 1) {s_solving[r*3+3][4][n]=0;s_solving[r*3+3][5][n]=0;s_solving[r*3+3][6][n]=0;}
					if (c != 2) {s_solving[r*3+3][7][n]=0;s_solving[r*3+3][8][n]=0;s_solving[r*3+3][9][n]=0;}
				}
			}
		}
	}

// cols
	for (n=1;n<10;n++) {
		for (r=0;r<3;r++) {
			for (c=0;c<3;c++) {
				if (((s_solving[r*3+1][c*3+1][n]==1) || (s_solving[r*3+2][c*3+1][n]==1) || (s_solving[r*3+3][c*3+1][n]==1)) && (s_solving[r*3+1][c*3+2][n]==0) && (s_solving[r*3+2][c*3+2][n]==0) && (s_solving[r*3+3][c*3+2][n]==0) && (s_solving[r*3+1][c*3+3][n]==0) && (s_solving[r*3+2][c*3+3][n]==0) && (s_solving[r*3+3][c*3+3][n]==0)) {
					if (r != 0) {s_solving[1][c*3+1][n]=0;s_solving[2][c*3+1][n]=0;s_solving[3][c*3+1][n]=0;}
					if (r != 1) {s_solving[4][c*3+1][n]=0;s_solving[5][c*3+1][n]=0;s_solving[6][c*3+1][n]=0;}
					if (r != 2) {s_solving[7][c*3+1][n]=0;s_solving[8][c*3+1][n]=0;s_solving[9][c*3+1][n]=0;}
				} 
				if (((s_solving[r*3+1][c*3+2][n]==1) || (s_solving[r*3+2][c*3+2][n]==1) || (s_solving[r*3+3][c*3+2][n]==1)) && (s_solving[r*3+1][c*3+1][n]==0) && (s_solving[r*3+2][c*3+1][n]==0) && (s_solving[r*3+3][c*3+1][n]==0) && (s_solving[r*3+1][c*3+3][n]==0) && (s_solving[r*3+2][c*3+3][n]==0) && (s_solving[r*3+3][c*3+3][n]==0)) {
					if (r != 0) {s_solving[1][c*3+2][n]=0;s_solving[2][c*3+2][n]=0;s_solving[3][c*3+2][n]=0;}
					if (r != 1) {s_solving[4][c*3+2][n]=0;s_solving[5][c*3+2][n]=0;s_solving[6][c*3+2][n]=0;}
					if (r != 2) {s_solving[7][c*3+2][n]=0;s_solving[8][c*3+2][n]=0;s_solving[9][c*3+2][n]=0;}
				} 
				if (((s_solving[r*3+1][c*3+3][n]==1) || (s_solving[r*3+2][c*3+3][n]==1) || (s_solving[r*3+3][c*3+3][n]==1)) && (s_solving[r*3+1][c*3+1][n]==0) && (s_solving[r*3+2][c*3+1][n]==0) && (s_solving[r*3+3][c*3+1][n]==0) && (s_solving[r*3+1][c*3+2][n]==0) && (s_solving[r*3+2][c*3+2][n]==0) && (s_solving[r*3+3][c*3+2][n]==0)) {
					if (r != 0) {s_solving[1][c*3+3][n]=0;s_solving[2][c*3+3][n]=0;s_solving[3][c*3+3][n]=0;}
					if (r != 1) {s_solving[4][c*3+3][n]=0;s_solving[5][c*3+3][n]=0;s_solving[6][c*3+3][n]=0;}
					if (r != 2) {s_solving[7][c*3+3][n]=0;s_solving[8][c*3+3][n]=0;s_solving[9][c*3+3][n]=0;}
				}
			}
		}
	}
}

function solve() {
	result = "";
	result_out = "";
	changed = 0;
	counter = 1;
	do {
// go through array and clear solving
		clear_n_in_col_row_subfield();
// find subrows (Zahl kann in 3x3 Block nur in einer bestimmten Zeile oder Spalte stehen)
		only_1_row_or_col_possible_for_n();

		for (r=1;(r<10);r++) {
			for (c=1;(c<10);c++) {
				if (sudoku[r][c] == 0) {
					num_count=0;
					for (n=1;n<10;n++) {
						if (s_solving[r][c][n] == 1) {
							num_count++;
							last_num=n;
						}
					}
					if (num_count == 1) {
						sudoku[r][c] = last_num;
						changed = 1;
					} else {
						square_r = Math.floor((r-1)/3);
						square_c = Math.floor((c-1)/3);
						for (n=1;n<10;n++) {
							if (s_solving[r][c][n] == 1) {
								num_count=0;
								if (s_solving[square_r*3+1][square_c*3+1][n]==1){num_count++;}
								if (s_solving[square_r*3+1][square_c*3+2][n]==1){num_count++;}
								if (s_solving[square_r*3+1][square_c*3+3][n]==1){num_count++;}
								if (s_solving[square_r*3+2][square_c*3+1][n]==1){num_count++;}
								if (s_solving[square_r*3+2][square_c*3+2][n]==1){num_count++;}
								if (s_solving[square_r*3+2][square_c*3+3][n]==1){num_count++;}
								if (s_solving[square_r*3+3][square_c*3+1][n]==1){num_count++;}
								if (s_solving[square_r*3+3][square_c*3+2][n]==1){num_count++;}
								if (s_solving[square_r*3+3][square_c*3+3][n]==1){num_count++;}
								if (num_count == 1) {
									sudoku[r][c] = n;
									changed = 1;
								}
							}
						}
					}
				}
			}
		}
	} while (changed && ((counter--)>0));
}

function loesung() {
	changed=0;
	document.sudoku.comment.value="";
	do {
		init_arrays();
		solve();
		play_back();
	} while (changed);
	document.sudoku.comment.value="Fertig!";
}




