All pastes #1991645 Raw Edit

rascal999 - Sudoku v2.2

public c v1 · immutable
#1991645 ·published 2010-11-14 23:02 UTC
rendered paste body
#include <stdio.h>#include <stdlib.h>#include <time.h>/* Rascal999 * http://rascal999.co.uk * * Sudoku v2.2 (Nov '10) * * It's been painful at times, but v2.2 is out. My immediate todo is to kill tmp[] in favor of direct * input to mat[][]. I don't have the time to do it now but it's certainly on the list. I'll also add * difficulty setting at some point. */int mkNode();int mkSudoku();int power(int a);void reset();int a, b, c, d, i, tcol[9], tblock[9], col[9], block[9], tmp[9], row, r, rp, bad=0, mReset, mat[9][9], my=0;int main(){   srand(time(NULL));   while(1)   {      for(i=0;i<9;i++)      {         tblock[i]=0;         tcol[i]=0;         block[i]=0;         col[i]=0;         tmp[i]=0;      }      mReset=0;      bad=0;      my=0;      mkSudoku();   }   return 0;}int mkNode(){   r = (rand() % 9) + 1;   rp = power(r);   if (((rp & row)==0) && ((rp & tblock[(a*3)+d])==0) && ((rp & tcol[(c*3)+d])==0))   {      tmp[(d*3)+c] = r;      tblock[(a*3)+d] = tblock[(a*3)+d] | rp;      tcol[(c*3)+d] = tcol[(c*3)+d] | rp;      row = row | rp;      return r;   } else {      if (((tcol[(c*3)+d]) | (row) | (tblock[(a*3)+d]))==511)      {         //40 may not be optimum, experiment         if (bad>40) mReset=1;         bad++;         d = 0;         c = 0;         reset();      }   }   return 0;}int mkSudoku(){   for(a=0;a<3;a++)   {      for(b=0;b<3;b++)      {         for(c=0;c<3;c++)         {            for(d=0;d<3;d++)            {               while(!mkNode()) if (mReset==1) return 0;//               printf("tmp\t%d\n",(d*3)+c); //               printf("3 skip\t%d\n",(d*3)+c);//               printf("block\t%d\n",(a*3)+d);//               printf("row\t%d\n",(a*3)+b);//               printf("col\t%d\n",(c*3)+d);//               printf("block\t%d\n",(a*3)+c);            }         }         //Throw in 2d array         for(i=0;i<9;i++)         {            if (((r = (rand() % 9)) + 1) > 3) mat[my][i]=0;            else mat[my][i]=tmp[i];            block[i] = block[i] | tblock[i];            col[i] = col[i] | tcol[i];         }         //Increment y axis         my++;         //Reset vars         reset();      }   }   //Print results   for(i=0;i<9;i++)   {      for(my=0;my<9;my++)      {         if (mat[my][i]==0) printf(".");         else printf("%d",mat[my][i]);      }   }   printf("\n");   return 0;}int power(int a){   return 1 << (a-1);}void reset(){   for(i=0;i<9;i++)   {      tblock[i] = block[i];      tcol[i] = col[i];      tmp[i] = 0;   }   row = 0;}