I'm not very good at this but I thought I'd still give it a shot, you know, practice and stuff.
My tank will randomly decide to move or shoot. When it decides to shoot, it'll try to shoot at the nearest available target.
package com.richarda.tankwar;
import zove.ppcg.tankwar.*;
import java.util.Random;
import java.util.ArrayList;
public class RichardATank implements Tank
{
private String name;
public RichardATank()
{
this.name = "Richard-A Tank";
}
/**
*
* @param field
* The current battlefield, complete with all obstacle and tank
* positions
* @param position
*/
@Override
public void onSpawn(Battlefield field, MapPoint position)
{
}
/**
* The tank will randomly move around and occasionally shoot at the nearest available target.
*
* @param field
* The current battlefield, complete with all obstacle and tank
* positions
* @param position
* The tank's current position
* @param health
* The tank's current health
* @return
*/
@Override
public TurnAction onTurn(Battlefield field, MapPoint position, float health)
{
Random r = new Random();
int n = r.nextInt(5);
TurnAction.Direction direction;
switch(n)
{
case 0:
direction = TurnAction.Direction.NORTH;
break;
case 1:
direction = TurnAction.Direction.EAST;
break;
case 2:
direction = TurnAction.Direction.SOUTH;
break;
case 3:
direction = TurnAction.Direction.WEST;
break;
default:
return this.tryShootAtNearestTank(field, position);
}
return TurnAction.createMoveAction(direction, r.nextInt(2) + 1);
}
/**
*
* @param newPosition
* The tank's new position (may not be changed)
* @param hit
* What the tank hit, if it decided to shoot
*/
@Override
public void turnFeedback(MapPoint newPosition, FieldObjectType hit)
{
}
/**
*
* @param field
* The battlefield
* @param won
*/
@Override
public void onDestroyed(Battlefield field, boolean won)
{
}
@Override
public String getName()
{
return this.name;
}
/**
* Try and shoot at the nearest tank
*
* @param bf The battlefield
* @param curTankLocation The current tank's location
* @return TurnAction the shoot action to the nearest tank
*/
private TurnAction tryShootAtNearestTank(Battlefield bf, MapPoint curTankLocation)
{
MapPoint nearestTankLoc = this.getNearestTankLocation(bf, curTankLocation);
double firingAngle = curTankLocation.angleBetween(nearestTankLoc);
return TurnAction.createShootAction((float) firingAngle);
}
/**
* Try to find the nearest tank's location
*
* @param bf The battlefield
* @param curTankLocation The current tank's location
* @return MapPoint The location of the nearest tank
*/
private MapPoint getNearestTankLocation(Battlefield bf, MapPoint curTankLocation)
{
ArrayList<MapPoint> enemyTankLocations = this.getEnemyTanksOnField(bf, curTankLocation);
MapPoint nearestTankLoc = null;
for(MapPoint enemyTankLoc : enemyTankLocations)
{
if(nearestTankLoc == null || curTankLocation.distanceTo(enemyTankLoc) < curTankLocation.distanceTo(nearestTankLoc))
{
nearestTankLoc = enemyTankLoc;
}
}
return nearestTankLoc;
}
/**
* Get all enemy tanks on the field
*
* @param bf The battlefield
* @param curTankLocation The current tank's location
* @return ArrayList<MapPoint> A list with all enemy tanks in it
*/
private ArrayList<MapPoint> getEnemyTanksOnField(Battlefield bf, MapPoint curTankLocation)
{
int maxSize = Battlefield.FIELD_SIZE;
ArrayList<MapPoint> tanks = new ArrayList<MapPoint>();
for(int i = 0; i < maxSize; i++)
{
for(int j = 0; j < maxSize; j++)
{
FieldObjectType objType = bf.getObjectTypeAt(i, j);
if(objType == FieldObjectType.TANK)
{
MapPoint tankLocation = new MapPoint(i, j);
if(!tankLocation.equals(curTankLocation))
{
tanks.add(tankLocation);
}
}
}
}
return tanks;
}
}
The full code including the control program can be found here.