Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.43 KB | None | 0 0
  1. public static function move($coords, Ship $myShip, $mines){
  2.         e('move');
  3.         // from a position to get to
  4.         // see what's best to do
  5.  
  6.         //set orientation
  7.         $myPos = $myShip->coords;
  8.         $dirHorizontal = 'NONE'; // left, right or none
  9.         if($coords[0] > $myPos[0])
  10.             $dirHorizontal = 'RIGHT';
  11.         else if($coords[0] < $myPos[0])
  12.             $dirHorizontal = 'LEFT';
  13.  
  14.         $dirVertical = 'NONE'; // top, bottom or none
  15.         if($coords[1] > $myPos[1])
  16.             $dirVertical = 'BOTTOM';
  17.         else if($coords[1] < $myPos[1])
  18.             $dirVertical = 'TOP';
  19.  
  20.         $dir = "$dirHorizontal $dirVertical";
  21.         $orientation = [0];
  22.  
  23.         if($dir == "NONE BOTTOM")
  24.             $orientation = [4, 5];
  25.         elseif($dir == "NONE TOP")
  26.             $orientation = [1, 2];
  27.         elseif($dir == "LEFT BOTTOM")
  28.             $orientation = [4];
  29.         elseif($dir == "LEFT TOP")
  30.             $orientation = [2];
  31.         elseif($dir == "LEFT NONE")
  32.             $orientation = [3];
  33.         elseif($dir == "RIGHT BOTTOM")
  34.             $orientation = [5];
  35.         elseif($dir == "RIGHT TOP")
  36.             $orientation = [1];
  37.         elseif($dir == "RIGHT NONE")
  38.             $orientation = [0];
  39.  
  40.         $mines = array_map(function($mine){
  41.             return $mine->coords;
  42.         }, $mines);            
  43.         $leftTurn = [1, 2, 3, 4, 5, 0];
  44.         if(in_array($myShip->orientation, $orientation)){ //if our orientation is correct
  45.             //check next case
  46.             $nextCase = Utils::FuturePos($myShip->coords, $myShip->orientation, false,  1);
  47.             if(!in_array($nextCase, $mines)){ //no mine where we go
  48.                 if($myShip->speed === 0)
  49.                     self::faster();
  50.                 else
  51.                     self::wait();
  52.             }
  53.             else{
  54.                 self::rotate();
  55.             }
  56.         }
  57.         else{ // set our orientation right
  58.             ('set orientation');
  59.             //leftway
  60.             $left = 1;
  61.             $currentOrientation = $myShip->orientation;
  62.             while($leftTurn[$currentOrientation] != $orientation){
  63.                 $left++;
  64.                 if($left > 3) break;
  65.                 $currentOrientation = ($currentOrientation+1) % 6;
  66.             }
  67.  
  68.             if($left <= 3)
  69.                 self::rotate('LEFT');
  70.             else
  71.                 self::rotate();
  72.         }
  73.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement