N Sudoku


SUBMITTED BY: Guest

DATE: Dec. 4, 2013, 11:49 a.m.

FORMAT: C++

SIZE: 8.2 kB

HITS: 709

  1. /*
  2. nsudoku 1.0
  3. gcc -o nsudoku nsudoku.c -lncurses
  4. Copyright (c) 2008 Tin Benjamin Matuka
  5. Permission is hereby granted, free of charge, to any person
  6. obtaining a copy of this software and associated documentation
  7. files (the "Software"), to deal in the Software without
  8. restriction, including without limitation the rights to use,
  9. copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the
  11. Software is furnished to do so, subject to the following
  12. conditions:
  13. The above copyright notice and this permission notice shall be
  14. included in all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  17. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  19. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  20. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. OTHER DEALINGS IN THE SOFTWARE.
  23. To compile nsudoku run
  24. $ gcc -lncurses -o nsudoku nsudoku.c
  25. I have no intention of updating this game, because I made it
  26. just to learn how to work with ncurses. You can use the code
  27. if you want to.
  28. For more information visit nsudoku web site:
  29. http://www.sh1fty.com/nsudoku
  30. I can be contacted at sh1fty[at]sh1fty[dot]com
  31. */
  32. #include<stdio.h>
  33. #include<time.h>
  34. #include<ncurses.h>
  35. int board[9][9], start[9][9];
  36. WINDOW *helpbox, *win;
  37. int main( int argc, char *argv[] )
  38. {
  39. int n;
  40. int check( void ); // check if all numbers in board array match the rules
  41. int clear_grid( int i, int j ); // clear a grid(3x3) in row i and column j
  42. int count( void ); // count how many values are in board array
  43. int generate( int n ); // generate sudoku
  44. int stdprint( void ); // stdout print board array
  45. int change( int x, int y, int num ); // change a value of an array field.
  46. int helpboxon( void ); // turn HelpBox on
  47. int helpboxoff( void ); // turn HelpBox off
  48. srand( time( NULL ) );
  49. if(argc>1)
  50. {
  51. n=atoi(argv[1]);
  52. if(n<0 || n>80) n=40;
  53. int ch,curx=0,cury=0,i,j,hb=1;
  54. int maxx,maxy,startx,starty;
  55. int posx=0,posy=0;
  56. generate(n);
  57. initscr();
  58. getmaxyx(stdscr,maxy,maxx);
  59. startx=(maxx-38)/2;
  60. starty=2;
  61. noecho();
  62. start_color();
  63. init_pair(1,COLOR_CYAN,COLOR_BLACK);
  64. keypad(stdscr, TRUE);
  65. win=newwin(25,37,starty,startx);
  66. helpbox=newwin(4,15,1,1);
  67. wbkgd(win,COLOR_PAIR(1));
  68. werase(win);
  69. refresh();
  70. wrefresh(win);
  71. for(i=0;i<10;i++)
  72. {
  73. for(j=0;j<10;j++)
  74. {
  75. if(i%3==0) wattron(win,A_BOLD);
  76. if(j%3==0) wattron(win,A_BOLD);
  77. wprintw(win,"+");
  78. if(j%3==0 && i%3!=0) wattroff(win,A_BOLD);
  79. if(j<9) wprintw(win,"---");
  80. if(i%3==0) wattroff(win,A_BOLD);
  81. }
  82. for(j=0;j<10 && i<9;j++)
  83. {
  84. if(j%3==0) wattron(win,A_BOLD);
  85. if(j>0) wprintw(win," |");
  86. else wprintw(win,"|");
  87. if(j%3==0) wattroff(win,A_BOLD);
  88. }
  89. }
  90. for(i=0;i<9;i++)
  91. for(j=0;j<9;j++) if(board[i][j]!=0) mvwprintw(win,i*2+1,j*4+2,"%d",board[i][j]);
  92. helpboxon();
  93. wmove(win,1,2);
  94. wrefresh(win);
  95. while(ch!='q' && ch!=900)
  96. {
  97. ch=getch();
  98. if(ch=='h')
  99. {
  100. if(hb==1)
  101. {
  102. helpboxoff();
  103. hb=0;
  104. }
  105. else
  106. {
  107. helpboxon();
  108. hb=1;
  109. }
  110. }
  111. else if(ch=='r') restart();
  112. else if(ch==KEY_RIGHT && posx<8) posx++;
  113. else if(ch==KEY_LEFT && posx>0) posx--;
  114. else if(ch==KEY_DOWN && posy<8) posy++;
  115. else if(ch==KEY_UP && posy>0) posy--;
  116. else if(ch==330)
  117. {
  118. if(start[posy][posx]==0)
  119. {
  120. board[posy][posx]=0;
  121. wprintw(win," ");
  122. }
  123. }
  124. else if(ch-48>0 && ch-48<10)
  125. {
  126. if(start[posy][posx]==0)
  127. {
  128. board[posy][posx]=ch-48;
  129. wprintw(win,"%d",ch-48);
  130. }
  131. }
  132. wmove(win,posy*2+1,posx*4+2);
  133. wrefresh(win);
  134. if(check()==1 && count()==81) ch=900;
  135. }
  136. endwin();
  137. if(ch==900) printf("Congratulations, you won the game!");
  138. stdprint();
  139. }
  140. else printf("Usage: nsudoku solved\n- solved is the number of presolved cells.\nFor more information visit www.sh1fty.com/nsudoku\n");
  141. return 0;
  142. }
  143. int helpboxon(void)
  144. {
  145. wprintw(helpbox,"HelpBox\nq - quit\nr - restart\nh - HelpBox");
  146. wrefresh(helpbox);
  147. return 0;
  148. }
  149. int helpboxoff(void)
  150. {
  151. werase(helpbox);
  152. wrefresh(helpbox);
  153. return 0;
  154. }
  155. int change(int x,int y,int num)
  156. {
  157. board[x][y]=num;
  158. mvwprintw(win,y*2+1,x*3+2,"%d",num);
  159. wrefresh(win);
  160. return 0;
  161. }
  162. int check(void)
  163. {
  164. int i,j,k,l,x,y;
  165. int c1[10],c2[10];
  166. for(i=0;i<9;i++)
  167. {
  168. for(k=1;k<10;k++)
  169. {
  170. c1[k]=0;
  171. c2[k]=0;
  172. }
  173. for(j=0;j<9;j++)
  174. {
  175. if(board[i][j]!=0) c1[board[i][j]]++;
  176. if(board[j][i]!=0) c2[board[j][i]]++;
  177. }
  178. for(j=1;j<10;j++) if(c1[j]>1 || c2[j]>1) return 0;
  179. }
  180. for(i=0;i<3;i++)
  181. {
  182. for(j=0;j<3;j++)
  183. {
  184. for(x=1;x<10;x++)
  185. {
  186. c1[x]=0;
  187. c2[x]=0;
  188. }
  189. for(k=0;k<3;k++)
  190. {
  191. for(l=0;l<3;l++)
  192. {
  193. x=(i*3)+k;
  194. y=(j*3)+l;
  195. if(board[x][y]!=0) c1[board[x][y]]++;
  196. if(board[y][x]!=0) c2[board[y][x]]++;
  197. }
  198. }
  199. for(x=1;x<10;x++) if(c1[x]>1 || c2[x]>1) return 0;
  200. }
  201. }
  202. return 1;
  203. }
  204. int clear_grid(int i, int j)
  205. {
  206. int k,l,x,y;
  207. for(k=0;k<3;k++)
  208. for(l=0;l<3;l++)
  209. {
  210. x=(i*3)+k;
  211. y=(j*3)+l;
  212. board[x][y]=0;
  213. }
  214. return 0;
  215. }
  216. int solve_grid(int i, int j)
  217. {
  218. int k,l,x,y,z;
  219. for(k=0;k<3;k++)
  220. for(l=0;l<3;l++)
  221. {
  222. x=(i*3)+k;
  223. y=(j*3)+l;
  224. for(z=1;z<10;z++)
  225. {
  226. board[x][y]=z;
  227. if(check()==1) z=10;
  228. }
  229. }
  230. return 0;
  231. }
  232. int count(void)
  233. {
  234. int i,j,ret=0;
  235. for(i=0;i<9;i++) for(j=0;j<9;j++) if(board[i][j]>0) ret++;
  236. return ret;
  237. }
  238. int stdprint(void)
  239. {
  240. int i,j;
  241. for(i=0;i<9;i++)
  242. {
  243. printf("\n+---+---+---+---+---+---+---+---+---+\n|");
  244. for(j=0;j<9;j++)
  245. {
  246. if(board[i][j]!=0) printf(" %d |",board[i][j]);
  247. else printf(" |");
  248. }
  249. }
  250. printf("\n+---+---+---+---+---+---+---+---+---+\n");
  251. return 0;
  252. }
  253. int generate(int n)
  254. {
  255. int i,j,k,l,r1,r2,x,y,z,num;
  256. for(i=0;i<9;i++) for(j=0;j<9;j++) start[i][j]=0;
  257. while(check()==0 || count()<80)
  258. {
  259. for(i=0;i<3;i++) for(j=0;j<3;j++) clear_grid(i,j);
  260. for(r1=0,i=0;i<3 && r1<50;i++,r1++)
  261. {
  262. for(r2=0,j=0;j<3 && r2<100;j++,r2++)
  263. {
  264. for(k=0;k<3;k++)
  265. {
  266. for(l=0;l<3;l++)
  267. {
  268. x=(i*3)+k;
  269. y=(j*3)+l;
  270. num=(((int)rand())%9)+1;
  271. for(z=0;z<9;z++,num++)
  272. {
  273. if(num==10) num=1;
  274. board[x][y]=num;
  275. if(check()==1) z=10;
  276. }
  277. }
  278. }
  279. if(check()==0)
  280. {
  281. if(i==2 && j==2) solve_grid(2,2);
  282. else
  283. {
  284. clear_grid(i,j);
  285. j--;
  286. }
  287. }
  288. }
  289. if(check()==0)
  290. {
  291. for(j=0;j<3;j++) clear_grid(i,j);
  292. i--;
  293. }
  294. }
  295. }
  296. for(i=0;i<n;i++)
  297. {
  298. z=0;
  299. while(z==0)
  300. {
  301. x=((int)rand())%9;
  302. y=((int)rand())%9;
  303. if(start[x][y]==0)
  304. {
  305. start[x][y]=board[x][y];
  306. z=1;
  307. }
  308. }
  309. }
  310. for(i=0;i<9;i++) for(j=0;j<9;j++) board[i][j]=start[i][j];
  311. return 0;
  312. }
  313. int restart(void)
  314. {
  315. int i,j;
  316. for(i=0;i<9;i++)
  317. for(j=0;j<9;j++)
  318. {
  319. board[i][j]=start[i][j];
  320. if(board[i][j]!=0) mvwprintw(win,i*2+1,j*4+2,"%d",board[i][j]);
  321. else mvwprintw(win,i*2+1,j*4+2," ",board[i][j]);
  322. }
  323. wrefresh(win);
  324. return 0;
  325. }

comments powered by Disqus