Package mini2

Class PearlUtil


  • public class PearlUtil
    extends java.lang.Object
    Utility class containing the key algorithms for moves in the a yet-to-be-determined game.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static void collectPearls​(State[] states, int start, int end)
      Replaces all PEARL states with EMPTY state between indices start and end, inclusive.
      static State[] createFromString​(java.lang.String text)
      Creates a state array from a string description, using the character representations defined by State.getValue.
      static int findRightmostMovableBlock​(State[] states, int start)
      Returns the index of the rightmost movable block that is at or to the left of the given index start.
      static boolean isValidForMoveBlocks​(State[] states)
      Determines whether the given state sequence is valid for the moveBlocks method.
      static void moveBlocks​(State[] states)
      Updates the given state sequence to be consistent with shifting all movable blocks as far to the right as possible, replacing their previous positions with EMPTY.
      static int movePlayer​(State[] states)
      Updates the given state sequence to be consistent with shifting the "player" to the right as far as possible.
      void shiftMovableBlocksAlt​(State[] cells)  
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • collectPearls

        public static void collectPearls​(State[] states,
                                         int start,
                                         int end)
        Replaces all PEARL states with EMPTY state between indices start and end, inclusive.
        Parameters:
        states - any array of States
        start - starting index, inclusive
        end - ending index, inclusive
      • findRightmostMovableBlock

        public static int findRightmostMovableBlock​(State[] states,
                                                    int start)
        Returns the index of the rightmost movable block that is at or to the left of the given index start. Returns -1 if there is no movable block at start or to the left.
        Parameters:
        states - array of State objects
        start - starting index for searching
        Returns:
        index of first movable block encountered when searching towards the left, starting from the given starting index; returns -1 if there is no movable block found
      • createFromString

        public static State[] createFromString​(java.lang.String text)
        Creates a state array from a string description, using the character representations defined by State.getValue. (For invalid characters, the corresponding State will be null, as determined by State.getValue.) Spaces in the given string are ignored; that is, the length of the returned array is the number of non-space characters in the given string.
        Parameters:
        text - given string
        Returns:
        State array constructed from the string
      • isValidForMoveBlocks

        public static boolean isValidForMoveBlocks​(State[] states)
        Determines whether the given state sequence is valid for the moveBlocks method. A state sequence is valid for moveBlocks if
        • its length is at least 2, and
        • the first state is EMPTY, OPEN_GATE, or PORTAL, and
        • it contains exactly one boundary state, which is the last element of the array
        Boundary states are defined by the method State.isBoundary, and are defined differently based on whether there is any movable block in the array.
        Parameters:
        states - any array of States
        Returns:
        true if the array is a valid state sequence, false otherwise
      • movePlayer

        public static int movePlayer​(State[] states)
        Updates the given state sequence to be consistent with shifting the "player" to the right as far as possible. The starting position of the player is always index 0. The state sequence is assumed to be valid for movePlayer, which means that the sequence could have been obtained by applying moveBlocks to a sequence that was valid for moveBlocks. That is, the validity condition is the same as for moveBlocks, except that
        • all movable blocks, if any, are as far to the right as possible, and
        • the last element may be OPEN_GATE or PORTAL, even if there are no movable blocks

        The player's new index, returned by the method, will be one of the following:

        • if the array contains any movable blocks, the new index is just before the first movable block in the array;
        • otherwise, if the very last element of the array is SPIKES_ALL, the new index is the last position in the array;
        • otherwise, the new index is the next-to-last position of the array.
        Note the last state of the array is always treated as a boundary for the player, even if it is OPEN_GATE or PORTAL. All pearls in the sequence are changed to EMPTY and any open gates passed by the player are changed to CLOSED_GATE by this method. (If the player's new index is on an open gate, its state remains OPEN_GATE.)
        Parameters:
        states - a valid state sequence
        Returns:
        the player's new index
      • moveBlocks

        public static void moveBlocks​(State[] states)
        Updates the given state sequence to be consistent with shifting all movable blocks as far to the right as possible, replacing their previous positions with EMPTY. Adjacent movable blocks with opposite parity are "merged" from the right and removed. The given array is assumed to be valid for moveBlocks in the sense of the method validForMoveBlocks. If a movable block moves over a pearl (whether or not the block is subsequently removed due to merging with an adjacent block) then the pearl is also replaced with EMPTY.

        Note that merging is logically done from the right. For example, given a cell sequence represented by ".+-+#", the resulting cell sequence would be "...+#", where indices 2 and 3 as move to index 3 and disappear and position 1 is moved to index 3.

        Parameters:
        states - a valid state sequence
      • shiftMovableBlocksAlt

        public void shiftMovableBlocksAlt​(State[] cells)