Answer the question
In order to leave comments, you need to log in
How can you write a Bot to move to food (there are its positions in a two-dimensional array) in the fastest way?
Is there a food code?
package project;
import java.util.Random;
import javafx.application.Platform;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Circle;
public class Food {
private Map map;
private Pane foodPane;
private Player player;
private Circle circle;
private Position foodPosition;
private Label seconds;
private final int timer = 5;
private int numOfCircles = 10;
private int time;
private int points;
private int size;
private int[][] cells;
public Food(Map map, Player player) {
this.map = map;
this.foodPane = new Pane();
this.map.getChildren().add(this.foodPane);
this.player = player;
this.size = this.map.getSize();
this.cells = this.map.getMap();
Thread thread = new Thread(() -> {
while (this.numOfCircles > 0) {
this.createFood();
Platform.runLater(() -> {
this.foodPane.getChildren().addAll(new Node[]{this.circle, this.seconds});
}
);
this.time = 5;
while (this.time > 0) {
Platform.runLater(() -> {
this.seconds.setText("" + this.time);
}
);
if (this.player.getPosition().equals(this.foodPosition)) {
this.points += this.time;
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException var1_2) {
// empty catch block
}
--this.time;
}
try {
Thread.sleep(10);
} catch (InterruptedException var1_3) {
// empty catch block
}
Platform.runLater(() -> {
this.foodPane.getChildren().clear();
}
);
--this.numOfCircles;
}
System.out.println(this.getPoints());
}
);
thread.start();
}
public int getPoints() {
return this.points;
}
Position pos;
private void createFood() {
int n;
int n2;
Random random = new Random();
double d = this.map.getUnit();
do {
n = random.nextInt(this.size);
n2 = random.nextInt(this.size);
} while (this.player.getPosition().equals(new Position(n, n2)) || this.map.getMap()[n][n2] == 1);
pos = new Position(n, n2);
this.circle = new Circle((double) n * d + d / 2.0, (double) n2 * d + d / 2.0, d / 4.0);
this.circle.setFill((Paint) Color.GREEN);
this.foodPosition = new Position(n, n2);
this.seconds = new Label("5");
this.seconds.setTranslateX((double) n * d);
this.seconds.setTranslateY((double) n2 * d);
}
public Position getPosition() {
return pos;
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question