Skip to content

Instantly share code, notes, and snippets.

@farukyildiz
Created March 12, 2017 21:21
Show Gist options
  • Save farukyildiz/37362d45ab6d8c736c9eb1b63c68a2bb to your computer and use it in GitHub Desktop.
Save farukyildiz/37362d45ab6d8c736c9eb1b63c68a2bb to your computer and use it in GitHub Desktop.
Solve Sudoku Puzzle / JavaScript
var NMArray = new Array();
function CrtSdk(){
var BorderColor = "";
var Counter = 0;
var Data = "<table style='width: 40%; margin: auto'>";
for(var i = 1; i < 10; ++i){
Data = Data + "<tr>";
for(var j = 1; j < 10; ++j){
if( j < 4 && i < 4 )
BorderColor = "red";
if( j > 3 && j < 7 && i < 4 )
BorderColor = "blue";
if( j > 6 && i < 4 )
BorderColor = "red"
if( j < 6 && i > 3 && i < 7 )
BorderColor = "blue";
if( j > 6 && i > 3 && i < 7 )
BorderColor = "blue";
if( j > 3 && j < 7 && i > 3 && i < 7 )
BorderColor = "red";
if( j < 4 && i > 6 )
BorderColor = "red";
if( j > 3 && i > 6 )
BorderColor = "blue";
if( j > 6 && i > 6 )
BorderColor = "red"
Data = Data + "<td>";
Data = Data + "<span><input id='NM" + Counter + "' type='number' style='width: 100%;height: 50px; line-height: 50px; border: 5px solid " + BorderColor + "' onkeyup='PushNMArray(this.id, " + i + ", " + j + ", " + 0 + ", " + Counter + ")' /></span>";
Data = Data + "</td>";
NMArray[Counter] = 0;
++Counter;
}
Data = Data + "</tr>";
}
document.getElementById("TBL").innerHTML = Data;
document.getElementById("Play").style.display = "block";
}
function PushNMArray(Id, Row, Column, ColumNM, Counter){
var NM = document.getElementById(Id).value;
if( NM > 0 && NM < 10 ){
for(var i = 0; i < 81; ++i){
if(Counter == i)
{
NMArray[Counter] = NM;
}
}
}
document.getElementById(Id).style.backgroundColor = "yellow";
if( NM == "" || NM > 9 || NM == 0 ){
document.getElementById(Id).style.backgroundColor = "";
document.getElementById(Id).value = "";
}
}
function PlaySDK(){
for(var i = 0; i < 81; ++i){
var StartRow = 0;
var o = 0, t = 0, th = 0, f = 0, fi = 0, si = 0, s = 0, e = 0, n = 0;
if(NMArray[i] == 0 || NMArray[i] == "" ){
o = 1; t = 1; th = 1; f = 1; fi = 1; si = 1; s = 1; e = 1; n = 1;
var Row = parseInt( i / 9 );
for( var j = 0; j < 9; ++j ){
if( NMArray[parseInt(Row) * 9 + j] == 1 )
o = 0;
if( NMArray[parseInt(Row) * 9 + j] == 2 )
t = 0;
if( NMArray[parseInt(Row) * 9 + j] == 3 )
th = 0;
if( NMArray[parseInt(Row) * 9 + j] == 4 )
f = 0;
if( NMArray[parseInt(Row) * 9 + j] == 5 )
fi = 0;
if( NMArray[parseInt(Row) * 9 + j] == 6 )
si = 0;
if( NMArray[parseInt(Row) * 9 + j] == 7 )
s = 0;
if( NMArray[parseInt(Row) * 9 + j] == 8 )
e = 0;
if( NMArray[parseInt(Row) * 9 + j] == 9 )
n = 0;
}
var ColNM = parseInt(i) - ( 9 * parseInt(Row) );
for( var j = 0; j < 9; ++j ){
if( NMArray[(j * 9) + ColNM] == 1 )
o = 0;
if( NMArray[(j * 9) + ColNM] == 2 )
t = 0;
if( NMArray[(j * 9) + ColNM] == 3 )
th = 0;
if( NMArray[(j * 9) + ColNM] == 4 )
f = 0;
if( NMArray[(j * 9) + ColNM] == 5 )
fi = 0;
if( NMArray[(j * 9) + ColNM] == 6 )
si = 0;
if( NMArray[(j * 9) + ColNM] == 7 )
s = 0;
if( NMArray[(j * 9) + ColNM] == 8 )
e = 0;
if( NMArray[(j * 9) + ColNM] == 9 )
n = 0;
}
if( Row < 3 )
StartRow = 0;
if( Row > 2 && Row < 6 )
StartRow = 3;
if( Row > 5 )
StartRow = 6;
for( var j = 0; j < 1; ++j ){
var BLM = parseInt(i) - ( 9 * parseInt(Row) );
if(BLM < 3)
BLM = 0;
if(BLM > 2 && BLM < 6)
BLM = 3;
if(BLM > 5)
BLM = 6;
//alert("b : " + BLM);
for( var l = 0; l < 3; ++l){
if( NMArray[parseInt(StartRow) * 9 + BLM + l] == 1 )
o = 0;
if( NMArray[parseInt(StartRow) * 9 + BLM + l] == 2 )
t = 0;
if( NMArray[parseInt(StartRow) * 9 + BLM + l] == 3 )
th = 0;
if( NMArray[parseInt(StartRow) * 9 + BLM + l] == 4 )
f = 0;
if( NMArray[parseInt(StartRow) * 9 + BLM + l] == 5 )
fi = 0;
if( NMArray[parseInt(StartRow) * 9 + BLM + l] == 6 )
si = 0;
if( NMArray[parseInt(StartRow) * 9 + BLM + l] == 7 )
s = 0;
if( NMArray[parseInt(StartRow) * 9 + BLM + l] == 8 )
e = 0;
if( NMArray[parseInt(StartRow) * 9 + BLM + l] == 9 )
n = 0;
if( NMArray[parseInt(StartRow) * 9 + BLM + l + 9] == 1 )
o = 0;
if( NMArray[parseInt(StartRow) * 9 + BLM + l + 18] == 1 )
o = 0;
if( NMArray[parseInt(StartRow) * 9 + BLM + l + 9] == 2 )
t = 0;
if( NMArray[parseInt(StartRow) * 9 + BLM + l + 18] == 2 )
t = 0;
if( NMArray[parseInt(StartRow) * 9 + BLM + l + 9] == 3 )
th = 0;
if( NMArray[parseInt(StartRow) * 9 + BLM + l + 18] == 3 )
th = 0;
if( NMArray[parseInt(StartRow) * 9 + BLM + l + 9] == 4 )
f = 0;
if( NMArray[parseInt(StartRow) * 9 + BLM + l + 18] == 4 )
f = 0;
if( NMArray[parseInt(StartRow) * 9 + BLM + l + 9] == 5 )
fi = 0;
if( NMArray[parseInt(StartRow) * 9 + BLM + l + 18] == 5 )
fi = 0;
if( NMArray[parseInt(StartRow) * 9 + BLM + l + 9] == 6 )
si = 0;
if( NMArray[parseInt(StartRow) * 9 + BLM + l + 18] == 6 )
si = 0;
if( NMArray[parseInt(StartRow) * 9 + BLM + l + 9] == 7 )
s = 0;
if( NMArray[parseInt(StartRow) * 9 + BLM + l + 18] == 7 )
s = 0;
if( NMArray[parseInt(StartRow) * 9 + BLM + l + 9] == 8 )
e = 0;
if( NMArray[parseInt(StartRow) * 9 + BLM + l + 18] == 8 )
e = 0;
if( NMArray[parseInt(StartRow) * 9 + BLM + l + 9] == 9 )
n = 0;
if( NMArray[parseInt(StartRow) * 9 + BLM + l + 18] == 9 )
n = 0;
}
}
}
if( o == 1 && t == 0 && th == 0 && f == 0 && fi == 0 && si == 0 && s == 0 && e == 0 && n == 0){
document.getElementById("NM" + i).value = "1";
document.getElementById("NM" + i).style.backgroundColor = "lightblue";
NMArray[i] = "1";
}
if( o == 0 && t == 1 && th == 0 && f == 0 && fi == 0 && si == 0 && s == 0 && e == 0 && n == 0){
document.getElementById("NM" + i).value = "2";
document.getElementById("NM" + i).style.backgroundColor = "lightblue";
NMArray[i] = "2";
}
if( o == 0 && t == 0 && th == 1 && f == 0 && fi == 0 && si == 0 && s == 0 && e == 0 && n == 0){
document.getElementById("NM" + i).value = "3";
document.getElementById("NM" + i).style.backgroundColor = "lightblue";
NMArray[i] = "3";
}
if( o == 0 && t == 0 && th == 0 && f == 1 && fi == 0 && si == 0 && s == 0 && e == 0 && n == 0){
document.getElementById("NM" + i).value = "4";
document.getElementById("NM" + i).style.backgroundColor = "lightblue";
NMArray[i] = "4";
}
if( o == 0 && t == 0 && th == 0 && f == 0 && fi == 1 && si == 0 && s == 0 && e == 0 && n == 0){
document.getElementById("NM" + i).value = "5";
document.getElementById("NM" + i).style.backgroundColor = "lightblue";
NMArray[i] = "5";
}
if( o == 0 && t == 0 && th == 0 && f == 0 && fi == 0 && si == 1 && s == 0 && e == 0 && n == 0){
document.getElementById("NM" + i).value = "6";
document.getElementById("NM" + i).style.backgroundColor = "lightblue";
NMArray[i] = "6";
}
if( o == 0 && t == 0 && th == 0 && f == 0 && fi == 0 && si == 0 && s == 1 && e == 0 && n == 0){
document.getElementById("NM" + i).value = "7";
document.getElementById("NM" + i).style.backgroundColor = "lightblue";
NMArray[i] = "7";
}
if( o == 0 && t == 0 && th == 0 && f == 0 && fi == 0 && si == 0 && s == 0 && e == 1 && n == 0){
document.getElementById("NM" + i).value = "8";
document.getElementById("NM" + i).style.backgroundColor = "lightblue";
NMArray[i] = "8";
}
if( o == 0 && t == 0 && th == 0 && f == 0 && fi == 0 && si == 0 && s == 0 && e == 0 && n == 1){
document.getElementById("NM" + i).value = "9";
document.getElementById("NM" + i).style.backgroundColor = "lightblue";
NMArray[i] = "9 ";
}
}
}
<html>
<body>
<span><input id="CRT" type="button" value="Oluştur" onclick="CrtSdk()" style="width: 100%" /></span>
<br/>
<div id="TBL" style="width: 100%; background-color: lightblue; margin-top: 25px">
</div>
<button id="Play" style="width: 100%; margin: auto; margin-top : 25px; display: none" onclick="PlaySDK()">Çöz</button>
</body>
</html>
<script type="text/javascript" src="runjs.js"></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment