Class BadgerWorld


  • public class BadgerWorld
    extends java.lang.Object
    The BadgerWorld class represents the environment where BadgerBots operate. It manages the properties of the world, the grid of cells, and the collection of bots. This class implements the singleton pattern to ensure only one instance of the world exists. It provides methods to load and save world properties, manage bots, and interact with the GUI.

    This class includes:

    • Loading and saving properties for the world configuration.
    • Managing the grid of cells and bots in the world.
    • Providing a graphical interface for visualizing the world and bots.
    Author:
    Jim Williams
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addBall​(java.lang.String name, java.lang.String color)
      Adds a ball item to the world with the specified name and color.
      void addCharacters​(java.lang.String name, java.lang.String color, java.lang.String characters)
      Adds a character item to the world with the specified name, color, and characters.
      static void configureWorld​(java.lang.String badgerWorldPropertiesFilename)
      Configures the BadgerWorld instance with the specified properties file.
      java.awt.Point convertWorldPointToCell​(java.awt.Point point)
      Converts a Point representing screen coordinates to a cell in the world grid.
      Cell getCell​(int x, int y)
      Retrieves the cell at the specified coordinates, wrapping the coordinates if necessary.
      java.lang.String getCellColor​(int x, int y)
      Retrieves the color of the cell at the specified coordinates as a string.
      java.lang.String getCellColorFixedWidth​(int x, int y)
      Retrieves the color of the cell at the specified coordinates as a fixed-width string.
      int getHeight()
      Gets the height of the world.
      static BadgerWorld getInstance()
      Gets the singleton instance of the BadgerWorld.
      int getWidth()
      Gets the width of the world.
      void pause​(int milliseconds)
      Pauses the current thread for the specified number of milliseconds.
      static void saveDefaultProperties()
      Saves the default properties to the properties file.
      void setCellColor​(int x, int y, java.lang.String color)
      Sets the color of the cell at the specified coordinates to the given color.
      java.lang.String toString()
      Returns a string representation of the BadgerWorld instance.
      java.lang.String toStringCellColors()
      Returns a string representation of the cell colors in the world.
      java.lang.String toStringFields()
      Returns a string representation of the main fields of the world.
      java.lang.String toStringProperties()
      Returns a string representation of the world's properties.
      int wrapX​(int x)
      Wraps the x-coordinate within the bounds of the world's width.
      int wrapY​(int y)
      Wraps the y-coordinate within the bounds of the world's height.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Method Detail

      • saveDefaultProperties

        public static void saveDefaultProperties()
        Saves the default properties to the properties file. This method is used to create or overwrite the properties file with the default settings.
      • getCellColor

        public java.lang.String getCellColor​(int x,
                                             int y)
        Retrieves the color of the cell at the specified coordinates as a string.
        Parameters:
        x - the x-coordinate of the cell
        y - the y-coordinate of the cell
        Returns:
        a string representing the color of the cell
      • getCellColorFixedWidth

        public java.lang.String getCellColorFixedWidth​(int x,
                                                       int y)
        Retrieves the color of the cell at the specified coordinates as a fixed-width string.
        Parameters:
        x - the x-coordinate of the cell
        y - the y-coordinate of the cell
        Returns:
        a string representing the color of the cell
      • setCellColor

        public void setCellColor​(int x,
                                 int y,
                                 java.lang.String color)
        Sets the color of the cell at the specified coordinates to the given color.
        Parameters:
        x - the x-coordinate of the cell
        y - the y-coordinate of the cell
        color - the color to set the cell to, represented as a string
      • wrapX

        public int wrapX​(int x)
        Wraps the x-coordinate within the bounds of the world's width.
        Parameters:
        x - the x-coordinate to wrap
        Returns:
        the wrapped x-coordinate
      • wrapY

        public int wrapY​(int y)
        Wraps the y-coordinate within the bounds of the world's height.
        Parameters:
        y - the y-coordinate to wrap
        Returns:
        the wrapped y-coordinate
      • getCell

        public Cell getCell​(int x,
                            int y)
        Retrieves the cell at the specified coordinates, wrapping the coordinates if necessary.
        Parameters:
        x - the x-coordinate of the cell
        y - the y-coordinate of the cell
        Returns:
        the cell at the wrapped coordinates
      • getWidth

        public int getWidth()
        Gets the width of the world.
        Returns:
        the width of the world in number of cells.
      • getHeight

        public int getHeight()
        Gets the height of the world.
        Returns:
        the height of the world in number of cells.
      • pause

        public void pause​(int milliseconds)
        Pauses the current thread for the specified number of milliseconds. This method uses Thread.sleep to suspend execution and catch any InterruptedException that may occur. The exception is caught and ignored.
        Parameters:
        milliseconds - the number of milliseconds to pause the thread
      • addBall

        public void addBall​(java.lang.String name,
                            java.lang.String color)
        Adds a ball item to the world with the specified name and color.
        Parameters:
        name - The name of the ball item.
        color - The color of the ball item.
      • addCharacters

        public void addCharacters​(java.lang.String name,
                                  java.lang.String color,
                                  java.lang.String characters)
        Adds a character item to the world with the specified name, color, and characters.
        Parameters:
        name - The name of the character item.
        color - The color of the character item.
        characters - The characters to be displayed on the item.
      • convertWorldPointToCell

        public java.awt.Point convertWorldPointToCell​(java.awt.Point point)
        Converts a Point representing screen coordinates to a cell in the world grid. If a GUI is present, it delegates the conversion to the GUI.
        Parameters:
        point - The Point representing the screen coordinates.
        Returns:
        The converted Point representing the cell coordinates in the world grid, or null if there is no GUI (since there shouldn't be any mouse input without a GUI).
      • toStringFields

        public java.lang.String toStringFields()
        Returns a string representation of the main fields of the world. The string includes the width, height, cell size, delay in milliseconds, and GUI presence.
        Returns:
        a string representing the main fields of the world in the format "width:, height:, cellSize:, delayInMS:, gui:"
      • toStringProperties

        public java.lang.String toStringProperties()
        Returns a string representation of the world's properties. The properties are converted to a string using their own toString method.
        Returns:
        a string representing the properties of the world
      • toStringCellColors

        public java.lang.String toStringCellColors()
        Returns a string representation of the cell colors in the world. The string includes the color of each cell in the world grid, formatted as a grid of colors.
        Returns:
        a string representing the cell colors in the world
      • toString

        public java.lang.String toString()
        Returns a string representation of the BadgerWorld instance. The string includes the width, height, cell size, delay in milliseconds, whether the GUI is enabled, and the properties used to initialize the world.
        Overrides:
        toString in class java.lang.Object
        Returns:
        a string representation of the BadgerWorld instance.
      • getInstance

        public static BadgerWorld getInstance()
        Gets the singleton instance of the BadgerWorld.
        Returns:
        the singleton instance of BadgerWorld.
      • configureWorld

        public static void configureWorld​(java.lang.String badgerWorldPropertiesFilename)
        Configures the BadgerWorld instance with the specified properties file. This method initializes the singleton instance of the BadgerWorld using the provided properties filename, setting up the world according to the configurations specified in the file.
        Parameters:
        badgerWorldPropertiesFilename - the filename of the properties file to be used for configuring the BadgerWorld instance.