用Java写一个狼和羊在地图上的
时间: 2024-03-25 07:41:28 浏览: 31
以下是用 Java 写的狼和羊在地图上的示例代码:
```
import java.util.Random;
public class Map {
private int width;
private int height;
private boolean[][] occupied;
public Map(int width, int height) {
this.width = width;
this.height = height;
this.occupied = new boolean[width][height];
}
public boolean isOccupied(int x, int y) {
if (x < 0 || x >= this.width || y < 0 || y >= this.height) {
return true;
}
return this.occupied[x][y];
}
public void occupy(int x, int y) {
if (x >= 0 && x < this.width && y >= 0 && y < this.height) {
this.occupied[x][y] = true;
}
}
public void vacate(int x, int y) {
if (x >= 0 && x < this.width && y >= 0 && y < this.height) {
this.occupied[x][y] = false;
}
}
}
public class Animal {
protected int x;
protected int y;
public Animal(int x, int y) {
this.x = x;
this.y = y;
}
public void move(int dx, int dy) {
this.x += dx;
this.y += dy;
}
public int getX() {
return this.x;
}
public int getY() {
return this.y;
}
}
public class Wolf extends Animal {
private int energy;
public Wolf(int x, int y) {
super(x, y);
this.energy = 10;
}
public void eat(Sheep sheep) {
this.energy += sheep.getNutritionValue();
}
public boolean isStarving() {
return this.energy <= 0;
}
public void loseEnergy() {
this.energy--;
}
public int getEnergy() {
return this.energy;
}
}
public class Sheep extends Animal {
private int nutritionValue;
public Sheep(int x, int y) {
super(x, y);
this.nutritionValue = 5;
}
public int getNutritionValue() {
return this.nutritionValue;
}
}
public class Simulation {
private Map map;
private Wolf wolf;
private Sheep sheep;
private Random random;
public Simulation(int width, int height) {
this.map = new Map(width, height);
this.random = new Random();
int wolfX = this.random.nextInt(width);
int wolfY = this.random.nextInt(height);
this.wolf = new Wolf(wolfX, wolfY);
this.map.occupy(wolfX, wolfY);
int sheepX = this.random.nextInt(width);
int sheepY = this.random.nextInt(height);
while (this.map.isOccupied(sheepX, sheepY)) {
sheepX = this.random.nextInt(width);
sheepY = this.random.nextInt(height);
}
this.sheep = new Sheep(sheepX, sheepY);
this.map.occupy(sheepX, sheepY);
}
public void run() {
int time = 0;
while (!this.wolf.isStarving()) {
time++;
System.out.println("Time: " + time);
System.out.println("Wolf: (" + this.wolf.getX() + ", " + this.wolf.getY() + "), Energy: " + this.wolf.getEnergy());
System.out.println("Sheep: (" + this.sheep.getX() + ", " + this.sheep.getY() + ")");
int dx = this.random.nextInt(3) - 1;
int dy = this.random.nextInt(3) - 1;
this.wolf.move(dx, dy);
if (this.wolf.getX() == this.sheep.getX() && this.wolf.getY() == this.sheep.getY()) {
this.wolf.eat(this.sheep);
this.map.vacate(this.sheep.getX(), this.sheep.getY());
int newX = this.random.nextInt(this.map.width);
int newY = this.random.nextInt(this.map.height);
while (this.map.isOccupied(newX, newY)) {
newX = this.random.nextInt(this.map.width);
newY = this.random.nextInt(this.map.height);
}
this.sheep = new Sheep(newX, newY);
this.map.occupy(newX, newY);
}
else {
this.wolf.loseEnergy();
}
System.out.println("=============================================");
}
System.out.println("Wolf is starving!");
}
}
public class Main {
public static void main(String[] args) {
Simulation simulation = new Simulation(10, 10);
simulation.run();
}
}
```
以上代码定义了五个类,`Map`、`Animal`、`Wolf`、`Sheep` 和 `Simulation`。`Map` 类代表地图,有 `width`、`height` 和 `occupied` 三个属性,分别代表宽度、高度和哪些位置被占用了;`Animal` 类代表动物,有 `x` 和 `y` 两个属性,分别代表横坐标和纵坐标;`Wolf` 类继承自 `Animal` 类,有 `energy` 属性,代表能量,还有 `eat`、`isStarving`、`loseEnergy` 和 `getEnergy` 方法,分别代表吃、是否饥饿、消耗能量和获取当前能量值;`Sheep` 类继承自 `Animal` 类,有 `nutritionValue` 属性,代表营养价值,还有 `getNutritionValue` 方法,返回营养价值;`Simulation` 类代表仿真,有 `map`、`wolf`、`sheep` 和 `random` 四个属性,分别代表地图、狼、羊和随机数生成器,还有 `run` 方法,代表运行仿真。
在 `Simulation` 类的构造函数中,会初始化地图、狼和羊。在 `run` 方法中,会循环执行以下操作:
1. 输出当前时间、狼的位置和能量、羊的位置。
2. 随机生成一个移动的方向,并让狼移动。
3. 如果狼和羊在同一个位置,狼吃掉羊,并在地图上移除羊,随机生成一个新的羊,并在地图上放置。
4. 否则,狼消耗能量。
5. 输出分割线。
如果狼的能量小于等于 0,就输出 `Wolf is starving!`。
这只是一个简单的示例,可以根据需要进一步扩展。