기본적으로 저는 현재 AP Comp Sci 클래스에서 제가하고있는 GridWorld Project를 가지고 있습니다. 나는 Pacman을하고있다. 여기에 행동 방법에 대한 내 코드 (GridWorld에 익숙하지 않은 사람들을 위해이 행위의 방법은 배우가 새로운 이동을 할 것으로 기대 될 때마다 호출됩니다) :IllegalArgumentException을 던지고 잡는 방법은 무엇입니까?
public void act()
{
Location loc = getLocation();
if(direction==null) {
}
else if(direction.equals("NORTH")) {
Location next = loc.getAdjacentLocation(loc.NORTH);
if(getGrid().isValid(next) && (getGrid().get(next)==null || getGrid().get(next) instanceof Food)) {
if(getGrid().get(next) instanceof Food)
addFood();
moveTo(next);
direction = "NORTH";
}
}
else if(direction.equals("SOUTH")) {
Location next = loc.getAdjacentLocation(loc.SOUTH);
if(getGrid().isValid(next) && (getGrid().get(next)==null || getGrid().get(next) instanceof Food)) {
if(getGrid().get(next) instanceof Food)
addFood();
moveTo(getLocation().getAdjacentLocation(getLocation().SOUTH));
direction = "SOUTH";
}
}
else if(direction.equals("EAST")) {
Location next = loc.getAdjacentLocation(loc.EAST);
if(getGrid().isValid(next) && (getGrid().get(next)==null || getGrid().get(next) instanceof Food)) {
if(getGrid().get(next) instanceof Food)
addFood();
moveTo(getLocation().getAdjacentLocation(getLocation().EAST));
direction = "EAST";
}
else if(getLocation().getCol()==20 && getLocation().getRow()==9) {
moveTo(new Location(9,0));
direction = "EAST";
}
}
else if(direction.equals("WEST")) {
Location next = loc.getAdjacentLocation(loc.WEST);
if(getGrid().isValid(next) && (getGrid().get(next)==null || getGrid().get(next) instanceof Food)) {
moveTo(getLocation().getAdjacentLocation(getLocation().WEST));
direction = "WEST";
}
else if(getLocation().getCol()==0 && getLocation().getRow()==9) {
moveTo(new Location(9,20));
direction = "WEST";
}
}
}
마지막 두의 이상한 표현하는 이유 진술이 BC 인 경우 나는 Pacman이 실제 게임에서 텔레포트 할 수 있기를 바랍니다. 이제 게임을 실행하면 약 90 %가 작동하지만 다른 10 %에서는 IllegalArgumentException이 발생합니다. 보드에없는 장소 (예 : (9, -1)) 및 (9,21)). 나는 이것을 어떻게 멈추기 위해 내가 어떻게 잡을 지 또는 던지거나 내가해야 할 일이 무엇인지 알고 싶다. 나는 잡동사니를 사용한 적이 없기 때문에 추론을 설명해주십시오!
고마워요!그래서 나는 당신의 충고를 받아 들였고 잠시가 지났지 만 마침내 내가 잘못 가고있는 곳에서 발견되어 결국 고쳐 버렸습니다. –
멋진 솔루션으로 끝나고 다행히 해결했습니다! –