public NodeIterator(Node start, int n) { for (int i = -2; i <= 2; i++) { for (int j = -2; j <= 2; j++) { Board settingClone = new BoardImpl((BoardImpl) setting); Player moverClone = new PlayerImpl(owner); settingClone.getPawn(moverClone, settingClone).setSquare(square); MovePawn tentative = new MovePawnImpl( square.getCol() + j, square.getRow() + i, moverClone, settingClone); if (tentative.isValid()) { nextList.add( new Node( new SquareImpl(square.getCol() + j, square.getRow() + i), n + 1, moverClone, start)); } } } backer = nextList.iterator(); }
public int pathLengthMin() { Player player; if (playerType(currentTurn) == MIN) { player = currentTurn; } else { player = currentTurn.getOpponent(); } if (bestMin == null) { Queue<Node> toVisit = new LinkedList<Node>(); Node current = new Node(setting.getPawn(player, setting).getSquare(), 0, player, null); toVisit.add(current); HashSet<Node> seen = new HashSet<Node>(); while (current.getSquare().getRow() != destRow(player.goalEnd())) { current = toVisit.remove(); seen.add(current); for (Node n : current) { if (!seen.contains(n)) { toVisit.add(n); } } } bestMin = new LinkedList<Node>(); while (current != null) { bestMin.add(current); current = current.getParent(); } } return bestMin.size(); }
private void populate() { // populate with MovePawns for (int i = -2; i <= 2; i++) { for (int j = -2; j <= 2; j++) { Square tmp = setting.getPawn(mover, setting).getSquare(); Board settingClone = new BoardImpl((BoardImpl) setting); Player moverClone = new PlayerImpl(mover); MovePawn tentative = new MovePawnImpl(tmp.getCol() + j, tmp.getRow() + i, moverClone, settingClone); if (tentative.isValid()) { pawnBackerList.add(new StateImpl(settingClone, moverClone, tentative)); } } } for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { Board settingClone = new BoardImpl((BoardImpl) setting); Player moverClone = new PlayerImpl(mover); PlaceWall vTentative = new PlaceWallImpl(i, j, Wall.VERTICAL, moverClone, settingClone); settingClone = new BoardImpl((BoardImpl) setting); moverClone = new PlayerImpl(mover); PlaceWall hTentative = new PlaceWallImpl(i, j, Wall.HORIZONTAL, moverClone, settingClone); if (vTentative.isValid()) { wallBackerList.add(new StateImpl(settingClone, moverClone, vTentative)); } if (hTentative.isValid()) { wallBackerList.add(new StateImpl(settingClone, moverClone, hTentative)); } } } }
public int alphabetaNum(int alpha, int beta, int depth) { if (depth == 0 || setting.getPawn(currentTurn, setting).getSquare().getRow() == destRow(currentTurn.goalEnd())) { determineScore(); return score; } int tmp; Iterator<StateImpl> nextMoves = swapIterator(); StateImpl s = null; if (playerType(currentTurn) == MIN) { while (nextMoves.hasNext() && alpha < beta) { s = nextMoves.next(); // get a new state s.nextMove.makeMove(); // apply the current move if (s.nextMove.type() == GenericMove.PAWN) { bestMin = null; } else { clearBestMin(s.setting); } tmp = s.alphabetaNum(alpha, beta, depth - 1); if (tmp > alpha) { alpha = tmp; } if (alpha >= beta) {} } return alpha; } if (playerType(currentTurn) == MAX) { while (nextMoves.hasNext() && alpha < beta) { s = nextMoves.next(); // get a new state s.nextMove.makeMove(); // apply the current move if (s.nextMove.type() == GenericMove.PAWN) { bestMax = null; } else { clearBestMax(s.setting); } tmp = s.alphabetaNum(alpha, beta, depth - 1); if (tmp < beta) { beta = tmp; } if (alpha >= beta) {} } return beta; } return 0; }