Overview
Features
Download
Source Code
Installing
Rules
Playing
Developing
a Ruler
Contributing
FAQ
|
Sample Random Ruler
Sample ruler which moves the rulers peasants and knights around at
random.
package net.sourceforge.battlefieldjava.samples ;
import java.awt.Color;
import java.awt.Point;
import java.util.HashMap;
import java.util.Random;
import net.sourceforge.battlefieldjava.engine.GameSettings;
import net.sourceforge.battlefieldjava.engine.Location;
import net.sourceforge.battlefieldjava.ruler.ICastle;
import net.sourceforge.battlefieldjava.ruler.IKnight;
import net.sourceforge.battlefieldjava.ruler.IObject;
import net.sourceforge.battlefieldjava.ruler.IPeasant;
import net.sourceforge.battlefieldjava.ruler.ISubject;
import net.sourceforge.battlefieldjava.ruler.Ruler;
import net.sourceforge.battlefieldjava.ruler.World;
public class RandomRuler extends Ruler
{
Random
random
= new Random();
public MyRuler(World world, Color color)
{
super(world, color);
}
/** Each ruler must supply a name */
public String getRulerName()
{
return
"Random";
}
/** Each ruler must supply a school name
(anything
will do, but I would recommend using
* a domain name if you have
one otherwise your
email address)
*/
public String getSchoolName()
{
return
"Sampler";
}
/** Called by the engine each time its
your rulers
turn to move.
* You may move each of your
pieces once and
only once.
* Knights move either move or
capture but not
both.
*/
public void orderSubjects(int
lastMoveTime)
{
// Get
the list of my peasants
IPeasant[] peasants = this.getPeasants();
// loop
through them
for (int
i = 0; i < peasants.length; i++)
{
IPeasant peasant = peasants[i];
// determine a
random direction to move in
int dir = getRandomDirection();
// and move the
peasant in that direction
// In debug
mode move will throw an exeption if you try
// to move off the
board or to a location currently occupied
// by another
piece.
this.move(peasant,
dir);
}
// Do the
same stuff for each of
the my rulers knights.
IKnight[] knights = this.getKnights();
for (int
i = 0; i < knights.length; i++)
{
IKnight knight = knights[i];
int dir = getRandomDirection();
this.move(knight,
dir);
}
}
// Generate a random directoin (value
between 1 and
8 inclusive).
int getRandomDirection()
{
return
(Math.abs(random.nextInt(8)) + 1;
}
}
|