【问题标题】:random and time adequacy随机和时间充分性
【发布时间】:2016-04-06 20:55:25
【问题描述】:

我尝试在这个版本的基础上做我自己版本的“水果忍者”进行训练:https://github.com/emmaguy/FruitNinja

我做了一些小的改动。我想要做的是影响fruittype中“枚举”中对象的不同分数。

所以,我添加了这个函数(目的是检索当前的随机值):

public static int currentrandom() {
    return random.nextInt(FruitType2.values().length );
}

我补充说,

if (FruitType2.currentrandom()<=9) {
       score++;
} else {
       score=score-5;
   }

在 FruitProjectileManager 结束时。

FruitProjectileManager 的完整代码:

public class FruitProjectileManager02 implements ProjectileManager {

    private final Random random2 = new Random();
    private final List<Projectile> fruitProjectiles =
            new ArrayList<Projectile>();
    private final SparseArray<Bitmap> bitmapCache;
    private Region clip;
    private int maxWidth;
    private int maxHeight;


    private String FruitTypen = "FruitType2";


    public FruitProjectileManager02(Resources r) {

        bitmapCache = new SparseArray<Bitmap>(FruitType2.values().length);

        for (FruitType2 t : FruitType2.values()) {
            bitmapCache.put(t.getResourceId(), BitmapFactory.decodeResource(r, t.getResourceId(), new Options()));
        }
    }

    public void draw(Canvas canvas) {
        for (Projectile f : fruitProjectiles) {
            f.draw(canvas);
        }
    }

    public void update() {

        if (maxWidth < 0 || maxHeight < 0) {
            return;
        }
        if (random2.nextInt(1000) < 30) {
            fruitProjectiles.add(createNewFruitProjectile());
        }

        for (Iterator<Projectile> iter = fruitProjectiles.iterator(); iter.hasNext(); ) {

            Projectile f = iter.next();
            f.move();
            if (f.hasMovedOffScreen()) {
                iter.remove();
            }
        }
    }

    private FruitProjectile02 createNewFruitProjectile() {
        int angle = random2.nextInt(20) + 70;
        int speed = random2.nextInt(30) + 120;
        boolean rightToLeft = random2.nextBoolean();

        float gravity = random2.nextInt(6) + 8.0f;
        float rotationStartingAngle = random2.nextInt(360);
        float rotationIncrement = random2.nextInt(100) / 3.0f;

        if (random2.nextInt(1) % 2 == 0) {
            rotationIncrement *= -1;
        }

        return new FruitProjectile02(bitmapCache.get(FruitType2.randomFruit().getResourceId()), maxWidth, maxHeight,
                angle, speed, gravity, rightToLeft, rotationIncrement, rotationStartingAngle);
    }

    public void setWidthAndHeight(int width, int height) {
        this.maxWidth = width;
        this.maxHeight = height;
        this.clip = new Region(0, 0, width, height);
    }

    @Override
    public int testForCollisions(List<TimedPath> allPaths) {
        int score = 0;
        for (TimedPath p : allPaths) {
            for (Projectile f : fruitProjectiles) {
                if (!f.isAlive())
                    continue;
                Region projectile = new Region(f.getLocation());
                Region path = new Region();
                path.setPath(p, clip);

                if (!projectile.quickReject(path) && projectile.op(path, Region.Op.INTERSECT)) {
                    if (FruitType2.currentrandom() <= 9) {
                        score++;
                    } else {
                        score = score - 5;
                    }
                    f.kill();
                }
            }
        }
        return score;
    }
}

FruitType 的完整代码:

public enum FruitType2 {
    T02(R.drawable.n002),
    T04(R.drawable.n004),
    T06(R.drawable.n006),
    T08(R.drawable.n008),
    T10(R.drawable.n010),
    T12(R.drawable.n012),
    T14(R.drawable.n014),
    T16(R.drawable.n016),
    T18(R.drawable.n018),
    T20(R.drawable.n020),


    OTHER1(R.drawable.n003),
    OTHER2(R.drawable.n007),
    OTHER3(R.drawable.n011);

    private final int resourceId;

    private FruitType2(int resourceId) {
        this.resourceId = resourceId;
    }

    public int getResourceId() {
        return resourceId;
    }

    private static final Random random = new Random();


    public static int currentrandom() {


        return random.nextInt(FruitType2.values().length);
    }


    public static FruitType2 randomFruit() {


        return FruitType2.values()[random.nextInt(FruitType2.values().length)];
    }
}

我理解这个问题,当前随机(生成水果时)与水果切片时的随机不一样,我的问题是如何 解决这个问题。我不知道,所以如果你有一些线索,我很感兴趣。

提前谢谢你。

【问题讨论】:

    标签: java android random


    【解决方案1】:

    也许我不明白这个问题,但是为什么不将随机数存储在变量中呢?稍后您可以从变量中取出随机数。

    【讨论】:

      猜你喜欢
      • 2021-07-30
      • 1970-01-01
      • 2011-03-15
      • 2022-01-01
      • 2014-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-14
      相关资源
      最近更新 更多