DemoRats

advertisement
package ratbot;
import actor.RatBot;
import grid.Location;
import java.util.Random;
/**
* @author Spock
* BeenThereRat is a Rat that keeps track of where it has been.
* It avoids spaces that it has already been to twice.
*/
public class BeenThereRat extends RatBot
{
Random randy = new Random();
int[][] beenThere = new int[24][24];
int spinCount = 0;
public BeenThereRat()
{
setName("BeenThere");
}
public int chooseAction()
{
//Mark that you have been to the space you are on.
beenThere[getLocation().getRow()][getLocation().getCol()]++;
Location front = getLocation().getAdjacentLocation(getDirection());
if(canMove() && beenThere[front.getRow()][front.getCol()] < 2)
{
spinCount = 0;
return MOVE;
}
//Otherwise turn a random direction.
setDesiredHeading(randy.nextInt(8)*45);
spinCount++;
if(spinCount > 5 && canMove())
return MOVE;
return CHANGE_HEADING;
}
public void initforRound()
{
//This method is called at the start of each round.
for(int r=0;r<24;r++)
for(int c=0;c<24;c++)
beenThere[r][c] = 0;
}
}
package ratbot;
import actor.RatBot;
import actor.Actor;
import actor.Block;
import grid.Grid;
import java.util.Random;
import grid.Location;
/**
* @author Spock
* Checker doesn't turn towards Blocks.
* Each move it randomly chooses to move or turn.
*/
public class Checker extends RatBot
{
Random randy = new Random();
public Checker()
{
setName("Checker");
}
public int chooseAction()
{
int myChoice = randy.nextInt(3)+1;
setDesiredHeading(randy.nextInt(8)*45);
if(myChoice == MOVE && !canMove())
myChoice = CHANGE_HEADING;
if(myChoice == CHANGE_HEADING)
dontTurnTowardPlacesYouCantGo();
return myChoice;
}
public void dontTurnTowardPlacesYouCantGo()
{
//Checks to see that your desired heading isn't facing a Block
//If it is it tries to change it.
Grid<Actor> grid = this.getSensorGrid();
int trapCounter = 0;
Location nextLoc = getLocation().getAdjacentLocation(getDirection());
while(
{
grid.isValid(nextLoc) &&
!(grid.get(nextLoc) instanceof Block) &&
trapCounter<30)
//Choose another direction
setDesiredHeading(randy.nextInt(8)*45);
nextLoc = getLocation().getAdjacentLocation(getDesiredHeading());
trapCounter++; //keeps this from becoming an inifinite loop!
}
}
}
package ratbot;
import actor.RatBot;
import actor.Actor;
import actor.Cheese;
import grid.Grid;
import java.util.Random;
import grid.Location;
import java.util.ArrayList;
/**
* @author Spock
* CheeseRat is much smarter than the other demo rats.
* CheeseRat always moves forward if there is a Cheese in front of it.
*
otherwise, it turns if there is a Cheese in an adjacent Location.
*
finally if no Cheese is adjacent it moves like ZipRat.
*/
public class CheeseRat extends RatBot
{
Random randy = new Random();
public CheeseRat()
{
setName("CheeseRat");
}
public int chooseAction()
{
Grid<Actor> grid = getSensorGrid();
//If a cheese is in front of you go there.
Location inFront = getLocation().getAdjacentLocation(getDirection());
if(grid.isValid(inFront))
{
Actor front = grid.get(inFront);
if(front instanceof Cheese)
return MOVE;
}
//If a cheese is adjacent to you, turn towards it.
ArrayList<Actor> neighbors = grid.getNeighbors(this.getLocation());
for(Actor a : neighbors)
{
if(a instanceof Cheese)
{
//turn toward the cheese
setDesiredHeading(getLocation().getDirectionToward(a.getLocation()));
return CHANGE_HEADING;
}
}
//If no neighboring cheese act like ZipRat.
setDesiredHeading(randy.nextInt(8)*45);
return randy.nextInt(3)+1;
}
}
package ratbot;
import actor.RatBot;
import java.util.Random;
import grid.Location;
/**
* @author Spock
* LazyRat moves randomly (like ZipRat) but keeps track of the moveNum
* and stops moving after 200 moves each round. (Lazy!)
*/
public class LazyRat extends RatBot
{
Random randy = new Random();
int moveNum = 0;
public LazyRat()
{
setName("LazyRat");
}
public int chooseAction()
{
moveNum++;
//Lazy Rat stops after 200 moves...
if(moveNum > 200) return REST;
//Otherwise move in the same fashion as ZipRat.
setDesiredHeading(randy.nextInt(8)*45);
return randy.nextInt(3)+1;
}
public void initforRound()
{
//This method is called at the start of each round.
moveNum = 0;
}
}
package ratbot;
import actor.RatBot;
import java.util.Random;
import grid.Location;
/**
* @author Spock
* RandomRat chooses a random move each turn.
*/
public class RandomRat extends RatBot
{
Random randy = new Random();
public RandomRat()
{
setName("RandomRat");
}
public int chooseAction()
{
setTarget(new Location(randy.nextInt(24),randy.nextInt(24)));
setDesiredHeading(randy.nextInt(8)*45);
//Randomly choose to move, turn, build or launch...
return randy.nextInt(5)+1;
}
}
package ratbot;
import actor.RatBot;
import java.util.Random;
import grid.Location;
/**
* @author Spock
* Shooter just shoots PaintBalls!
*/
public class Shooter extends RatBot
{
Random randy = new Random();
public Shooter()
{
setName("Shooter");
}
public int chooseAction()
{
setTarget(new Location(randy.nextInt(24),randy.nextInt(24)));
return LAUNCH_PAINTBALL;
}
}
package ratbot;
import actor.RatBot;
import java.util.Random;
import grid.Location;
/**
* @author Spock
* SmartRat is smarter than many other demo Rats.
* If it can move forward it does. Otherwise it turns.
* This allows it to avoid running into walls.
*/
public class SmartRat extends RatBot
{
Random randy = new Random();
public SmartRat()
{
setName("SmartRat");
}
public int chooseAction()
{
if(canMove())
return MOVE;
else
{
setDesiredHeading(randy.nextInt(8)*45);
return CHANGE_HEADING;
}
}
}
package ratbot;
import actor.RatBot;
import java.util.Random;
import grid.Location;
/**
* @author Spock
* ZipRat is a RandomRat that only focuses on moving.
* Each move it randomly chooses to move or turn.
*/
public class ZipRat extends RatBot
{
Random randy = new Random();
public ZipRat()
{
setName("Zippy");
}
public int chooseAction()
{
setDesiredHeading(randy.nextInt(8)*45);
return randy.nextInt(3)+1;
}
}
Download