private boolean isLegalMove() { Quadrant quadrant = currentTank.getLocation(); Direction direction = currentTank.getDirection(); Quadrant neighborQuadrant = quadrant.getNeighbor(direction); return quadrant.hasEmptyNeighbor(direction, battleField) && !getOpponentLocation().equals(neighborQuadrant); }
private boolean isFinishedMove(Quadrant startQuadrant) { Quadrant currentQuadrant = currentTank.getLocation(); Direction direction = currentTank.getDirection(); Quadrant nextQuadrant = ActionField.getQuadrant( currentTank.getX() + direction.stepX, currentTank.getY() + direction.stepY); // nextQuadrant is not currentQuadrant.getNeighbor ! return currentQuadrant.v >= startQuadrant.v + 1 || currentQuadrant.h >= startQuadrant.h + 1 || nextQuadrant.v < startQuadrant.v - 1 || nextQuadrant.h < startQuadrant.h - 1; }
public void processMove(AbstractTank tank) throws InterruptedException { currentTank = tank; Quadrant startQuadrant = tank.getLocation(); Direction direction = tank.getDirection(); if (isLegalMove()) { while (!isFinishedMove(startQuadrant)) { tank.updateX(direction.stepX); tank.updateY(direction.stepY); Thread.sleep(tank.getSpeed()); } System.out.println("MOVED " + direction); } else { System.out.println("NO MOVE, direction: " + direction); Thread.sleep(tank.getSpeed()); } }