home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / TextEditors&Viewers / Texteditors / FFRED10.LHA / fpl / Solitaire.FPL < prev    next >
Encoding:
Text File  |  1994-09-22  |  3.4 KB  |  152 lines

  1.  
  2. char best=0;
  3. int itercount=0;
  4.  
  5.  
  6. char mall[64]={
  7.     '0','0','2','2','2','0','0','0',
  8.     '0','0','2','2','2','0','0','0',
  9.     '2','2','2','2','2','2','2','0',
  10.     '2','2','2','2','2','2','2','0',
  11.     '2','2','2','2','2','2','2','0',
  12.     '0','1','2','2','2','0','0','0',
  13.     '0','0','2','2','2','0','0','0',
  14.     '0','0','0','0','0','0','0','0',  // This line has to be all zeroes.
  15.  
  16. };
  17.  
  18. char buffer[64*64];
  19. char pos[64];
  20. char dir[64];
  21.  
  22. void NextIter(int iternumber, int parenttest, int parentdir)
  23. {
  24.   char callnext=1;
  25.   char nocall=1;
  26.   int lastdir=0;
  27.   int lasttest=0;
  28.  
  29.   itercount++;
  30.  
  31.   PrintPlan(iternumber, 0);
  32.   while (lasttest<56) {
  33.     int bufpek=64*iternumber+lasttest;
  34.     switch (buffer[bufpek]) {
  35.     default:
  36.       lasttest++;
  37.       break;
  38.     case 1:
  39.       while (1) {
  40.         if (callnext) {
  41.           int count;
  42.           for (count=0; count<64; count++)
  43.             buffer[64*(iternumber+1)+count]=buffer[64*iternumber+count];
  44.         }
  45.         callnext=0;
  46.         lastdir++;
  47.         if (lastdir==5) {
  48.           lasttest++;
  49.           lastdir=0;
  50.           break;
  51.         } else {
  52.           int rad, kol;
  53.           switch (lastdir) {
  54.           case 1:
  55.             rad=-8;
  56.             kol=0;
  57.             break;
  58.           case 2:
  59.             rad=8;
  60.             kol=0;
  61.             break;
  62.           case 3:
  63.             rad=0;
  64.             kol=-1;
  65.             break;
  66.           case 4:
  67.             rad=0;
  68.             kol=1;
  69.             break;
  70.           default:
  71.             rad=0;
  72.             kol=0;
  73.             lastdir=0;
  74.             break;
  75.           }
  76.           if (buffer[bufpek+rad+kol]==1 && buffer[bufpek+(rad+kol)*2]==0) {
  77.             callnext=0;
  78.             if (lastdir>parentdir || (lastdir==parentdir && lasttest>parenttest))
  79.               callnext=1;
  80.             else {
  81.               if (buffer[bufpek-64]!=1 ||
  82.                   buffer[bufpek+rad+kol-64]!=1 ||
  83.                   buffer[bufpek+(rad+kol)*2-64]!=0) {
  84.                 callnext=1;
  85.               }
  86.             }
  87.             if (callnext) {
  88.               buffer[bufpek+64+(rad+kol)*2]=1;
  89.               buffer[bufpek+64+0]=0;
  90.               buffer[bufpek+64+rad+kol]=0;
  91.               nocall=0;
  92.               pos[iternumber]=lasttest;
  93.               dir[iternumber]=lastdir;
  94.               NextIter(iternumber+1, lasttest, lastdir);
  95.               PrintPlan(iternumber, 0);
  96.             }
  97.           }
  98.         }
  99.       }
  100.     }
  101.   }
  102.   if (nocall) {
  103.     if (iternumber>best) {
  104.       best=iternumber;
  105.       PrintLine(Sprintf("The best after %ld iterations:", itercount), 13);
  106.       PrintPlan(best, 13);
  107.     }
  108.   }
  109.   pos[iternumber]=0;
  110.   dir[iternumber]=0;
  111. }
  112.  
  113.  
  114. void PrintPlan(int number, int offset)
  115. {
  116.   int count1, count2;
  117.  
  118.   PrintLine(Sprintf("Depth: %ld  ", number-1), 1+offset);
  119.   PrintLine("         ", 2+offset);
  120.   for (count1=0; count1<8; count1++) {
  121.     string tempbuffer=" ";
  122.     int bufpek=number*64+count1*8;
  123.     for (count2=0; count2<8; count2++) {
  124.       switch (buffer[bufpek+count2]) {
  125.       default:
  126.        tempbuffer+=" ";
  127.        break;
  128.       case 0:
  129.        tempbuffer+=".";
  130.        break;
  131.       case 1:
  132.        tempbuffer+="*";
  133.        break;
  134.       }
  135.     }
  136.     PrintLine(tempbuffer, count1+3+offset);
  137.   }
  138. }
  139.  
  140. {
  141.   int count, width=ReadInfo("view_columns");
  142.   for (count=ReadInfo("view_lines"); count>0; count--)
  143.     PrintLine(Sprintf(joinstr("%",ltostr(width), "lc"), ' '), count);
  144.  
  145.   for (count=0; count<64; count++) {
  146.     buffer[64+count]=mall[count]-'1';
  147.     buffer[count]='1';
  148.   }
  149.   NextIter(1, 0, 0);
  150.   Request(Sprintf("Best result was %ld iterations", best-1));
  151. }
  152.