/*
nsudoku 1.0
gcc -o nsudoku nsudoku.c -lncurses
Copyright (c) 2008 Tin Benjamin Matuka
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
To compile nsudoku run
$ gcc -lncurses -o nsudoku nsudoku.c
I have no intention of updating this game, because I made it
just to learn how to work with ncurses. You can use the code
if you want to.
For more information visit nsudoku web site:
http://www.sh1fty.com/nsudoku
I can be contacted at sh1fty[at]sh1fty[dot]com
*/
#include<stdio.h>
#include<time.h>
#include<ncurses.h>
int board[9][9], start[9][9];
WINDOW *helpbox, *win;
int main( int argc, char *argv[] )
{
int n;
int check( void ); // check if all numbers in board array match the rules
int clear_grid( int i, int j ); // clear a grid(3x3) in row i and column j
int count( void ); // count how many values are in board array
int generate( int n ); // generate sudoku
int stdprint( void ); // stdout print board array
int change( int x, int y, int num ); // change a value of an array field.
int helpboxon( void ); // turn HelpBox on
int helpboxoff( void ); // turn HelpBox off
srand( time( NULL ) );
if(argc>1)
{
n=atoi(argv[1]);
if(n<0 || n>80) n=40;
int ch,curx=0,cury=0,i,j,hb=1;
int maxx,maxy,startx,starty;
int posx=0,posy=0;
generate(n);
initscr();
getmaxyx(stdscr,maxy,maxx);
startx=(maxx-38)/2;
starty=2;
noecho();
start_color();
init_pair(1,COLOR_CYAN,COLOR_BLACK);
keypad(stdscr, TRUE);
win=newwin(25,37,starty,startx);
helpbox=newwin(4,15,1,1);
wbkgd(win,COLOR_PAIR(1));
werase(win);
refresh();
wrefresh(win);
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(i%3==0) wattron(win,A_BOLD);
if(j%3==0) wattron(win,A_BOLD);
wprintw(win,"+");
if(j%3==0 && i%3!=0) wattroff(win,A_BOLD);
if(j<9) wprintw(win,"---");
if(i%3==0) wattroff(win,A_BOLD);
}
for(j=0;j<10 && i<9;j++)
{
if(j%3==0) wattron(win,A_BOLD);
if(j>0) wprintw(win," |");
else wprintw(win,"|");
if(j%3==0) wattroff(win,A_BOLD);
}
}
for(i=0;i<9;i++)
for(j=0;j<9;j++) if(board[i][j]!=0) mvwprintw(win,i*2+1,j*4+2,"%d",board[i][j]);
helpboxon();
wmove(win,1,2);
wrefresh(win);
while(ch!='q' && ch!=900)
{
ch=getch();
if(ch=='h')
{
if(hb==1)
{
helpboxoff();
hb=0;
}
else
{
helpboxon();
hb=1;
}
}
else if(ch=='r') restart();
else if(ch==KEY_RIGHT && posx<8) posx++;
else if(ch==KEY_LEFT && posx>0) posx--;
else if(ch==KEY_DOWN && posy<8) posy++;
else if(ch==KEY_UP && posy>0) posy--;
else if(ch==330)
{
if(start[posy][posx]==0)
{
board[posy][posx]=0;
wprintw(win," ");
}
}
else if(ch-48>0 && ch-48<10)
{
if(start[posy][posx]==0)
{
board[posy][posx]=ch-48;
wprintw(win,"%d",ch-48);
}
}
wmove(win,posy*2+1,posx*4+2);
wrefresh(win);
if(check()==1 && count()==81) ch=900;
}
endwin();
if(ch==900) printf("Congratulations, you won the game!");
stdprint();
}
else printf("Usage: nsudoku solved\n- solved is the number of presolved cells.\nFor more information visit www.sh1fty.com/nsudoku\n");
return 0;
}
int helpboxon(void)
{
wprintw(helpbox,"HelpBox\nq - quit\nr - restart\nh - HelpBox");
wrefresh(helpbox);
return 0;
}
int helpboxoff(void)
{
werase(helpbox);
wrefresh(helpbox);
return 0;
}
int change(int x,int y,int num)
{
board[x][y]=num;
mvwprintw(win,y*2+1,x*3+2,"%d",num);
wrefresh(win);
return 0;
}
int check(void)
{
int i,j,k,l,x,y;
int c1[10],c2[10];
for(i=0;i<9;i++)
{
for(k=1;k<10;k++)
{
c1[k]=0;
c2[k]=0;
}
for(j=0;j<9;j++)
{
if(board[i][j]!=0) c1[board[i][j]]++;
if(board[j][i]!=0) c2[board[j][i]]++;
}
for(j=1;j<10;j++) if(c1[j]>1 || c2[j]>1) return 0;
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
for(x=1;x<10;x++)
{
c1[x]=0;
c2[x]=0;
}
for(k=0;k<3;k++)
{
for(l=0;l<3;l++)
{
x=(i*3)+k;
y=(j*3)+l;
if(board[x][y]!=0) c1[board[x][y]]++;
if(board[y][x]!=0) c2[board[y][x]]++;
}
}
for(x=1;x<10;x++) if(c1[x]>1 || c2[x]>1) return 0;
}
}
return 1;
}
int clear_grid(int i, int j)
{
int k,l,x,y;
for(k=0;k<3;k++)
for(l=0;l<3;l++)
{
x=(i*3)+k;
y=(j*3)+l;
board[x][y]=0;
}
return 0;
}
int solve_grid(int i, int j)
{
int k,l,x,y,z;
for(k=0;k<3;k++)
for(l=0;l<3;l++)
{
x=(i*3)+k;
y=(j*3)+l;
for(z=1;z<10;z++)
{
board[x][y]=z;
if(check()==1) z=10;
}
}
return 0;
}
int count(void)
{
int i,j,ret=0;
for(i=0;i<9;i++) for(j=0;j<9;j++) if(board[i][j]>0) ret++;
return ret;
}
int stdprint(void)
{
int i,j;
for(i=0;i<9;i++)
{
printf("\n+---+---+---+---+---+---+---+---+---+\n|");
for(j=0;j<9;j++)
{
if(board[i][j]!=0) printf(" %d |",board[i][j]);
else printf(" |");
}
}
printf("\n+---+---+---+---+---+---+---+---+---+\n");
return 0;
}
int generate(int n)
{
int i,j,k,l,r1,r2,x,y,z,num;
for(i=0;i<9;i++) for(j=0;j<9;j++) start[i][j]=0;
while(check()==0 || count()<80)
{
for(i=0;i<3;i++) for(j=0;j<3;j++) clear_grid(i,j);
for(r1=0,i=0;i<3 && r1<50;i++,r1++)
{
for(r2=0,j=0;j<3 && r2<100;j++,r2++)
{
for(k=0;k<3;k++)
{
for(l=0;l<3;l++)
{
x=(i*3)+k;
y=(j*3)+l;
num=(((int)rand())%9)+1;
for(z=0;z<9;z++,num++)
{
if(num==10) num=1;
board[x][y]=num;
if(check()==1) z=10;
}
}
}
if(check()==0)
{
if(i==2 && j==2) solve_grid(2,2);
else
{
clear_grid(i,j);
j--;
}
}
}
if(check()==0)
{
for(j=0;j<3;j++) clear_grid(i,j);
i--;
}
}
}
for(i=0;i<n;i++)
{
z=0;
while(z==0)
{
x=((int)rand())%9;
y=((int)rand())%9;
if(start[x][y]==0)
{
start[x][y]=board[x][y];
z=1;
}
}
}
for(i=0;i<9;i++) for(j=0;j<9;j++) board[i][j]=start[i][j];
return 0;
}
int restart(void)
{
int i,j;
for(i=0;i<9;i++)
for(j=0;j<9;j++)
{
board[i][j]=start[i][j];
if(board[i][j]!=0) mvwprintw(win,i*2+1,j*4+2,"%d",board[i][j]);
else mvwprintw(win,i*2+1,j*4+2," ",board[i][j]);
}
wrefresh(win);
return 0;
}