java for swarm 学习笔记3

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",

                     "bug-swarm@santafe.edu", 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

转载于:https://www.cnblogs.com/proteus/archive/2012/07/11/2586080.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值