Class BadgerBot

  • All Implemented Interfaces:
    java.awt.event.KeyListener, java.awt.event.MouseListener, java.awt.event.MouseMotionListener, java.util.EventListener

    public class BadgerBot
    extends java.lang.Object
    implements java.awt.event.KeyListener, java.awt.event.MouseListener, java.awt.event.MouseMotionListener
    Inspired by Karel the Dog and Turtle worlds. Created to have visual feedback and also be able to be autograded.

    The BadgerBot class represents a robot in the BadgerWorld simulation. It includes properties and methods to control the robot's movement, direction, and drawing capabilities.

    Version:
    1.0
    Author:
    Jim Williams, Victor Suciu
    • Constructor Summary

      Constructors 
      Constructor Description
      BadgerBot()
      No-argument constructor for the BadgerBot.
      BadgerBot​(int x, int y)
      Constructor for the BadgerBot that initializes it at the specified coordinates.
      BadgerBot​(int x, int y, java.lang.String itemName)
      Constructor for the BadgerBot that initializes it at the specified coordinates with a specified item.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addKeyListener​(java.awt.event.KeyListener listener)
      Adds a KeyListener to the BadgerBot.
      void addMouseListener​(java.awt.event.MouseListener listener)
      Adds a MouseListener to the BadgerBot.
      void addMouseMotionListener​(java.awt.event.MouseMotionListener listener)
      Adds a MouseMotionListener to the BadgerBot.
      java.lang.String facingColor()
      Returns the color of the location the bot is currently facing.
      java.lang.String facingItem()
      Returns the name of the item in the location the bot is currently facing.
      java.lang.String getDirection()
      Gets the current direction of the BadgerBot.
      boolean isFacingEdge()
      Checks if the BadgerBot is facing an edge.
      boolean isFacingWall()
      Checks if the BadgerBot is facing a wall.
      void keyPressed​(java.awt.event.KeyEvent e)
      Handles the keyPressed event and forwards it to the registered KeyListener, if present.
      void keyReleased​(java.awt.event.KeyEvent e)
      Handles the keyReleased event and forwards it to the registered KeyListener, if present.
      void keyTyped​(java.awt.event.KeyEvent e)
      Handles the keyTyped event and forwards it to the registered KeyListener, if present.
      void mark​(java.lang.String color)
      Marks the current cell of the BadgerBot with the specified color.
      void mouseClicked​(java.awt.event.MouseEvent e)
      Handles the mouseClicked event and forwards it to the registered MouseListener, if present.
      void mouseDragged​(java.awt.event.MouseEvent e)
      Handles the mouseDragged event and forwards it to the registered MouseMotionListener, if present.
      void mouseEntered​(java.awt.event.MouseEvent e)
      Handles the mouseEntered event and forwards it to the registered MouseListener, if present.
      void mouseExited​(java.awt.event.MouseEvent e)
      Handles the mouseExited event and forwards it to the registered MouseListener, if present.
      void mouseMoved​(java.awt.event.MouseEvent e)
      Handles the mouseMoved event and forwards it to the registered MouseMotionListener, if present.
      void mousePressed​(java.awt.event.MouseEvent e)
      Handles the mousePressed event and forwards it to the registered MouseListener, if present.
      void mouseReleased​(java.awt.event.MouseEvent e)
      Handles the mouseReleased event and forwards it to the registered MouseListener, if present.
      java.lang.String onColor()
      Returns the color of the current location of the bot.
      java.lang.String onItem()
      Returns the name of the top item at the current location of the bot.
      boolean setDirection​(java.lang.String direction)
      Sets the direction of the BadgerBot based on a string representation.
      void setImageFilename​(java.lang.String imageFilename)
      Sets the image for the BadgerBot from a specified filename.
      void setLocation​(int x, int y)
      Sets the location of the BadgerBot within the world.
      void setLocation​(java.awt.Point point)
      Sets the location of the BadgerBot based on a Point representing screen coordinates.
      void setPenColor​(java.lang.String color)
      Sets the pen color for the BadgerBot.
      void startDrawing()
      Starts the drawing mode for the BadgerBot.
      void step()
      Moves the BadgerBot one step in its current direction.
      void step​(int distance)
      Moves the BadgerBot forward by a specified distance.
      void stopDrawing()
      Stops the drawing mode for the BadgerBot.
      java.lang.String toString()
      Returns a string representation of the BadgerBot, including its id, coordinates, direction, pen color, and drawing status.
      java.lang.String toStringActions()
      Returns a string representation of the actions taken by the BadgerBot.
      java.lang.String toStringMethodContext()
      Returns the method context of the current method as a string.
      java.lang.String toStringMethodContextList()
      Returns a string representation of the method contexts from the list of actions.
      java.lang.String toStringState()
      Returns a string representation of the current state of the object.
      void turnLeft()
      Turns the BadgerBot to the left.
      void turnLeft​(int increments)
      Turns the BadgerBot to the left by a specified number of increments.
      void turnRight()
      Turns the BadgerBot to the right.
      void turnRight​(int increments)
      Turns the BadgerBot to the right by a specified number of increments.
      boolean turnToward​(int x, int y)
      Turns the BadgerBot to face towards a specified location (x, y).
      • Methods inherited from class java.lang.Object

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

      • BadgerBot

        public BadgerBot()
        No-argument constructor for the BadgerBot. Initializes the BadgerBot at the center of the BadgerWorld.
      • BadgerBot

        public BadgerBot​(int x,
                         int y)
        Constructor for the BadgerBot that initializes it at the specified coordinates.
        Parameters:
        x - The x-coordinate where the BadgerBot will be initialized.
        y - The y-coordinate where the BadgerBot will be initialized.
      • BadgerBot

        public BadgerBot​(int x,
                         int y,
                         java.lang.String itemName)
        Constructor for the BadgerBot that initializes it at the specified coordinates with a specified item.
        Parameters:
        x - The x-coordinate where the BadgerBot will be initialized.
        y - The y-coordinate where the BadgerBot will be initialized.
        itemName - The name of the item to associate with the BadgerBot.
    • Method Detail

      • setImageFilename

        public void setImageFilename​(java.lang.String imageFilename)
        Sets the image for the BadgerBot from a specified filename. Resizes and rotates the image to fit within the world cells.
        Parameters:
        imageFilename - the filename of the image to be set.
      • setLocation

        public void setLocation​(java.awt.Point point)
        Sets the location of the BadgerBot based on a Point representing screen coordinates. This method converts the screen coordinates to world cell coordinates and updates the BadgerBot's location accordingly.
        Parameters:
        point - The Point representing the screen coordinates of the mouse pointer.
      • setLocation

        public void setLocation​(int x,
                                int y)
        Sets the location of the BadgerBot within the world. Ensures that the coordinates are within the world's bounds.
        Parameters:
        x - the x-coordinate to set.
        y - the y-coordinate to set.
      • setDirection

        public boolean setDirection​(java.lang.String direction)
        Sets the direction of the BadgerBot based on a string representation. Possible directions: "NORTH", "NORTH_EAST", "EAST", "SOUTH_EAST", "SOUTH", "SOUTH_WEST", "WEST", "NORTH_WEST".
        Parameters:
        direction - the direction to set (e.g., "NORTH").
        Returns:
        true if the direction is valid and set, false otherwise.
      • turnToward

        public boolean turnToward​(int x,
                                  int y)
        Turns the BadgerBot to face towards a specified location (x, y). The direction is determined based on the relative position of the target location.
        Parameters:
        x - The x-coordinate of the target location.
        y - The y-coordinate of the target location.
        Returns:
        true if the BadgerBot's direction was changed, false if the BadgerBot is already at the target location.
      • mark

        public void mark​(java.lang.String color)
        Marks the current cell of the BadgerBot with the specified color. The cell's color is updated in the BadgerWorld, and the world is refreshed to reflect the change.
        Parameters:
        color - The color to mark the current cell.
      • facingColor

        public java.lang.String facingColor()
        Returns the color of the location the bot is currently facing. This method updates the facing location of the bot and retrieves the color of the cell in that location.
        Returns:
        The color of the location the bot is facing as a string.
      • facingItem

        public java.lang.String facingItem()
        Returns the name of the item in the location the bot is currently facing. This method updates the facing location of the bot and retrieves the item of the cell in that location. Built in items include "badger", "coin", "coinBag", "coinStack", "bat", "child", "monster", "pit", "airplane", "battleship", "book", "dog", "heart", "iron", "racecar", "soccerBall", "thimble", "tophat", "wheelbarrow", "worm" Items can be added @see BadgerWorld.addItem.
        Returns:
        The name of the item in the location the bot is facing as a string.
      • onColor

        public java.lang.String onColor()
        Returns the color of the current location of the bot. This method retrieves the color of the cell at the bot's current coordinates. Color names can be specified as a string, e.g., "RED", "BLUE", "GREEN", "YELLOW", "BLACK", "WHITE", "CYAN", "DARK_GRAY", "GRAY", "LIGHT_GRAY", "MAGENTA", "ORANGE", "PINK". or specify a hexadecimal color code. For example, "#FF0000".
        Returns:
        The color of the current location of the bot as a string.
      • onItem

        public java.lang.String onItem()
        Returns the name of the top item at the current location of the bot. This method retrieves the item at the cell at the bot's current coordinates.
        Returns:
        The name of the top item at the current location of the bot as a string.
      • isFacingEdge

        public boolean isFacingEdge()
        Checks if the BadgerBot is facing an edge.
        Returns:
        true if the bot is facing an edge, false otherwise.
      • isFacingWall

        public boolean isFacingWall()
        Checks if the BadgerBot is facing a wall.
        Returns:
        true if the bot is facing a wall, false otherwise.
      • step

        public void step()
        Moves the BadgerBot one step in its current direction. If the bot is at the edge of the world, it wraps around to the other side. If the bot is in drawing mode, it sets the color of the current cell to the bot's pen color. If it is facing a wall it won't move.
      • turnLeft

        public void turnLeft()
        Turns the BadgerBot to the left.
      • turnLeft

        public void turnLeft​(int increments)
        Turns the BadgerBot to the left by a specified number of increments.
        Parameters:
        increments - the number of 45-degree increments to turn.
      • turnRight

        public void turnRight()
        Turns the BadgerBot to the right.
      • turnRight

        public void turnRight​(int increments)
        Turns the BadgerBot to the right by a specified number of increments.
        Parameters:
        increments - the number of 45-degree increments to turn.
      • step

        public void step​(int distance)
        Moves the BadgerBot forward by a specified distance.
        Parameters:
        distance - the number of steps to move forward.
      • setPenColor

        public void setPenColor​(java.lang.String color)
        Sets the pen color for the BadgerBot. The pen color is used when the BadgerBot is in drawing mode. Color names can be specified as a string, e.g., "RED", "BLUE", "GREEN", "YELLOW", "BLACK", "WHITE", "CYAN", "DARK_GRAY", "GRAY", "LIGHT_GRAY", "MAGENTA", "ORANGE", "PINK". or specify a hexadecimal color code. For example, "#FF0000".
        Parameters:
        color - the name of the color to set (e.g., "RED" or "#FF0000").
      • startDrawing

        public void startDrawing()
        Starts the drawing mode for the BadgerBot. The bot will draw on the cells as it moves.
      • stopDrawing

        public void stopDrawing()
        Stops the drawing mode for the BadgerBot. The bot will not draw on the cells as it moves.
      • getDirection

        public java.lang.String getDirection()
        Gets the current direction of the BadgerBot. Possible directions: "NORTH", "NORTH_EAST", "EAST", "SOUTH_EAST", "SOUTH", * "SOUTH_WEST", "WEST", "NORTH_WEST"
        Returns:
        the current direction.
      • addKeyListener

        public void addKeyListener​(java.awt.event.KeyListener listener)
        Adds a KeyListener to the BadgerBot. The KeyListener will handle key events for the BadgerBot.
        Parameters:
        listener - The KeyListener to be added.
      • addMouseListener

        public void addMouseListener​(java.awt.event.MouseListener listener)
        Adds a MouseListener to the BadgerBot. The MouseListener will handle mouse events for the BadgerBot.
        Parameters:
        listener - The MouseListener to be added.
      • addMouseMotionListener

        public void addMouseMotionListener​(java.awt.event.MouseMotionListener listener)
        Adds a MouseMotionListener to the BadgerBot. The MouseMotionListener will handle mouse motion events for the BadgerBot.
        Parameters:
        listener - The MouseMotionListener to be added.
      • toStringMethodContext

        public java.lang.String toStringMethodContext()
        Returns the method context of the current method as a string. This context is retrieved from the stack trace.
        Returns:
        a string representing the current method context
      • toStringState

        public java.lang.String toStringState()
        Returns a string representation of the current state of the object. The string includes the x and y coordinates, direction, pen color, and drawing state.
        Returns:
        a string representing the current state of the object in the format "x,y direction penColor drawing/not drawing"
      • toString

        public java.lang.String toString()
        Returns a string representation of the BadgerBot, including its id, coordinates, direction, pen color, and drawing status.
        Overrides:
        toString in class java.lang.Object
        Returns:
        a string representation of the BadgerBot.
      • toStringActions

        public java.lang.String toStringActions()
        Returns a string representation of the actions taken by the BadgerBot.
        Returns:
        a string representation of the steps taken by the BadgerBot.
      • toStringMethodContextList

        public java.lang.String toStringMethodContextList()
        Returns a string representation of the method contexts from the list of actions. Each method context is appended to the string builder on a new line.
        Returns:
        A string containing the method contexts from the actions list.
      • keyTyped

        public void keyTyped​(java.awt.event.KeyEvent e)
        Handles the keyTyped event and forwards it to the registered KeyListener, if present.
        Specified by:
        keyTyped in interface java.awt.event.KeyListener
        Parameters:
        e - The KeyEvent object containing details of the keyTyped event.
      • keyPressed

        public void keyPressed​(java.awt.event.KeyEvent e)
        Handles the keyPressed event and forwards it to the registered KeyListener, if present.
        Specified by:
        keyPressed in interface java.awt.event.KeyListener
        Parameters:
        e - The KeyEvent object containing details of the keyPressed event.
      • keyReleased

        public void keyReleased​(java.awt.event.KeyEvent e)
        Handles the keyReleased event and forwards it to the registered KeyListener, if present.
        Specified by:
        keyReleased in interface java.awt.event.KeyListener
        Parameters:
        e - The KeyEvent object containing details of the keyReleased event.
      • mouseClicked

        public void mouseClicked​(java.awt.event.MouseEvent e)
        Handles the mouseClicked event and forwards it to the registered MouseListener, if present.
        Specified by:
        mouseClicked in interface java.awt.event.MouseListener
        Parameters:
        e - The MouseEvent object containing details of the mouseClicked event.
      • mousePressed

        public void mousePressed​(java.awt.event.MouseEvent e)
        Handles the mousePressed event and forwards it to the registered MouseListener, if present.
        Specified by:
        mousePressed in interface java.awt.event.MouseListener
        Parameters:
        e - The MouseEvent object containing details of the mousePressed event.
      • mouseReleased

        public void mouseReleased​(java.awt.event.MouseEvent e)
        Handles the mouseReleased event and forwards it to the registered MouseListener, if present.
        Specified by:
        mouseReleased in interface java.awt.event.MouseListener
        Parameters:
        e - The MouseEvent object containing details of the mouseReleased event.
      • mouseEntered

        public void mouseEntered​(java.awt.event.MouseEvent e)
        Handles the mouseEntered event and forwards it to the registered MouseListener, if present.
        Specified by:
        mouseEntered in interface java.awt.event.MouseListener
        Parameters:
        e - The MouseEvent object containing details of the mouseEntered event.
      • mouseExited

        public void mouseExited​(java.awt.event.MouseEvent e)
        Handles the mouseExited event and forwards it to the registered MouseListener, if present.
        Specified by:
        mouseExited in interface java.awt.event.MouseListener
        Parameters:
        e - The MouseEvent object containing details of the mouseExited event.
      • mouseDragged

        public void mouseDragged​(java.awt.event.MouseEvent e)
        Handles the mouseDragged event and forwards it to the registered MouseMotionListener, if present.
        Specified by:
        mouseDragged in interface java.awt.event.MouseMotionListener
        Parameters:
        e - The MouseEvent object containing details of the mouseDragged event.
      • mouseMoved

        public void mouseMoved​(java.awt.event.MouseEvent e)
        Handles the mouseMoved event and forwards it to the registered MouseMotionListener, if present.
        Specified by:
        mouseMoved in interface java.awt.event.MouseMotionListener
        Parameters:
        e - The MouseEvent object containing details of the mouseMoved event.