if (!list.isEmpty() && isadd) { Random random = new Random(); int index = random.nextInt(list.size()); Check check = list.get(index); // 2, 4出现概率3:1 int randomValue = random.nextInt(4); check.value = ( randomValue % 3 == 0 || randomValue % 3 == 1) ? 2 : 4;//只有[0,4)中的2才能生成4 isadd = false;
时间: 2024-02-14 18:36:15 浏览: 13
这段代码是在实现一个经典的数字方块游戏 2048 中的随机数生成功能。具体来说,它会检查方块列表是否为空并且当前可以添加新的方块。如果条件满足,它会生成一个随机数,然后根据一定的概率生成值为 2 或者 4 的新方块,并将其加入到方块列表中。最后,将 isadd 标志设置为 false,以便在下一次生成方块时能够正确地进行判断。
相关问题
绘制以下代码的uml图private void createBlock() { List<Block> list = getEmptyBlocks(); if (!list.isEmpty() && isadd) { Random random = new Random(); int index = random.nextInt(list.size()); Block block = list.get(index); // 2, 4出现概率3:1 block.value = (random.nextInt(4) % 3 == 0) ? 2 : 4; isadd = false; } }
根据代码可以画出以下 UML 图:
```
___________________
| Game2048 |
|__________________|
| - blocks[][]: Block |
| - isadd: boolean |
|___________________|
| + createBlock() |
| - getEmptyBlocks(): List<Block> |
|___________________|
___________________
| Block |
|__________________|
| - value: int |
| - foreground: Color |
| - background: Color |
| - blockFont: Font |
|___________________|
| + getValue(): int |
| + getForeground(): Color |
| + getBackground(): Color |
| + getBlockFont(): Font |
|___________________|
```
其中,主要涉及的类有:
- Game2048:主类,包含了游戏的整体逻辑和界面的绘制,其中包含了一个 Block 类型的二维数组,表示游戏中的方块,以及一个 boolean 类型的变量 isadd,表示是否可以添加新的方块。
- Block:表示游戏中的方块,包含方块的值、前景色和背景色等属性,以及相应的 getter 方法。其中,value 、foreground 和 background 都是私有属性,只能通过 getter 方法访问。
用Java写一个放烟花的代码
以下是一个简单的 Java 代码,用于放烟花:
```java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Fireworks extends JFrame {
private static final long serialVersionUID = 1L;
private List<Particle> particles = new ArrayList<Particle>();
private Random random = new Random();
private int counter = 0;
public Fireworks() {
setTitle("Fireworks");
setSize(800, 600);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel() {
private static final long serialVersionUID = 1L;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (Particle particle : particles) {
particle.draw(g);
}
}
};
setContentPane(panel);
setVisible(true);
run();
}
private void run() {
while (true) {
if (counter++ % 10 == 0) {
if (random.nextInt(100) < 30) {
particles.add(new Firework(random.nextInt(getWidth()), getHeight() - 1));
}
}
for (Particle particle : particles) {
particle.move();
}
particles.removeIf(p -> p.isDead());
repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private interface Particle {
void move();
void draw(Graphics g);
boolean isDead();
}
private class Firework implements Particle {
private int x;
private int y;
private int age;
private int maxAge;
private List<Spark> sparks = new ArrayList<Spark>();
public Firework(int x, int y) {
this.x = x;
this.y = y;
maxAge = random.nextInt(60) + 60;
}
@Override
public void move() {
if (age++ < maxAge) {
y--;
} else {
for (int i = 0; i < 20; i++) {
sparks.add(new Spark(x, y));
}
}
for (Spark spark : sparks) {
spark.move();
}
}
@Override
public void draw(Graphics g) {
if (age < maxAge) {
g.setColor(Color.WHITE);
g.drawLine(x, y, x, y);
} else {
for (Spark spark : sparks) {
spark.draw(g);
}
}
}
@Override
public boolean isDead() {
return age > maxAge && sparks.isEmpty();
}
}
private class Spark implements Particle {
private Point position;
private Point velocity;
private int age;
private int maxAge;
private Color color;
public Spark(int x, int y) {
position = new Point(x, y);
velocity = new Point(random.nextInt(5) - 2, random.nextInt(5) - 8);
maxAge = random.nextInt(20) + 20;
color = new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255));
}
@Override
public void move() {
position.translate(velocity.x, velocity.y);
velocity.translate(0, 1);
age++;
}
@Override
public void draw(Graphics g) {
g.setColor(color);
g.drawLine(position.x, position.y, position.x, position.y);
}
@Override
public boolean isDead() {
return age > maxAge;
}
}
public static void main(String[] args) {
new Fireworks();
}
}
```
这个程序使用 Java Swing 绘制烟花。它创建了一个 `Fireworks` 类来启动程序,该类继承自 `JFrame`,并在其中创建一个 `JPanel` 作为内容面板。这个程序在一个无限循环中运行,每隔一段时间就会发射一个烟花。烟花会发射出许多火花,火花会向上飞,然后开始下落,最终消失。