Demo Rats

This page contains code for demo RatBots. Each of these RatBots is intended to show one or two specific concepts that might be helpful in making your RatBot successful. None of the demos are very exceptional on their own. More demos may be posted as the competition continues.

RandomRat

package ratbot;

import actor.RatBot;
import java.util.Random;
/**
 * @author Spock
 * RandomRat chooses a random move each turn.
 */
public class RandomRat extends RatBot
{
    Random randy = new Random();
    
    public RandomRat()
    {
        setName("RandomRat");
    }
    
    @Override
    public int chooseAction()
    {        
        return randy.nextInt(360);  //move some Random direction!
    }
    
}

DartRat

package ratbot;

import actor.RatBot;
import java.util.Random;
/**
 * @author Spock
 * DartRat chooses to keep going the same direction as long as possible.
 */
public class DartRat extends RatBot
{
    Random randy = new Random();
    
    public DartRat()
    {
        setName("DartRat");
        setDirection(randy.nextInt(4)*90);
    }
    
    @Override
    public int chooseAction()
    {     
        if(!canMove()) //Only change directions when needed.
            return randy.nextInt(360);  //move some Random direction!     
        else
            return getDirection();
    }
    
}

SmartRandomRat

package ratbot;

import actor.RatBot;
import java.util.Random;
/**
 * @author Spock
 * SmartRandomRat chooses a random direction each time, 
 * but does not choose a direction that runs into a wall.
 */
public class SmartRandomRat extends RatBot
{
    Random randy = new Random();
    
    public SmartRandomRat()
    {
        setName("SmartRandomRat");
    }
    
    @Override
    public int chooseAction()
    {     
        int loopCount = 0;
    
        do
        {
            setDirection(randy.nextInt(360));  //turn some Random direction!     
            loopCount++;
        } while(!canMove() && loopCount < 10); //choose again if can't move.
        
        return getDirection();
    }
    
}

SmartDartRat

package ratbot;

import actor.RatBot;
import java.util.Random;
/**
 * @author Spock
 * SmartRat chooses to keep going the same direction as long as possible.
 * When it changes directions, it avoids directions where it can't move.
 */
public class SmartDartRat extends RatBot
{
    Random randy = new Random();
    
    public SmartDartRat()
    {
        setName("SmartDartRat");
        setDirection(randy.nextInt(4)*90);
    }
    
    @Override
    public int chooseAction()
    {     
        int loopCount = 0;
    
        while(!canMove() && loopCount < 10) //Only change directions when needed.
        {
            setDirection(randy.nextInt(360));  //move some Random direction!     
            loopCount++;
        }
        
        return getDirection();
    }
    
}

CheeseGetter

package ratbot;

import actor.RatBot;
import java.util.Random;
/**
 * @author Spock
 * CheeseGetter chooses a random move each turn.
 * However, CheeseGetter will always go to a Cheese it sees.  
 */
public class CheeseGetter extends RatBot
{
    Random randy = new Random();
    
    public CheeseGetter()
    {
        setName("CheeseGetter");
    }
    
    @Override
    public int chooseAction()
    {        
        for(int dir = 0; dir<360; dir+=45)
        {   //If I see a Cheese in a direction, go that direction!
            if(seesCheese(dir))
                return dir;
        }
        
        //If I don't see Cheese...
        return randy.nextInt(360);  //move some Random direction!
    }
    
}

LazyRat

package ratbot;

import actor.RatBot;
import java.util.Random;
/**
 * @author Spock
 * LazyRat chooses a random move each turn, and rests after 350 turns.
 */
public class LazyRat extends RatBot
{
    Random randy = new Random();
    private int moveNum;
    
    public LazyRat()
    {
        setName("LazyRat");
        moveNum = 0;
    }
    
    @Override
    public int chooseAction()
    {        
        moveNum++;
        if(moveNum > 350)
            return REST;
        
        return randy.nextInt(360);  //move some Random direction!
    }
    
    @Override
    public void initForRound()
    {	//Use this method to reset variables each round.
        moveNum = 0;
    }
}

WallBuilder

package ratbot;

import actor.RatBot;
import java.util.Random;
/**
 * @author Spock
 * WallBuilder chooses a random move or build each turn.
 */
public class WallBuilder extends RatBot
{
    Random randy = new Random();
    
    public WallBuilder()
    {
        setName("WallBuilder");
    }
    
    @Override
    public int chooseAction()
    {        
        return randy.nextInt(720);  //move or build some Random direction!
    }
    
}

ComboRat

package ratbot;

import actor.RatBot;
import java.util.Random;
/**
 * @author Spock
 * ComboRat moves as if it was SmartDartRat.
 * However, ComboRat will always go to a Cheese it sees.
 * (It is a combination of CheeseGetter and SmartDartRat.
 */
