Package mini2
Class PearlUtil
- java.lang.Object
-
- mini2.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 byState.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 indexstart
.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)
-
-
-
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 Statesstart
- starting index, inclusiveend
- 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 indexstart
. Returns -1 if there is no movable block atstart
or to the left.- Parameters:
states
- array of State objectsstart
- 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 byState.getValue
. (For invalid characters, the corresponding State will be null, as determined byState.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
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.
- 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 methodvalidForMoveBlocks
. 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)
-
-