Package api

Class StringUtil


  • public class StringUtil
    extends java.lang.Object
    Utilities for Strings to represent grid and cell information in the Pearls game.
    Author:
    smkautz
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static char NULL_CHAR
      Character to represent a null state in string conversions.
      static char PLAYER_CHAR
      Character for representing the player position in the grid.
      static char PLAYER_IN_GATE_CHAR
      Character for representing the player position in the grid, when the player lands on a gate.
      static char PLAYER_IN_PORTAL_CHAR
      Character for representing the player position in the grid, when the player lands on a portal
      static char PLAYER_IN_SPIKES_CHAR
      Character for representing the player position in the grid, when the player lands on spikes.
      static char[] TEXT
      Basic character representations of cell states, corresponding to State values.
    • Constructor Summary

      Constructors 
      Constructor Description
      StringUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static Cell[] createCellsFromString​(java.lang.String text)
      Creates a cell array from a string description, using the character representations defined in this class.
      static State[] createFromString​(java.lang.String text)
      Creates a cell state array from a string description, using the character representations defined in this class.
      static Cell[][] createFromStringArray​(java.lang.String[] strings)
      Initializes a cell grid from an array of strings.
      static char getChar​(State s)
      Returns the basic character for a state.
      static State getValue​(char c)
      Returns the State corresponding to the given character, if possible.
      static void printCellArray​(Cell[] cells)
      Prints an array of Cells using the character representations defined in this class.
      static void printGrid​(Pearls g)
      Prints out a game's grid.
      static void printGrid​(java.lang.String[] strings)
      Prints a string array as a 2D grid.
      static void printStateArray​(State[] states, int playerIndex)
      Prints an array of States using the character representations defined in this class.
      static void printString​(java.lang.String s)
      Prints a string with an extra space between characters.
      static java.lang.String toString​(Cell[] arr)
      Converts a cell array to a string representation, using the character representations defined in this class.
      static java.lang.String toString​(State[] arr)
      Converts a state array to a string representation, using the character representations defined in this class, with an extra space inserted between characters.
      static java.lang.String toString​(State[] arr, boolean addSpaces)
      Converts a state array to a string representation, using the character representations defined in this class, where an extra space is optionally inserted between characters.
      static java.lang.String toString​(State[] arr, int playerIndex)
      Converts a cell state array to a string representation, using the character representations defined in this class, including the player position.
      static java.lang.String[] toStringArray​(Cell[][] grid)
      Converts a grid to a string array representation using the characters defined in this class.
      static java.lang.String[] toStringArray​(Pearls game)
      Converts the given game's grid to a string array representation using the characters defined in this class.
      • Methods inherited from class java.lang.Object

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

      • TEXT

        public static final char[] TEXT
        Basic character representations of cell states, corresponding to State values.
      • PLAYER_CHAR

        public static final char PLAYER_CHAR
        Character for representing the player position in the grid.
        See Also:
        Constant Field Values
      • PLAYER_IN_PORTAL_CHAR

        public static final char PLAYER_IN_PORTAL_CHAR
        Character for representing the player position in the grid, when the player lands on a portal
        See Also:
        Constant Field Values
      • PLAYER_IN_SPIKES_CHAR

        public static final char PLAYER_IN_SPIKES_CHAR
        Character for representing the player position in the grid, when the player lands on spikes.
        See Also:
        Constant Field Values
      • PLAYER_IN_GATE_CHAR

        public static final char PLAYER_IN_GATE_CHAR
        Character for representing the player position in the grid, when the player lands on a gate.
        See Also:
        Constant Field Values
      • NULL_CHAR

        public static final char NULL_CHAR
        Character to represent a null state in string conversions.
        See Also:
        Constant Field Values
    • Constructor Detail

      • StringUtil

        public StringUtil()
    • Method Detail

      • getChar

        public static char getChar​(State s)
        Returns the basic character for a state. Returns NULL_CHAR if the given state is null.
        Parameters:
        s - a State
        Returns:
        character for the given state
      • getValue

        public static State getValue​(char c)
        Returns the State corresponding to the given character, if possible. Any capital letter in the range 'A' - 'Z' is treated as a portal.
        Parameters:
        c - given character
        Returns:
        State corresponding to the given character, of null if the character does not correspond to a State
      • toString

        public static java.lang.String toString​(State[] arr)
        Converts a state array to a string representation, using the character representations defined in this class, with an extra space inserted between characters. A null state will be represented by the character NULL_CHAR.
        Parameters:
        arr - array of State
        Returns:
        string representation of the State array
      • toString

        public static java.lang.String toString​(State[] arr,
                                                boolean addSpaces)
        Converts a state array to a string representation, using the character representations defined in this class, where an extra space is optionally inserted between characters. A null state will be represented by the character NULL_CHAR.
        Parameters:
        arr - array of State
        addSpaces - if true, method inserts a space between characters
        Returns:
        string representation of the State array
      • createFromStringArray

        public static Cell[][] createFromStringArray​(java.lang.String[] strings)
        Initializes a cell grid from an array of strings. All strings must be the same length and may include the characters from TEXT, the PLAYER_CHAR, and capital letters 'A' - 'Z'. Spaces are also treated as empty cells. Each string represents one row of the grid. Capital letters represent portals, and therefore any capital letters must occur exactly twice in the string array. This method will identify pairs of portals and calculate the correct row and column offsets.
        Parameters:
        strings - array of strings representing an initial game grid
        Returns:
        2D array of cell object constructed from the string array
      • toStringArray

        public static java.lang.String[] toStringArray​(Cell[][] grid)
        Converts a grid to a string array representation using the characters defined in this class. All portals will be represented as the 'O' character. The returned array has one string per row of the grid.
        Parameters:
        grid - given 2D array of cells
        Returns:
        string representation of the given grid
      • toStringArray

        public static java.lang.String[] toStringArray​(Pearls game)
        Converts the given game's grid to a string array representation using the characters defined in this class. All portals will be represented as the 'O' character. The returned array has one string per row of the grid.
        Parameters:
        game - game instance
        Returns:
        string representation of the game's grid
      • createCellsFromString

        public static Cell[] createCellsFromString​(java.lang.String text)
        Creates a cell array from a string description, using the character representations defined in this class. This method does not calculate portal offsets.
        Parameters:
        text - given string
        Returns:
        Cell array constructed from the string
      • createFromString

        public static State[] createFromString​(java.lang.String text)
        Creates a cell state array from a string description, using the character representations defined in this class. The player position is ignored. PLAYER_IN_GATE_CHAR is rendered as an open gate, and PLAYER_IN_SPIKES_CHAR is rendered as SPIKES_ALL character.
        Parameters:
        text - given string
        Returns:
        Cell array constructed from the string
      • toString

        public static java.lang.String toString​(State[] arr,
                                                int playerIndex)
        Converts a cell state array to a string representation, using the character representations defined in this class, including the player position.
        Parameters:
        arr - array of State
        playerIndex - index within the array where a symbol for the player should appear
        Returns:
        string representation of the State array
      • toString

        public static java.lang.String toString​(Cell[] arr)
        Converts a cell array to a string representation, using the character representations defined in this class.
        Parameters:
        arr - array of Cells
        Returns:
        string representation of the Cell array
      • printCellArray

        public static void printCellArray​(Cell[] cells)
        Prints an array of Cells using the character representations defined in this class.
        Parameters:
        cells - array of cells
      • printStateArray

        public static void printStateArray​(State[] states,
                                           int playerIndex)
        Prints an array of States using the character representations defined in this class.
        Parameters:
        states - array of States
        playerIndex - index within the array where a character for the player should appear
      • printString

        public static void printString​(java.lang.String s)
        Prints a string with an extra space between characters.
        Parameters:
        s - any string
      • printGrid

        public static void printGrid​(Pearls g)
        Prints out a game's grid.
        Parameters:
        g - game instance
      • printGrid

        public static void printGrid​(java.lang.String[] strings)
        Prints a string array as a 2D grid.
        Parameters:
        strings - array of strings, one per row of the grid