1 在eclipse中新建项目StartSimpleBug,在项目中新建包mypackage,在包中新建StartSimpleBug类。
2 编辑StartSimpleBug类如下。
// StartSimpleBug.java
// The Java SimpleBug application.
import swarm.Globals;
public class StartSimpleBug
{
public static void main (String[] args)
{
// The size of the bug's world and its initial position.
int worldXSize = 80;
int worldYSize = 80;
int xPos = 40;
int yPos = 40;
int i;
// Swarm initialization: all Swarm apps must call this first.
Globals.env.initSwarm ("SimpleBug", "2.1",
"[email protected]", args);//(本程序不必要)
System.out.println("I started at X = " + xPos + " Y = " + yPos);
// Have the bug randomly walk backward (-1), forward (+1), or
// not at all (0) in first the X and then the Y direction.
// (The randomMove() method, defined below, returns an
// integer between -1 and +1.) Note that the bug's world is a
// torus. If the bug walks off the edge of its rectangular
// world, it is magically transported (via the modulus
// operator) to the opposite edge.
for(i = 0; i < 100; i++)
{
xPos += randomMove();
yPos += randomMove();
xPos = (xPos + worldXSize) % worldXSize;
yPos = (yPos + worldYSize) % worldYSize;
System.out.println("I moved to X = " + xPos + " Y = " + yPos);
}
return;
}
// Returns -1, 0 or +1 with equal probability.
static int randomMove()
{
double randnum;
// Math.random returns a pseudo random number in the interval
// [0, 1).
randnum = Math.random();
if (randnum <= 0.33333)
return -1;
else if (randnum <= 0.66667)
return 0;
else
return 1;
}
}
3 运行
运行结果如下:
I started at X = 40 Y = 40
I moved to X = 39 Y = 40
I moved to X = 40 Y = 41
I moved to X = 41 Y = 40
I moved to X = 42 Y = 41
I moved to X = 43 Y = 42
I moved to X = 44 Y = 41
I moved to X = 43 Y = 41
I moved to X = 43 Y = 42
I moved to X = 44 Y = 43
I moved to X = 43 Y = 44
I moved to X = 44 Y = 43
I moved to X = 45 Y = 43
I moved to X = 45 Y = 43
I moved to X = 45 Y = 43
I moved to X = 45 Y = 44
I moved to X = 45 Y = 45
I moved to X = 45 Y = 44
I moved to X = 46 Y = 44
I moved to X = 47 Y = 45
I moved to X = 47 Y = 46
I moved to X = 48 Y = 46
I moved to X = 49 Y = 46
I moved to X = 48 Y = 46
I moved to X = 47 Y = 46
I moved to X = 48 Y = 45
I moved to X = 47 Y = 46
I moved to X = 46 Y = 47
I moved to X = 45 Y = 48
I moved to X = 46 Y = 49
I moved to X = 45 Y = 50
I moved to X = 44 Y = 49
I moved to X = 43 Y = 49
I moved to X = 42 Y = 49
I moved to X = 43 Y = 48
I moved to X = 43 Y = 48
I moved to X = 42 Y = 48
I moved to X = 42 Y = 48
I moved to X = 41 Y = 48
I moved to X = 42 Y = 49
I moved to X = 43 Y = 48
I moved to X = 43 Y = 49
I moved to X = 42 Y = 50
I moved to X = 42 Y = 49
I moved to X = 43 Y = 48
I moved to X = 44 Y = 48
I moved to X = 43 Y = 48
I moved to X = 43 Y = 47
I moved to X = 43 Y = 46
I moved to X = 42 Y = 46
I moved to X = 42 Y = 46
I moved to X = 41 Y = 46
I moved to X = 41 Y = 45
I moved to X = 42 Y = 44
I moved to X = 43 Y = 44
I moved to X = 42 Y = 44
I moved to X = 41 Y = 43
I moved to X = 42 Y = 42
I moved to X = 41 Y = 41
I moved to X = 42 Y = 41
I moved to X = 41 Y = 40
I moved to X = 41 Y = 40
I moved to X = 40 Y = 39
I moved to X = 39 Y = 38
I moved to X = 39 Y = 37
I moved to X = 40 Y = 38
I moved to X = 41 Y = 38
I moved to X = 41 Y = 38
I moved to X = 42 Y = 38
I moved to X = 41 Y = 37
I moved to X = 42 Y = 36
I moved to X = 41 Y = 36
I moved to X = 40 Y = 37
I moved to X = 41 Y = 38
I moved to X = 40 Y = 39
I moved to X = 39 Y = 40
I moved to X = 39 Y = 39
I moved to X = 40 Y = 38
I moved to X = 40 Y = 38
I moved to X = 39 Y = 37
I moved to X = 38 Y = 38
I moved to X = 38 Y = 37
I moved to X = 38 Y = 38
I moved to X = 37 Y = 37
I moved to X = 36 Y = 37
I moved to X = 37 Y = 38
I moved to X = 38 Y = 37
I moved to X = 38 Y = 36
I moved to X = 37 Y = 35
I moved to X = 37 Y = 36
I moved to X = 36 Y = 35
I moved to X = 36 Y = 35
I moved to X = 36 Y = 36
I moved to X = 36 Y = 36
I moved to X = 37 Y = 37
I moved to X = 37 Y = 38
I moved to X = 36 Y = 38
I moved to X = 36 Y = 38
I moved to X = 37 Y = 39
I moved to X = 36 Y = 40
I moved to X = 37 Y = 39