Last active
December 21, 2015 14:59
-
-
Save OmarJH/6323762 to your computer and use it in GitHub Desktop.
Sudoku solver on c++ via console
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
using namespace std; | |
#include <windows.h> | |
HANDLE outcon = GetStdHandle(STD_OUTPUT_HANDLE); | |
bool end22 = false; | |
bool solved = false; | |
void gotoxy(short x, short y) | |
{ | |
COORD coord = { x, y }; | |
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); | |
} | |
void printSCH() | |
{ | |
for (int i = 0; i<37; i++) | |
{ | |
SetConsoleTextAttribute(outcon, 0xcc); | |
cout << "-"; | |
SetConsoleTextAttribute(outcon, 0xf0); | |
} | |
for (int j = 1; j<10; j++) | |
{ | |
cout << endl; | |
for (int i = 1; i<19; i++) | |
{ | |
if ((i % 2 != 0)&(i != 1) || (i == 2)) | |
{ | |
for (int i=0;i<3;i++) | |
cout << " "; | |
} | |
if ((i % 2 == 0)&(i % 6 != 0)&(i != 1)) | |
cout << "|"; | |
if ((i % 6 == 0) || (i == 1) || (i == 18)) | |
{ | |
SetConsoleTextAttribute(outcon, 0xcc); | |
cout << "|"; | |
SetConsoleTextAttribute(outcon, 0xf0); | |
} | |
} | |
cout << endl; | |
if (j % 3 == 0) | |
{ | |
for (int i = 0; i<37; i++) | |
{ | |
SetConsoleTextAttribute(outcon, 0xcc); | |
cout << "-"; | |
SetConsoleTextAttribute(outcon, 0xf0); | |
} | |
} | |
else | |
{ | |
for (int i = 0; i<37; i++) | |
cout << "-"; | |
} | |
} | |
} | |
void Input(int a[][10]) | |
{ | |
SetConsoleTextAttribute(outcon, 0xf9); | |
for (int y = 1, j = 1; j<10; y = y + 2, j++) | |
{ | |
for (int x = 2, i = 1; i<10; x = x + 4, i++) | |
{ | |
gotoxy(x, y); | |
cin >> a[i][j]; | |
} | |
} | |
} | |
void print_numm(int a[][10]) | |
{ | |
for (int i = 1; i<10; i++) | |
{ | |
for (int j = 1; j<10; j++) | |
{ | |
gotoxy(2 + 4 * (i - 1), 1 + 2 * (j - 1)); | |
cout << a[i][j]; | |
} | |
} | |
} | |
class solve | |
{ | |
private: | |
int a[10][10], b[10]; | |
int x, y, r, f, cxy, k, n; | |
public: | |
void build(int c[][10], int s, int d) | |
{ | |
for (int j = 1; j<10; j++) | |
{ | |
for (int i = 1; i<10; i++) | |
{ | |
a[i][j] = c[i][j]; | |
} | |
} | |
x = s; | |
y = d; | |
} | |
void horizontal() | |
{ | |
cxy = 0; | |
for (int i = 0; i<10; i++) | |
{ | |
b[i] = i; | |
} | |
r = 0; | |
f = 0; | |
k = 0; | |
for (int i = 1; i<10; i++) | |
{ | |
if (a[i][y] != 0) | |
{ | |
int s = a[i][y]; | |
b[s] = 0; | |
//r++; | |
} | |
} | |
} | |
void vertical() | |
{ | |
for (int i = 1; i<10; i++) | |
{ | |
if (a[x][i] != 0) | |
{ | |
int s = a[x][i]; | |
b[s] = 0; | |
} | |
} | |
} | |
void squere() | |
{ | |
k = 0; | |
int t, g, h, l; | |
if ((x<4)&(y<4)) | |
t = 4, g = 4, h = 1, l = 1; | |
if ((x>3)&(y<4)) | |
t = 7, g = 4, h = 4, l = 1; | |
if ((x>6)&(y<4)) | |
t = 10, g = 4, h = 7, l = 1; | |
if ((x<4)&(y>3)) | |
t = 4, g = 7, h = 1, l = 4; | |
if ((x>3)&(y>3)) | |
t = 7, g = 7, h = 4, l = 4; | |
if ((x>6)&(y>3)) | |
t = 10, g = 7, h = 7, l = 4; | |
if ((x<4)&(y>6)) | |
t = 4, g = 10, h = 1, l = 7; | |
if ((x>3)&(y>6)) | |
t = 7, g = 10, h = 4, l = 7; | |
if ((x>6)&(y>6)) | |
t = 10, g = 10, h = 7, l = 7; | |
for (int j = l; j<g; j++) | |
{ | |
for (int i = h; i<t; i++) | |
{ | |
if (a[i][j] != 0) | |
{ | |
int s = a[i][j]; | |
b[s] = 0; | |
} | |
} | |
} | |
for (int i = 1; i<10; i++) | |
{ | |
if (b[i] != 0) | |
{ | |
n = b[i]; | |
k++; | |
} | |
} | |
if (k == 1) | |
{ | |
a[x][y] = n; | |
gotoxy(2 + 4 * (x - 1), 1 + 2 * (y - 1)); | |
cout << a[x][y]; | |
cxy = a[x][y]; | |
f = 1; | |
} | |
if (k == 0) | |
{ | |
gotoxy(0, 25); | |
cout << "sorrrrry" << endl; | |
end22 = true; | |
} | |
} | |
int forb() | |
{ | |
if (f == 0) | |
return 0; | |
if (f == 1) | |
return 1; | |
} | |
int getcxy() | |
{ | |
return cxy; | |
} | |
int random(int aaa[][10]) | |
{ | |
newstart: | |
int aa[10][10]; | |
for (int j = 1; j<10; j++) | |
{ | |
for (int i = 1; i<10; i++) | |
{ | |
aa[i][j] = aaa[i][j]; | |
} | |
} | |
bool able = false; | |
for (int i = 1; i<10; i++) | |
{ | |
if (b[i] != 0) | |
{ | |
aa[x][y] = b[i]; | |
gotoxy(2 + 4 * (x - 1), 1 + 2 * (y - 1)); | |
cout << aa[x][y]; | |
b[i] = 0; | |
able = true; | |
break; | |
} | |
} | |
if (able == false) | |
return 1; | |
int f, forb, forbb; | |
solve ob[10][10]; | |
while (1) | |
{ | |
forbb = 0; | |
for (int i = 1; i<10; i++) | |
{ | |
for (int j = 1; j<10; j++) | |
{ | |
if (aa[i][j] == 0) | |
{ | |
ob[i][j].build(aa, i, j); | |
ob[i][j].horizontal(); | |
ob[i][j].vertical(); | |
ob[i][j].squere(); | |
aa[i][j] = ob[i][j].getcxy(); | |
forb = ob[i][j].forb(); | |
//print_numm(aa); | |
if (forb == 1) | |
forbb = 1; | |
} | |
} | |
} | |
f = 0; | |
for (int i = 1; i<10; i++) | |
{ | |
for (int j = 1; j<10; j++) | |
{ | |
if (aa[i][j] == 0) | |
f = 1; | |
} | |
} | |
if ((f == 0) || (forbb == 0)) | |
break; | |
} | |
gotoxy(0, 25); | |
if (end22 == true) | |
{ | |
end22 = false; | |
goto newstart; | |
} | |
if (forbb == 0) | |
{ | |
for (int i = 1; i<10; i++) | |
{ | |
for (int j = 1; j<10; j++) | |
{ | |
if (aa[i][j] == 0) | |
{ | |
if (ob[i][j].random(aa) == 1) | |
{ | |
goto newstart; | |
} | |
else goto end; | |
} | |
} | |
} | |
} | |
end: | |
return 0; | |
} | |
}; | |
void main() | |
{ | |
int a[10][10], f, forb, forbb, count = 0; | |
system("Color f0"); | |
string x; | |
printSCH(); | |
Input(a); | |
SetConsoleTextAttribute(outcon, 0xf4); | |
solve ob[10][10]; | |
while (1) | |
{ | |
forbb = 0; | |
for (int i = 1; i<10; i++) | |
{ | |
for (int j = 1; j<10; j++) | |
{ | |
if (a[i][j] == 0) | |
{ | |
ob[i][j].build(a, i, j); | |
ob[i][j].horizontal(); | |
ob[i][j].vertical(); | |
ob[i][j].squere(); | |
a[i][j] = ob[i][j].getcxy(); | |
forb = ob[i][j].forb(); | |
if (forb == 1) | |
forbb = 1; | |
} | |
} | |
} | |
f = 0; | |
for (int i = 1; i<10; i++) | |
{ | |
for (int j = 1; j<10; j++) | |
{ | |
if (a[i][j] == 0) | |
f = 1; | |
} | |
} | |
if ((f == 0) || (forbb == 0)) | |
break; | |
} | |
gotoxy(0, 25); | |
if (end22 == true) | |
goto ohhh; | |
if (forbb == 0) | |
{ | |
for (int i = 1; i<10; i++) | |
{ | |
for (int j = 1; j<10; j++) | |
{ | |
if (a[i][j] == 0) | |
{ | |
start: | |
if (ob[i][j].random(a) == 1) | |
{ | |
cout << "NO solution" << endl; | |
goto ohhh; | |
} | |
} | |
else goto ohhh; | |
} | |
} | |
} | |
cout << "end!!!!" << endl; | |
ohhh: | |
cin >> x; | |
} |
الله يعطيك العافية ^_^ حضارة =D
الله يعافكي :)
Amazing
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
سودوكووووووووو