Example #1
1
 private static boolean[] search(Position playerPos, Box box, Map map) {
   char[][] board = map.getMap();
   // Directions[0-4] = west, east, north, south!
   boolean[] directions = new boolean[4];
   for (int i = 0; i < 4; i++) {
     directions[i] = false;
   }
   Position boxPos = box.getPosition();
   Position west = new Position(boxPos.getRow(), boxPos.getCol() - 1);
   Position east = new Position(boxPos.getRow(), boxPos.getCol() + 1);
   Position north = new Position(boxPos.getRow() - 1, boxPos.getCol());
   Position south = new Position(boxPos.getRow() + 1, boxPos.getCol());
   boolean[][] visited = new boolean[map.getRows()][map.getCols()];
   // Initialize visited to false
   for (int row = 0; row < map.getRows(); row++) {
     for (int col = 0; col < map.getCols(); col++) {
       visited[row][col] = false;
     }
   }
   Queue<Position> q = new LinkedList<Position>();
   q.add(playerPos);
   visited[playerPos.getRow()][playerPos.getCol()] = true;
   while (!q.isEmpty()) {
     Position currPos = q.remove();
     // may reach left position of the box
     if (currPos.isEqualTo(west)) {
       directions[0] = true;
     }
     // may reach right position of the box
     if (currPos.isEqualTo(east)) {
       directions[1] = true;
     }
     // may reach peter north position of the box
     if (currPos.isEqualTo(north)) {
       directions[2] = true;
     }
     // may reach south position of the box
     if (currPos.isEqualTo(south)) {
       directions[3] = true;
     }
     for (int direction = 0; direction < 4; direction++) {
       if (direction == WEST) {
         Position newPos = new Position(currPos.getRow(), currPos.getCol() - 1);
         if (!visited[newPos.getRow()][newPos.getCol()] && isAvailiable(board, newPos)) {
           visited[newPos.getRow()][newPos.getCol()] = true;
           q.add(newPos);
         }
       } else if (direction == EAST) {
         Position newPos = new Position(currPos.getRow(), currPos.getCol() + 1);
         if (!visited[newPos.getRow()][newPos.getCol()] && isAvailiable(board, newPos)) {
           visited[newPos.getRow()][newPos.getCol()] = true;
           q.add(newPos);
         }
       } else if (direction == NORTH) {
         Position newPos = new Position(currPos.getRow() - 1, currPos.getCol());
         if (!visited[newPos.getRow()][newPos.getCol()] && isAvailiable(board, newPos)) {
           visited[newPos.getRow()][newPos.getCol()] = true;
           q.add(newPos);
         }
       } else if (direction == SOUTH) {
         Position newPos = new Position(currPos.getRow() + 1, currPos.getCol());
         if (!visited[newPos.getRow()][newPos.getCol()] && isAvailiable(board, newPos)) {
           visited[currPos.getRow()][currPos.getCol()] = true;
           q.add(newPos);
         }
       }
     }
   }
   return directions;
 }
Example #2
0
 private boolean canMoveBoxAt(PositionSwitcher movement, Box box) {
   if (box != null) {
     Position newBoxPos = movement.change(box.getPosition());
     return isEmpty(newBoxPos);
   }
   return false;
 }
Example #3
0
 /** Find all possible directions from where the player is standing, to boxes that can be pushed */
 public static ArrayList<Move> findPossibleMoves(Map map) {
   ArrayList<Move> possibleMoves = new ArrayList<Move>();
   Position playerPos = map.getPlayerPosition();
   ArrayList<Box> boxes = map.getAllBoxes();
   boolean[] directions = new boolean[4];
   for (int boxNr = 0; boxNr < boxes.size(); boxNr++) {
     Box currBox = boxes.get(boxNr);
     directions = search(playerPos, currBox, map);
     for (int i = 0; i < 4; i++) {
       if (directions[i]) {
         if (i == WEST) { // Push the box east
           Position newBoxPos =
               new Position(currBox.getPosition().getRow(), currBox.getPosition().getCol() + 1);
           if (legalPush(newBoxPos, map)) {
             Move move = new Move(boxNr, newBoxPos);
             possibleMoves.add(move);
           } else {
             continue;
           }
         } else if (i == EAST) { // Push the box west
           Position newBoxPos =
               new Position(currBox.getPosition().getRow(), currBox.getPosition().getCol() - 1);
           if (legalPush(newBoxPos, map)) {
             Move move = new Move(boxNr, newBoxPos);
             possibleMoves.add(move);
           }
         } else if (i == NORTH) { // Push the box south
           Position newBoxPos =
               new Position(currBox.getPosition().getRow() + 1, currBox.getPosition().getCol());
           if (legalPush(newBoxPos, map)) {
             Move move = new Move(boxNr, newBoxPos);
             possibleMoves.add(move);
           }
         } else if (i == SOUTH) { // Push the box north
           Position newBoxPos =
               new Position(currBox.getPosition().getRow() - 1, currBox.getPosition().getCol());
           if (legalPush(newBoxPos, map)) {
             Move move = new Move(boxNr, newBoxPos);
             possibleMoves.add(move);
           }
         }
       }
     }
   }
   return possibleMoves;
 }
Example #4
0
 private void insert(Box n, ArrayList<Box> a, int i) {
   for (;
       i > 0 && ((Comparable<Integer>) a.get(i - 1).getPosition()).compareTo(n.getPosition()) > 0;
       i--) a.set(i, a.get(i - 1));
   a.set(i, n);
 }