News:

GinGly.com - Used by 85,000 Members - SMS Backed up 7,35,000 - Contacts Stored  28,850 !!

Main Menu

Number Puzzle !! Intersting one !!

Started by krishnan, Jul 25, 2008, 02:01 PM

Previous topic - Next topic

krishnan

Hi [you],

Number Puzzle !!

Number Puzzle enter in between 1 to 27. If you want above 27 then change the integer value in the program.

Sample Code

   1.
       //Number Puzzle
   2.
      #include<iostream.h>
   3.
      class NumPuzzle
   4.
      {
   5.
              public:
   6.
              int x,i,j,m,count;
   7.
              int puz[25][25];
   8.
              void numpuzz();
   9.
              void putData();
  10.
         NumPuzzle();
  11.
       
  12.
      };
  13.
      NumPuzzle::NumPuzzle()
  14.
              {
  15.
                       for(i=0;i<25;i++)
  16.
                              for(j=0;j<25;j++)
  17.
                                      puz[j]=0;
  18.
                      cout<<"Enter the Odd number between 1 to 27:";
  19.
                      a:
  20.
                      cin>>x;
  21.
                      if(x%2==0)
  22.
                      {
  23.
                        cout<<endl<<"Enter the Odd number:";
  24.
                        goto a;
  25.
                      }
  26.
              }
  27.
      void NumPuzzle::putData()
  28.
      {
  29.
              for(i=0;i<x;i++)
  30.
              {
  31.
                      for(j=0;j<x;j++)
  32.
                      {
  33.
                              cout<<puz[j];
  34.
                              cout<<"    "<<"    ";
  35.
                      }
  36.
                      cout<<endl<<endl;
  37.
              }
  38.
      }
  39.
      void NumPuzzle::numpuzz()
  40.
      {
  41.
              m=x/2;
  42.
              count=1;
  43.
              while(puz[x-1][x/2]==0)
  44.
              {
  45.
                      if(puz[0][x/2]==0)
  46.
                      {
  47.
                              puz[0][x/2]=count;
  48.
                              count++;
  49.
                              j=0;
  50.
                      }
  51.
                      else if(j==0&&m<x)
  52.
                      {
  53.
                              m++;
  54.
                              j=x-1;
  55.
                              if(m==x)
  56.
                              {
  57.
                                      m=x-1;
  58.
                                      j=1;
  59.
                              }
  60.
                              puz[j][m]=count;
  61.
                              count++;
  62.
                              if((m==(x-1))&&(j==x/2-1)&&puz[x-1][x/2-1]!=0)
  63.
                              {
  64.
                                      m=m+1;
  65.
                                      j=x-1;
  66.
                                      puz[j][m]=count;
  67.
                                      count++;
  68.
                              }
  69.
                      }
  70.
                      else if(m==x-1&&j>0)
  71.
                      {
  72.
                              m=0;
  73.
                              j=j-1;
  74.
                              puz[j][m]=count;
  75.
                              count++;
  76.
                      }
  77.
                      else if(j<=x-1&&m>=0&&j!=0)
  78.
                      {
  79.
                              if(puz[j-1][m+1]==0&&m!=x)
  80.
                              {
  81.
                                      m=m+1;
  82.
                                      j=j-1;
  83.
                                      puz[j][m]=count;
  84.
                                      count++;
  85.
                              }
  86.
       
  87.
                              else if(puz[j-1][m+1]!=0||m==x)
  88.
                              {
  89.
                                      j=j+1;
  90.
                                      puz[j][m]=count;
  91.
                                      count++;
  92.
                              }
  93.
                      }
  94.
              }
  95.
      }
  96.
      void main()
  97.
      {
  98.
              char ch='y';
  99.
              do
100.
              {
101.
                      NumPuzzle venki;
102.
                      venki.numpuzz();
103.
                      venki.putData();
104.
                      cout<<endl<<"If you want ot continue press 'y':"<<endl;
105.
                      cin>>ch;
106.
              }while(ch=='y');
107.
              cout<<endl<<" thank you ";
108.
      }

akhiloesh

#1
U :police :yes