/*
#####################################
# SU DO KU #
# tested in Turbo C++,version 3.0 #
#################9/3/2008############
*/
#include <time.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
struct { short data, fixed; } cell[9*9];
void Put(int x, int y, char *str)
{
gotoxy(x, y), cprintf("%s", str);
}
void HideCurser()
{
textattr(BLUE*16+BLUE), Put(1, 1, " "), gotoxy(1, 1);
}
void DrawTable()
{
textbackground(BLUE), clrscr();
textcolor(LIGHTRED);
Put(12, 3, "ÉÍÍÍÍÍÑÍÍÍÍÍÑÍÍÍÍÍËÍÍÍÍÍÑÍÍÍÍÍÑÍÍÍÍÍËÍÍÍÍÍÑÍÍÍÍÍÑÍÍÍÍÍ»");
Put(12, 4, "º ³ ³ º ³ ³ º ³ ³ º");
Put(12, 5, "ÇÄÄÄÄÄÅÄÄÄÄÄÅÄÄÄÄÄ×ÄÄÄÄÄÅÄÄÄÄÄÅÄÄÄÄÄ×ÄÄÄÄÄÅÄÄÄÄÄÅÄÄÄÄĶ");
Put(12, 6, "º ³ ³ º ³ ³ º ³ ³ º");
Put(12, 7, "ÇÄÄÄÄÄÅÄÄÄÄÄÅÄÄÄÄÄ×ÄÄÄÄÄÅÄÄÄÄÄÅÄÄÄÄÄ×ÄÄÄÄÄÅÄÄÄÄÄÅÄÄÄÄĶ");
Put(12, 8, "º ³ ³ º ³ ³ º ³ ³ º");
Put(12, 9, "ÌÍÍÍÍÍØÍÍÍÍÍØÍÍÍÍÍÎÍÍÍÍÍØÍÍÍÍÍØÍÍÍÍÍÎÍÍÍÍÍØÍÍÍÍÍØÍÍÍÍ͹");
Put(12, 10, "º ³ ³ º ³ ³ º ³ ³ º");
Put(12, 11, "ÇÄÄÄÄÄÅÄÄÄÄÄÅÄÄÄÄÄ×ÄÄÄÄÄÅÄÄÄÄÄÅÄÄÄÄÄ×ÄÄÄÄÄÅÄÄÄÄÄÅÄÄÄÄĶ");
Put(12, 12, "º ³ ³ º ³ ³ º ³ ³ º");
Put(12, 13, "ÇÄÄÄÄÄÅÄÄÄÄÄÅÄÄÄÄÄ×ÄÄÄÄÄÅÄÄÄÄÄÅÄÄÄÄÄ×ÄÄÄÄÄÅÄÄÄÄÄÅÄÄÄÄĶ");
Put(12, 14, "º ³ ³ º ³ ³ º ³ ³ º");
Put(12, 15, "ÌÍÍÍÍÍØÍÍÍÍÍØÍÍÍÍÍÎÍÍÍÍÍØÍÍÍÍÍØÍÍÍÍÍÎÍÍÍÍÍØÍÍÍÍÍØÍÍÍÍ͹");
Put(12, 16, "º ³ ³ º ³ ³ º ³ ³ º");
Put(12, 17, "ÇÄÄÄÄÄÅÄÄÄÄÄÅÄÄÄÄÄ×ÄÄÄÄÄÅÄÄÄÄÄÅÄÄÄÄÄ×ÄÄÄÄÄÅÄÄÄÄÄÅÄÄÄÄĶ");
Put(12, 18, "º ³ ³ º ³ ³ º ³ ³ º");
Put(12, 19, "ÇÄÄÄÄÄÅÄÄÄÄÄÅÄÄÄÄÄ×ÄÄÄÄÄÅÄÄÄÄÄÅÄÄÄÄÄ×ÄÄÄÄÄÅÄÄÄÄÄÅÄÄÄÄĶ");
Put(12, 20, "º ³ ³ º ³ ³ º ³ ³ º");
Put(12, 21, "ÈÍÍÍÍÍÏÍÍÍÍÍÏÍÍÍÍÍÊÍÍÍÍÍÏÍÍÍÍÍÏÍÍÍÍÍÊÍÍÍÍÍÏÍÍÍÍÍÏÍÍÍÍͼ");
textcolor(RED);
Put(26, 23, "ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿");
Put(26, 24, "³ ³");
Put(26, 25, "ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ");
}
void ShowCell(int index, int bg)
{
gotoxy(14+index%9*6, 4+index/9*2);
textattr((cell[index].fixed==2 ? LIGHTCYAN : LIGHTGREEN)+16*(bg ? BLACK : BLUE));
cprintf(" %c ", (cell[index].data ? cell[index].data+'0' : ' '));
}
void ShowSquare()
{
int i;
for(i=0; i<81; i++)
ShowCell(i, 0);
HideCurser();
}
int PutCell(int ix, int d)
{
int i, j;
for(i=0; i<9; i++)
if(cell[ix/9*9+i].data==d||cell[i*9+ix%9].data==d||cell[ix/27*27+ix%9/3*3+i/3*9+i%3].data==d)
return 0;
cell[ix].data=d;
return 1;
}
int Putting(int index)
{
int data=cell[index].data;
while(1)
{
if(data++ == 9)
return 0;
if(PutCell(index, data))
break;
}
return 1;
}
void SetCell()
{
int i, index;
while(1)
{
for(i=0; i<81; i++)
cell[i].data= cell[i].fixed= 0;
for(i=0; i<18; i++)
{
index=random(81);
if(cell[index].data || !PutCell(index, 1+random(9)))
i--;
else
cell[index].fixed= 2;
}
if(Solving())
{
for(i=0; i<81; i++)
cell[i].fixed= 0;
for(i=0; i<36; i++)
{
index=random(81);
if(cell[index].fixed)
i--;
else
cell[index].fixed= 2;
}
for(i=0; i<81; i++)
if(!cell[i].fixed)
cell[i].data=0;
return;
}
}
}
int Solving()
{
int i, timeSenser=0;
for(i=0; i<81; i++)
{
if(timeSenser++ == 10000)
return 0;
if(!cell[i].fixed)
{
if(!Putting(i))
{
cell[i].data= 0;
while(1)
{
if(i-- == 0)
return 0;
if(!cell[i].fixed)
break;
}
i--;
}
}
}
return 1;
}
void GiveClue(int index)
{
int i, data, clue[10];
if(cell[index].fixed)
Put(31, 24, " ");
else
{
for(i=1; i<=9; i++)
clue[i]= 0;
for(i=0; i<9; i++)
{
if(data=cell[index/9*9+i].data)
clue[data]= 1;
if(data=cell[i*9+index%9].data)
clue[data]= 1;
if(data=cell[index/27*27+index%9/3*3+i/3*9+i%3].data)
clue[data]= 1;
}
for(i=0; i<9; i++)
{
if((data=cell[index/9*9+i].data) && cell[index/9*9+i].fixed)
clue[data]= 2;
if((data=cell[i*9+index%9].data) && cell[i*9+index%9].fixed)
clue[data]= 2;
if((data=cell[index/27*27+index%9/3*3+i/3*9+i%3].data) && cell[index/27*27+index%9/3*3+i/3*9+i%3].fixed)
clue[data]= 2;
}
gotoxy(31, 24);
for(i=1; i<=9; i++)
textcolor(clue[i]==2 ? BLUE : (clue[i]==1 ? BLACK : WHITE)), cprintf("%d ", i);
}
}
int GetKey()
{
char ch;
int i, oldIndex=0, index=0;
while(1)
{
GiveClue(index);
ShowCell(oldIndex, 0);
ShowCell(index, 1);
oldIndex= index;
HideCurser();
ch= getch();
if(ch == 27)
exit(0);
else if(ch == 13)
return 0;
else if((ch=='0' || ch==' ' || ch=='\b') && !cell[index].fixed)
cell[index].data= 0;
else if(isdigit(ch) && !cell[index].fixed)
{
if(PutCell(index, ch-'0'))
{
for(i=0; i<81; i++)
if(!cell[i].data)
break;
if(i == 81)
return 1;
}
}
else if(ch == 0)
{
switch(getch())
{
case 83: if(!cell[index].fixed)
cell[index].data= 0; break;
case 72: if(index/9 != 0) index-= 9; break;
case 75: if(index%9 != 0) index-= 1; break;
case 77: if(index%9 != 8) index+= 1; break;
case 80: if(index/9 != 8) index+= 9; break;
}
}
}
}
void Game()
{
int i;
SetCell();
ShowSquare();
if(GetKey())
textcolor(WHITE), Put(31, 24, " congratulations "), ShowSquare();
else
{
ShowSquare();
for(i=0; i<81; i++)
if(cell[i].data && cell[i].fixed!=2)
cell[i].fixed=1;
textcolor(WHITE);
if(Solving())
Put(28, 24, "It May be Your solution "), ShowSquare();
else
Put(28, 24, "You may not get solution");
}
HideCurser();
if(getch() == 27)
exit(0);
Put(28, 24, " ");
HideCurser();
}
void Instructions()
{
textbackground(BLUE), clrscr();
textcolor(LIGHTGREEN);
Put(37, 6, "SU DO KU");
Put(4, 9, "Enter key for new game");
Put(4, 16, "Esc key to exit the game");
textcolor(RED);
Put(36, 7, "ßßßßßßßßßß");
Put(4, 10, "ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ");
Put(4, 17, "ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ");
textcolor(WHITE);
Put(9, 11, "When the enter key is pressed, the current sudoku,");
Put(5, 12, "including the numbers you inserted, is solved.");
Put(5, 13, "If they cannot be solved, \"You may not get solution\" will be displayed.");
Put(5, 14, "Otherwise; \"It May be Your solution\" will be displayed.");
textcolor(LIGHTRED);
Put(13, 24, "For more programs: http://programs.on.googlepages.com");
HideCurser(), getch();
}
void main()
{
textmode(C80);
randomize();
Instructions();
DrawTable();
while(1) Game();
}
/*******************************************************
* More programs: http://programs.on.googlepages.com *
* Please report bugs to : programs.on@gmail.com *
********************************************308*lines**/