Example Java program (Battleground)
Dr. David Matuszek, dave@acm.org,
Villanova University |
The idea of this program is that you have a Battleground containing
some number of Fighters. Each Fighter fights only for himself.
The program goes in a series of steps; at each step, each Fighter moves
and, if he encounters another Fighter, fights to the death. The stronger
Fighter always wins (if they are equally strong, the fighter who moved
wins) and adds the strength from his vanquished opponent to his own strength.
Play continues for a fixed number of steps or until there is only one
Fighter left (whichever comes first).
|
class Fighter
{
static int count; // CLASS VARIABLE: how many fighters there are
int strength = 1; // my strength
int direction; // direction I'm facing
Battleground place; // the Battleground that I fight on
int row, column; // where I am
int newRow, newColumn; // where I want to be
int lastMoved = -1; // last turn that I did something
Fighter (Battleground place, int row, int column) // Construct a Fighter.
{
direction = (int) (Math.random () * 4); // face in a direction 0 to 3
this.place = place; // remember my battleground
this.row = row; // remember my location
this.column = column;
count++; // count me
}
void doSomething (int step)
{
// If I've already moved, don't move again
if (step == lastMoved) return;
else lastMoved = step;
// sometimes change direction (about 10% of the time)
if (Math.random () < 0.10) direction = (int) (Math.random () * 4);
// figure out where I want to be
newRow = row; newColumn = column;
switch (direction)
{
case 0: newRow = (row + 1) % place.size; break;
case 1: newRow = (place.size + row - 1) % place.size; break;
case 2: newColumn = (column + 1) % place.size; break;
case 3: newColumn = (place.size + column - 1) % place.size; break;
}
// if that space is occupied, fight for it, else just move there
if (place.warzone [newRow][newColumn] != null)
fight (newRow, newColumn);
else
move (newRow, newColumn);
}
void move (int newRow, int newCol) // Do a simple, uncontested move
{
place.warzone [row][column] = null; // Move from here
place.warzone [newRow][newColumn] = this; // to here, and
row = newRow; column = newColumn; // remember where I am now.
}
void fight (int newRow, int newColumn) // Fight someone in that location
{
Fighter opponent = place.warzone [newRow][newColumn];
if (strength >= opponent.strength) // If I win,
{
strength += opponent.strength; // take my opponent's strength
move (newRow, newColumn); // and position;
Fighter.count--; // he's gone now, reduce count.
}
else
{
opponent.strength += strength; // But if I lose,
place.warzone [row][column] = null; // erase myself
Fighter.count--; // and count me gone.
}
}
public String toString () // Represent a fighter by just his strength
{
if (strength < 10) return " " + strength; // add a blank if < 10
else return "" + strength; // else just convert to String
}
}
public class Battleground
{
int size; // size of the battleground
Fighter [][] warzone; // array representing the battleground
Battleground (int size) // Construct a Battleground.
{
warzone = new Fighter [size][size]; // Make the array
this.size = size; // and remember how big it is.
for (int i = 0; i < size; i++) // Put a Fighter in 25% of
for (int j = 0; j < size; j++) // squares (the rest are initially
if (Math.random () < 0.25) // null).
warzone[i][j] = new Fighter (this, i, j);
}
void print () // Print the Battleground.
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (warzone[i][j] == null) System.out.print (" --");
else System.out.print (" " + warzone[i][j]);
}
System.out.println ();
}
}
public static void main (String args[])
{
final int SIZE = 10; // Constant: size of battleground
final int STEPS = 10; // Constant: number of steps to run simulation
Battleground battleground = new Battleground (SIZE); // Make battleground.
for (int step = 0; step < STEPS; step++) // Run for STEPS steps.
{
System.out.println ("Step " + step + ", " +
Fighter.count + " fighters:");
battleground.print ();
if (Fighter.count == 1) break; // Quit early if we have a winner,
for (int i = 0; i < SIZE; i++) // else loop through battleground
for (int j = 0; j < SIZE; j++) // and let each Fighter doSomething.
if (battleground.warzone[i][j] instanceof Fighter)
battleground.warzone[i][j].doSomething (step);
}
System.out.println ("At end (" + Fighter.count + " fighters left):");
battleground.print ();
}
}
Code Outline
class Fighter
Fighter (Battleground place, int row, int column) // Construct a Fighter.
void doSomething (int step)
void move (int newRow, int newCol) // Do a simple, uncontested move
void fight (int newRow, int newColumn) // Fight someone in that location
public String toString () // Represent a fighter by just his strength
public class Battleground
Battleground (int size) // Construct a Battleground.
void print () // Print the Battleground.
public static void main (String args[])
|