我正在编写我的第一个简单的星际争霸Broodwar AI,我的2个自定义类有问题。我们的想法是提供一系列预订,储存矿物和粪便气体的临时预订,因此我可以在订购工人建造结构时预留,以便其他工人和建筑物在此期间不会花费资源。工人到达建筑工地。问题是,当标准BWAPI事件侦听器“onFrame”启动countDownReservations()
方法时,根本不会调用各个数组行,而其余的AI根本不会被处理,并且实际上已被破坏。
因此,当我启动机器人时,除了控制台打印出“宾果游戏”外,没有任何反应 “0开始” 每一帧都一遍又一遍。 行“bingo1”甚至没有出现一次,所以当调用这个方法“countDown()”时,程序会以某种方式崩溃。遗憾的是,它根本没有出错,并且没有任何预编译器警告。
有谁可以解释什么是错的?
控制台输出:
Java项目:
Starcraft bot:
@Override
public void onFrame() {
System.out.println("bingo");
resourceReservations.countDownReservations();
public class ResourceReservations {
private ResourceReservation[] resourceReservationArray = new ResourceReservation[15];
public ResourceReservations() {
}
// looks through the array for a reservation with 0 time left on it (which should thus be empty)
// - and overwrites the empty reservation with a new one
// if no empty reservation can be found, nothing happens and a warning is output to the console
public void addReservation(int minerals, int gas, int timer) {
for (int loopCounter = 0; loopCounter < 15; loopCounter++) {
if (resourceReservationArray[loopCounter].getReservationTime() == 0) {
resourceReservationArray[loopCounter].setMinerals(minerals);
resourceReservationArray[loopCounter].setGas(gas);
resourceReservationArray[loopCounter].setTimerInSeconds(timer);
break;
}
}
System.out.println("Failed to add a reservation, no free space in array!");
}
// sums up all reserved minerals in the array
public int reservedMinerals() {
int reservedMinerals = 0;
for (int loopCounter = 0; loopCounter < 15; loopCounter++) {
reservedMinerals = reservedMinerals + resourceReservationArray[loopCounter].getMinerals();
}
return reservedMinerals;
}
// sums up all reserved vespene gas in the array
public int reservedVespeneGas() {
int reservedVespeneGas = 0;
for (int loopCounter = 0; loopCounter < 15; loopCounter++) {
reservedVespeneGas = reservedVespeneGas + resourceReservationArray[loopCounter].getGas();
}
return reservedVespeneGas;
}
// count down all the reservations
public void countDownReservations() {
for (int loopNumber = 0; loopNumber < 15; loopNumber++) {
System.out.println(loopNumber + " started");
resourceReservationArray[loopNumber].countDown();
System.out.println(loopNumber + " completed");
}
}
}
public class ResourceReservation {
private int minerals = 0;
private int vespeneGas = 0;
private int reservationTime = 0;
public ResourceReservation() {
}
// read status - - - - - - - - - - - - - - - - -
public int getGas() {
return vespeneGas;
}
public int getMinerals() {
return minerals;
}
public int getReservationTime() {
return reservationTime;
}
// update status - - - - - - - - - - - - - - - -
public void setGas(int newGas) {
vespeneGas = newGas;
}
public void setMinerals(int newMinerals) {
minerals = newMinerals;
}
public void setTimerInFrames(int newTimer) {
reservationTime = newTimer;
}
public void setTimerInSeconds(int newTimer) {
reservationTime = newTimer * 60;
}
// clear - - - - - - - - - - - - - - - - - - -
public void initialize() {
minerals = 0;
vespeneGas = 0;
reservationTime = 0;
}
// count down - - - - - - - - - - - - - - - - -
public void countDown() {
System.out.println("bingo1");
if (reservationTime > 0) {
System.out.println("bingo2");
reservationTime = reservationTime - 1;
System.out.println("bingo3");
if (reservationTime == 0) {
System.out.println("bingo4");
initialize();
}
}
}
}
答案 0 :(得分:0)
啊,我刚刚解决了,问题是15预约不存在
将此循环添加到Reservations类的头部解决了问题:
for (int x = 0; x < 15; x++) {
resourceReservationArray[x] = new ResourceReservation();
}
公共类ResourceReservations {
private ResourceReservation[] resourceReservationArray = new ResourceReservation[15];
public ResourceReservations() {
for (int x = 0; x < 15; x++) {
resourceReservationArray[x] = new ResourceReservation();
}
}
感谢您忍受我并提醒我调试。 Hadn还没在Java中做过。 ^^
编辑:哦,顺便说一句,我将预订数量减少到了5,并且在大约20场比赛中还没有看到它的溢出。