/** * 解答の操作文字列を得る。 * * @return */ public String getSolvedString() { String historyOperation = history.getOperationHistory(correctPuzzle); if (historyOperation != null && !historyOperation.isEmpty()) { return historyOperation; } else { return ""; } }
public void solvePuzzle() { startmills = System.currentTimeMillis(); List<String> candidateList = null; while (true) { long time = System.currentTimeMillis(); long duration = time - startmills; long secs = (duration / 1000); if (secs > MAXTIME) { break; } candidateList = answerBoard.getListFromCandidate(MAXSIZE); // Util.writeLog(candidateList.get(0)); answerBoard = new AnswerBoard(); for (String oldPazzle : candidateList) { String operationHistory = history.getOperationHistory(oldPazzle); int currentDistance = history.getDistanceHistory(oldPazzle); int currentMaxDistance = history.getMaxDistanceHistory(oldPazzle); for (int op = 0; op < 4; op++) { switch (op) { case UP_OP: board = new Board(width, height, oldPazzle); if (board.up()) { MovedPosition position = board.getPosition(); move(operationHistory + "U", position, currentDistance, currentMaxDistance); } break; case LEFT_OP: board = new Board(width, height, oldPazzle); if (board.left()) { MovedPosition position = board.getPosition(); move(operationHistory + "L", position, currentDistance, currentMaxDistance); } break; case RIGHT_OP: board = new Board(width, height, oldPazzle); if (board.right()) { MovedPosition position = board.getPosition(); move(operationHistory + "R", position, currentDistance, currentMaxDistance); } break; case DOWN_OP: board = new Board(width, height, oldPazzle); if (board.down()) { MovedPosition position = board.getPosition(); move(operationHistory + "D", position, currentDistance, currentMaxDistance); } break; } // 解答があったら終了。 if (answerBoard.contains(correctPuzzle)) { return; } } } } }
/** * * 移動とその距離をhistoryに加える処理。 * * @param operation * @param position * @param currentDistance * @param currentMaxDistance */ private void move( String operation, MovedPosition position, int currentDistance, int currentMaxDistance) { String boardStr = board.getBoardString(); char changedChar = position.getC(); int oldX = position.getOldX(); int oldY = position.getOldY(); int newX = position.getNewX(); int newY = position.getNewY(); if (history.getOperationHistory(boardStr) == null) { char[][] c = Util.createBoard(width, height, boardStr); int newDistance = distance.getNewDistance(changedChar, oldX, oldY, newX, newY, currentDistance); int newMaxDistance = distance.getMaxDistance(c); answerBoard.put(boardStr, newDistance, newMaxDistance); history.putHistory(boardStr, operation, newDistance, newMaxDistance); } }
/** * 解答が出たかどうか 出た場合はtrue * * @return */ public boolean solved() { String operation = history.getOperationHistory(correctPuzzle); return operation != null; }