Friday, October 1, 2010

array baruun dooshoo zuun deesh ( right bottom left top )



Editor -> Dev-Cpp 5 (gnu c++ 3.4.4)





2d.h

namespace bor
{
const int LENGTH = 10;

const int TOP = 1;
const int RIGHT = 2;
const int BOTTOM = 3;
const int LEFT = 4;

class cube
{
private:
         int arr[LENGTH][LENGTH];
protected:
           int sector;
public:
       void fill();
       void change();  
       void print();
};
}


2d.cpp


#include <iostream>
#include <cstdlib>

#include "2d.h"

using namespace std;

namespace bor
{
void cube::fill(){
     sector = TOP;
     for(int i = 0; i < LENGTH; i++) {
      for(int j = 0; j < LENGTH; j++) {
       arr[i][j] = 1;
      }
     }
     change();
}
void cube::change(){  
     int i = 0, j = 0;
     do {
      if(sector == TOP) {    
       if(j < LENGTH && arr[i][j] != 2)
            { arr[i][j] = 2; j++; }
            else
            { j--; i++; sector = RIGHT; }
            print();
      }
      if(sector == RIGHT) {
       if(i < LENGTH && arr[i][j] != 2)
            { arr[i][j] = 2; i++; }
            else
            { i--; j--; sector = BOTTOM; }
            print();
      }
      if(sector == BOTTOM) {
       if(j > -1 && arr[i][j] != 2)
            { arr[i][j] = 2; j--;
              if(j < 0) { j++; i--; sector = LEFT; }
            }
            else
            { j++; i--; sector = LEFT; }
            print();
      }
      if(sector == LEFT) {    
       if(i > -1 && arr[i][j] != 2)
            { arr[i][j] = 2; i--;
              if(i < 0) { i++; j++; sector = TOP; }
            }
            else
            { i++; j++; sector = TOP; }
            print();
      }
      if(i == 5 && j == 5) break;
     } while (i == -1 || j == -1 || (i > -1 || j > -1 && arr[i][j] != 2) );     
     print();
}
void cube::print(){
     cout<<endl<<endl;
     for(int i = 0; i < LENGTH; i++) {
      for(int j = 0; j < LENGTH; j++) {
              cout<<arr[i][j]<<"  ";
      }
      cout<<endl;
     }
     system("pause");  
}
}


main.cpp

#include <cstdlib>
#include <iostream>

#include "2d.h"

using namespace std;
using namespace bor;

int main(int argc, char* argv[])
{
    cube *c = new cube();
    c->fill();
    return 0;
}

No comments: