Advertisement
A0D1MQ

moveIsPossible

Jan 22nd, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.96 KB | None | 0 0
  1. int moveIsPossible(int Row, int Col, int Destx, int Desty)
  2. {
  3.     int dx, dy, dir, i, x, y;
  4.  
  5.     if (Destx>Col) dx=1;        //
  6.     else if(Destx<Col) dx=-1;   //
  7.     else dx=0;                  //          Check where we should make the first move to achieve destination
  8.     if (Desty>Row) dy=1;        //
  9.     else if(Desty<Row) dy=-1;   //
  10.     else dy=0;                  //
  11.  
  12.     if (Row%2==0) ///even                  
  13.     {
  14.         if (dx==-1 && dy==0) {dir = 1;}
  15.         if (dx==1 && dy==0) {dir = 2;}
  16.         if (dx==0 && dy==-1) {dir = 3;}     //Check which direction takes the first move (for even rows)
  17.         if (dx==0 && dy==1) {dir = 4;}
  18.         if (dx==1 && dy==-1) {dir = 5;}
  19.         if (dx==1 && dy==1) {dir = 6;}
  20.     }
  21.     else          ///odd
  22.     {
  23.         if (dx==-1 && dy==0) {dir = 1;}
  24.         if (dx==1 && dy==0) {dir = 2;}
  25.         if (dx==-1 && dy==-1) {dir = 3;}    //Check which direction takes the first move (for odd rows)
  26.         if (dx==-1 && dy==1) {dir = 4;}
  27.         if (dx==0 && dy==-1) {dir = 5;}
  28.         if (dx==0 && dy==1) {dir = 6;}
  29.     }
  30.  
  31.     x=Col;
  32.     y=Row;
  33.     for(i=0; i<40; i++)                     //checking every move between start position and destination by moving in one direction
  34.     {                                       //(if we can achieve destination by moving in one direction these two positions are in a straight line)
  35.         if(y % 2 == 1)
  36.             {
  37.             switch(dir)
  38.             {
  39.                 case 1:
  40.                     x--;
  41.                     break;
  42.                 case 2:
  43.                     x++;
  44.                     break;
  45.                 case 3:
  46.                     x--;
  47.                     y--;
  48.                     break;
  49.                 case 4:
  50.                     x--;
  51.                     y++;
  52.                     break;
  53.                 case 5:
  54.                     y--;
  55.                     break;
  56.                 case 6:
  57.                     y++;
  58.                     break;
  59.             }
  60.         } else {
  61.             switch(dir)
  62.             {
  63.                 case 1:
  64.                     x--;
  65.                     break;
  66.                 case 2:
  67.                     x++;
  68.                     break;
  69.                 case 3:
  70.                     y--;
  71.                     break;
  72.                 case 4:
  73.                     y++;
  74.                     break;
  75.                 case 5:
  76.                     x++;
  77.                     y--;
  78.                     break;
  79.                 case 6:
  80.                     x++;
  81.                     y++;
  82.                     break;
  83.             }
  84.  
  85.         }
  86.         if (x==Destx && y==Desty && (brdTab[y+1][x] >=1 && brdTab[y+1][x] <=3))
  87.             return 1;                      //if computed x and y is equal to destination x and y
  88.                                            //AND destination tile exists and there is no penguin - can move to this position
  89.     }
  90.     return 0;                              // if ^ not true - cannot move
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement