private EightPuzzleResult Solve(PriorityQueue <AStarState> queue) { int visitedNodesCount = 0; while (true) { AStarState state = queue.TakeLast(); visitedNodesCount++; if (state.Board.Equals(Board.FinalBoard)) { return(new EightPuzzleResult(state, visitedNodesCount)); } IReadOnlyCollection <Board> neighborBoards = state.Board.GetNeighbors(); foreach (Board board in neighborBoards) { if (!WasVisitedPreviously(state, board)) { AStarState newState = stateFactory.Create(board, state); queue.Add(newState); } } } }
private EightPuzzleResult Solve(PriorityQueue <AStarState> queue) { HashSet <int> visitedBoardHashes = new HashSet <int>(); int visitedNodesCount = 0; while (true) { AStarState state = queue.TakeLast(); visitedNodesCount++; if (state.Board.Equals(Board.FinalBoard)) { return(new EightPuzzleResult(state, visitedNodesCount)); } IReadOnlyCollection <Board> neighborBoards = state.Board.GetNeighbors(); foreach (Board board in neighborBoards) { int boardHash = board.GetHashCode(); if (!visitedBoardHashes.Contains(boardHash)) { AStarState newState = stateFactory.Create(board, state); queue.Add(newState); visitedBoardHashes.Add(boardHash); } } } }
public AStarStateWithVisitedHashes Create(Board board, AStarStateWithVisitedHashes previousState) { AStarState baseState = baseStateFactory.Create(board, previousState); var hashSet = new HashSet <int>(previousState.VisitedStatesHashes); hashSet.Add(board.GetHashCode()); return(new AStarStateWithVisitedHashes(baseState, hashSet)); }
public EightPuzzleResult Solve(Board startBoard) { AStarState startingState = stateFactory.CreateStartingState(startBoard); var queue = new PriorityQueue <AStarState>(); queue.Add(startingState); return(Solve(queue)); }
public AStarStateWithVisitedHashes CreateStartingState(Board startingBoard) { AStarState baseState = baseStateFactory.Create(startingBoard, null); return(new AStarStateWithVisitedHashes(baseState, new HashSet <int>(startingBoard.GetHashCode()))); }
public AStarStateWithVisitedHashes(AStarState baseState, HashSet <int> visitedStatesHashes) : base(baseState.Board, baseState.PreviousState, baseState.CurrentHeuristicScore, baseState.TotalHeuristicScore) { VisitedStatesHashes = visitedStatesHashes; }