public class ComboRat extends RatBot
{
    Random randy = new Random();
    
    public ComboRat()
    {
        setName("ComboRat");
    }
    
    @Override
    public int chooseAction()
    {       
        //Look in every direction for a Cheese... 
        for(int dir = 0; dir<360; dir+=45)
        {
            if(seesCheese(dir) ) 
                return dir; //Go towards valuable Cheeses.
        }
        
        //If I don't see Cheese...
        return smartDart();  //move like SnartDartRat!
    }

    /**
     * Returns the value SmartDartRat would have chosen.
     * @return the value SmartDartRat would have chosen.
     */
    public int smartDart()
    {
        int loopCount = 0;
    
        while(!canMove() && loopCount < 10) //Only change directions when needed.
        {
            setDirection(randy.nextInt(360));  //face some Random direction!     
            loopCount++; //The loop count protects this from being an infinite loop
                         //Which could happen if I was boxed in.  
        }
        
        return getDirection(); //Go the direction you're facing.
    }

}

BeenThereRat

package ratbot;

import actor.RatBot;
import grid.Location;
import java.util.Random;
/**
 * @author Spock
 * BeenThereRat chooses to keep going the same direction as long as possible.
 * ...but avoids spaces it has been to too many times.
 */
public class BeenThereRat extends RatBot
{
    Random randy = new Random();
    int[][] beenThere = new int[20][20];
    
    private final int MAX_BEEN_THERE = 4;
    
    public BeenThereRat()
    {
        setName("BeenThere");
        setDirection(randy.nextInt(4)*90);
    }
    
    @Override
    public int chooseAction()
    {     
        updateBeenThere();
        
        //Only change directions when needed.
        if(!canMove() || haveIBeenThereTooMuch(getDirection())) 
            return randy.nextInt(360);  //move some Random direction!     
        else
            return getDirection();
    }
    
    public void updateBeenThere()
    {
        //Add one to beenThere for the space currently on.  
        beenThere[getLocation().getCol()][getLocation().getRow()]++;
    }
   
    private void resetBeenThere()
    {   //Sets all locations to zero in the beenThere variable.  
        for(int x=0;x<20;x++)
        {
            for(int y=0;y<20;y++)
            {
                beenThere[x][y] = 0;
            }
        }
    }
    /**
     * Determines whether I have been to space in the given direction 
     * more than MAX_BEEN_THERE times.  
     * @param dir the direction in question.
     * @return true if been there too many times.  
     */
    public boolean haveIBeenThereTooMuch(int dir)
    {
        Location loc = getLocation().getAdjacentLocation(dir);
        if(beenThere[loc.getCol()][loc.getRow()] > MAX_BEEN_THERE)
            return true;
        
        return false;
    }
    
    @Override
    public void initForRound()
    {
        resetBeenThere();
    }
}


Patience

package ratbot;

import actor.RatBot;
import java.util.Random;
/**
 * @author Spock
 * Patience chooses a random move each turn.
 * However, Patience will always go to a Cheese it sees.  
 * Also, Patience makes sure the Cheese is at least 10 points.
 */
public class Patience extends RatBot
{
    Random randy = new Random();
    
    public Patience()
    {
        setName("Patience");
    }
    
    @Override
    public int chooseAction()
    {       
        //Loop through each of the eight directions.
        for(int dir = 0; dir<360; dir+=45)
        {
            if(seesCheese(dir))
            {
                //This line checks to see the value of the Cheese I see.
                if(getCheese(dir).getPointValue() > 10)
                {
                    return dir;
                }
            }
        }
        
        //If I don't see Cheese...
        return randy.nextInt(360);  //move some Random direction!
    }
    
}

WallFinder

package ratbot;

import actor.RatBot;
import grid.Location;
import java.util.Random;
/**
 * @author Spock
 * WallFinder chooses a random move each turn.
 * It also looks to see where there are walls.  
 */
public class WallFinder extends RatBot
{
    Random randy = new Random();
    
    public WallFinder()
    {
        setName("WallFinder");
    }
    
    @Override
    public int chooseAction()
    {        
        //This is just to demonstrate HOW to look at walls.  
        //It is not an effective use of this knowledge.  
        if(canMove(NORTH))
        {
            Location up = getLocation().getAdjacentLocation(NORTH);
            boolean isWall = getSensorGrid().isWall(up, WEST);
            /* Now 'isWall' tells me if there is a wall to the west in 
             * the space just above me.
            */ 
        }
        
        
        return randy.nextInt(360);  //move some Random direction!
    }
    
}