Class BotWorld

java.lang.Object
  extended by BotWorld

public class BotWorld
extends Object


Nested Class Summary
 class BotWorld.Cell
          A class to represent an individual cell in (this) BotWorld, holding such attributes as number of dots present and whether the cell is a wall.
 
Field Summary
static char[] DIRS
          Available directions.
 
Constructor Summary
BotWorld()
          Create a new BotWorld, with a view and default dots/walls.
BotWorld(boolean hasView)
          Create a new BotWorld, with dots and a view if the argument is true.
BotWorld(boolean hasView, boolean hasDefaultSet)
          Create a new BotWorld, with a view and default set as specified.
BotWorld(boolean hasView, boolean hasDefaultSet, String filename)
          Create a new BotWorld, optionally with a view, default set, and additional objects from a file.
BotWorld(boolean hasView, boolean hasDefaultSet, String filename, int cellsWide, int cellsHigh)
          Creates a new BotWorld, optionally with a view, default set, additional objects from a file, and the given width and height.
BotWorld(boolean hasView, String filename)
          Create a new BotWorld, optionally with a view and initially populated with objects from a file.
BotWorld(String filename)
          Create a new BotWorld with a view, initially populated from a file.
 
Method Summary
 boolean add(Bot b)
          Add the given Bot to the World.
 boolean addDot(int x, int y)
          Add a dot at the given coordinates.
 boolean addDots(int x, int y, int n)
          Add the specified number of dots at the given coordinates.
 boolean addWall(int x, int y)
          Create a wall at the given coordinates.
 boolean addWall(int x, int y, char dir, int n)
          Create a wall from the given coordinate (inclusive) extending n spaces in the given direction (for a total of n wall segments).
 void clearCells()
          Clear the cells in this BotWorld.
 boolean ensureCells(int x1, int y1, int x2, int y2)
          Make sure there are cells created within the given range.
static String getCellKey(int x, int y)
          Primarily for internal use.
static int getSleepDelay()
          Return the current sleep delay.
 boolean hasDot(int x, int y)
          Return whether there is a dot at the given coordinate.
 boolean isWall(int x, int y)
          Find out whether (true) or not (false) there is a wall at the given coordinates.
 boolean loadFile(String filename)
          Load the given file into this BotWorld, creating walls/dots/&c as appropriate, using a BotFileReader.
 boolean loadFile(String filename, boolean clearFirst, boolean animate, Class<?> seed)
          Load the given file into this BotWorld, creating walls/dots/&c as appropriate, using a BotFileReader.
 boolean remove(Bot b)
          Remove the given Bot from the world.
 boolean removeDot(int x, int y)
          Try to remove a dot from the given coordinates.
 boolean removeView()
          Remove this BotWorld's view, if it has one.
 boolean removeWall(int x, int y)
          Remove the wall at the given location, if it exists.
protected  BotWorld.Cell safeGet(int x, int y)
           
static void safeSleep(int ms)
          Primarily for internal use.
static void setSleepDelay(int ms)
          Set the sleep delay (in miliseconds) - that is, how long the BotWorld pauses after each display update.
 void updateView()
          Allow an external class to request a view update.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DIRS

public static char[] DIRS
Available directions. For convenience in loops: don't count on the order

Constructor Detail

BotWorld

public BotWorld()
Create a new BotWorld, with a view and default dots/walls.


BotWorld

public BotWorld(boolean hasView)
Create a new BotWorld, with dots and a view if the argument is true.

Parameters:
hasView - whether or not this BotWorld has a graphical view

BotWorld

public BotWorld(boolean hasView,
                boolean hasDefaultSet)
Create a new BotWorld, with a view and default set as specified.

Parameters:
hasView - whether or not this BotWorld has a graphical view
hasDefaultSet - whether or not this BotWorld has the default set of dots and walls

BotWorld

public BotWorld(boolean hasView,
                boolean hasDefaultSet,
                String filename)
Create a new BotWorld, optionally with a view, default set, and additional objects from a file.

Parameters:
hasView - whether or not this BotWorld has a graphical view
hasDefaultSet - whether or not this BotWorld has the default set of dots and walls
filename - the file to load initial objects from

BotWorld

public BotWorld(boolean hasView,
                boolean hasDefaultSet,
                String filename,
                int cellsWide,
                int cellsHigh)
Creates a new BotWorld, optionally with a view, default set, additional objects from a file, and the given width and height.

Parameters:
hasView - whether or not this BotWorld has a graphical view
hasDefaultSet - whether or not this BotWorld has the default set of dots and walls
filename - the file to load initial objects from
cellsWide - the total cell width of the world
cellsHigh - the total cell height of the world

