Class AI

java.lang.Object
  extended by AI

public class AI
extends java.lang.Object

Artificial Intelligence that the user will play against and try to eliminate.

Author:
Jonathan Chang

Field Summary
private  int moveCounter
           
private  Player p
           
 
Constructor Summary
AI(Grid gr, int direction, int color)
          Creates a Player in the given Grid with direction and color.
AI(Player p)
          Connects the AI to the player.
 
Method Summary
private  void blowBox(Box b)
          Finds a box near the player and places a bomb there to blow it up only if the player is right next to the box.
private  boolean canMoveThere(int x, int y)
          Checks if the location the AI wants to move to is free or if the location isn't free, it doesn't have a wall, box, or bomb.
private  Box findBox()
          Finds the closest Box that is nearby.
 Player getPlayer()
          Returns the player this AI controls.
private  void killPlayer(Player play)
          Moves toward the player that it wants to kill and lays a bomb there if the player is within range.
private  void moveToSafe(Bomb b)
          Moves to a safe location.
private  void moveTowards(int x, int y)
          Turns and moves towards the coordinates passed.
private  void moveTowards(Obj o)
          Moves towards the object that you pass it.
private  Player nearPlayer()
          Returns a player that is within 3 distance from the player.
private  PowerUp nearPowerUp()
          Checks if it is near a PowerUp.
private  Bomb notSafe()
          Checks if there is a bomb nearby that can kill it.
 void think()
          This is the method that ultimately decides what the AI should do.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

p

private Player p

moveCounter

private int moveCounter
Constructor Detail

AI

public AI(Grid gr,
          int direction,
          int color)
Creates a Player in the given Grid with direction and color.

Parameters:
gr - Grid this AI's Player is in
direction - initial direction this AI's Player is facing
color - the color of the Player

AI

public AI(Player p)
Connects the AI to the player.

Parameters:
p - Player this AI will control
Method Detail

think

public void think()
This is the method that ultimately decides what the AI should do. It first checks if the AI is in danger and moves away if it is. Then it checks if there is a player nearby and tries to kill it if there is. Then it checks to see if there is a powerup nearby and gets it if there is. Last it checks for boxes nearby and blows them up it there is.


notSafe

private Bomb notSafe()
Checks if there is a bomb nearby that can kill it. Nearby is defined as the same row or column and closer than the bomb's range + 1.

Returns:
the Bomb endangering the Player, or null if the Player is not in any danger

moveToSafe

private void moveToSafe(Bomb b)
Moves to a safe location.

Parameters:
b - Bomb which the Player will move away from

canMoveThere

private boolean canMoveThere(int x,
                             int y)
Checks if the location the AI wants to move to is free or if the location isn't free, it doesn't have a wall, box, or bomb.

Parameters:
x - x-coordinate of grid-square
y - y-coordinate of grid-square
Returns:
true if the Player can move to (x,y)

nearPowerUp

private PowerUp nearPowerUp()
Checks if it is near a PowerUp. Near is defined as the distance to the PowerUp is less than or equal to 3.

Returns:
the PowerUp near the Player, or null if the Player is not near a PowerUp

findBox

private Box findBox()
Finds the closest Box that is nearby.

Returns:
the Box nearest the Player, or null if Player isn't near a Box

blowBox

private void blowBox(Box b)
Finds a box near the player and places a bomb there to blow it up only if the player is right next to the box.

Parameters:
b - Box to blow up

moveTowards

private void moveTowards(Obj o)
Moves towards the object that you pass it.

Parameters:
o - Object to move towards

moveTowards

private void moveTowards(int x,
                         int y)
Turns and moves towards the coordinates passed.

Parameters:
x - x-coordinate to move towards
y - y-coordinate to move towards

nearPlayer

private Player nearPlayer()
Returns a player that is within 3 distance from the player.

Returns:
a player within 3 of this player

killPlayer

private void killPlayer(Player play)
Moves toward the player that it wants to kill and lays a bomb there if the player is within range.

Parameters:
play - Player to attempt to brutally murder by arson

getPlayer

public Player getPlayer()
Returns the player this AI controls.

Returns:
Player under the influence of this AI