BotWorld

public BotWorld(boolean hasView,
                String filename)
Create a new BotWorld, optionally with a view and initially populated with objects from a file.

Parameters:
hasView - whether or not this BotWorld has a graphical view
filename - the file to load initial objects from

BotWorld

public BotWorld(String filename)
Create a new BotWorld with a view, initially populated from a file.

Parameters:
filename - the file to load initial objects from
Method Detail

add

public boolean add(Bot b)
Add the given Bot to the World.


addDot

public boolean addDot(int x,
                      int y)
Add a dot at the given coordinates.

Returns:
true if the addition was successful, false otherwise

addDots

public boolean addDots(int x,
                       int y,
                       int n)
Add the specified number of dots at the given coordinates.

Parameters:
n - the number of dots to add.
Returns:
true if the addition was successful, false otherwise

addWall

public boolean addWall(int x,
                       int y)
Create a wall at the given coordinates.

Returns:
true if a wall was created, false otherwise

addWall

public boolean addWall(int x,
                       int y,
                       char dir,
                       int n)
Create a wall from the given coordinate (inclusive) extending n spaces in the given direction (for a total of n wall segments).

Parameters:
x - the x coordinate of the start of the wall
y - the y coordinate of the start of the wall
dir - the direction in which to extend from the start coordinates, one of 'n', 'e', 's', or 'w'
n - the number of wall segments to create
Returns:
true (if the creation was successful, false otherwise)

clearCells

public void clearCells()
Clear the cells in this BotWorld. (Bots are unaffected.)


ensureCells

public boolean ensureCells(int x1,
                           int y1,
                           int x2,
                           int y2)
Make sure there are cells created within the given range.

Parameters:
x1 - the x coordinate of the upper left coordinate of the area
y1 - the y coordinate of the upper left coordinate of the area
x2 - the x coordinate of the lower right coordinate of the area
y2 - the y coordinate of the lower right coordinate of the area

getCellKey

public static String getCellKey(int x,
                                int y)
Primarily for internal use. Get the unique coordinate-based key for the specified coordinates (for a Cell).

Parameters:
x - the x coordinate the key should reflect
y - the y coordinate the key should reflect
Returns:
a String of the form "x, y"

getSleepDelay

public static int getSleepDelay()
Return the current sleep delay.


hasDot

public boolean hasDot(int x,
                      int y)
Return whether there is a dot at the given coordinate.


isWall

public boolean isWall(int x,
                      int y)
Find out whether (true) or not (false) there is a wall at the given coordinates.

Returns:
true if the given coordinates are at a wall, false otherwise

loadFile

public boolean loadFile(String filename)
Load the given file into this BotWorld, creating walls/dots/&c as appropriate, using a BotFileReader. Clear the current world contents first; don't animate.


loadFile

public boolean loadFile(String filename,
                        boolean clearFirst,
                        boolean animate,
                        Class<?> seed)
Load the given file into this BotWorld, creating walls/dots/&c as appropriate, using a BotFileReader.

Parameters:
filename - the file where the world description resides
clearFirst - whether (true) or not (false) to clear the world's current contents before adding file-described contents
animate - whether to update the view incrementally (true) or just update the view once at the end (false)
Returns:
true if the read succeeded, false if it failed. (no guarantee of no change on failure)

remove

public boolean remove(Bot b)
Remove the given Bot from the world. (Since Bots aren't directly tracked by the world, this just removes the graphics.) However: This leaves a world-less Bot.


removeDot

public boolean removeDot(int x,
                         int y)
Try to remove a dot from the given coordinates. Return true if successful, false if there was no dot to remove.


removeView

public boolean removeView()
Remove this BotWorld's view, if it has one.

Returns:
true if a view was removed, false otherwise

removeWall

public boolean removeWall(int x,
                          int y)
Remove the wall at the given location, if it exists.

Returns:
true if a wall was removed, false otherwise

safeGet

protected BotWorld.Cell safeGet(int x,
                                int y)

safeSleep

public static void safeSleep(int ms)
Primarily for internal use. Utility method, allowing Thread.sleep(..) to be used without worring about InterruptedException.

Parameters:
ms - miliseconds to sleep

setSleepDelay

public static void setSleepDelay(int ms)
Set the sleep delay (in miliseconds) - that is, how long the BotWorld pauses after each display update. Note that there is no pause when there is no view, so there is no need to set the sleep delay to zero if BotWorld is being run 'headless'.


updateView

public void updateView()
Allow an external class to request a view update.