-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolver.java
More file actions
116 lines (93 loc) · 3.8 KB
/
Solver.java
File metadata and controls
116 lines (93 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.leytontaylor.bfstwiddle;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Set;
/**
*
* @author leyto
*/
public class Solver{
public Set<String> closed = new HashSet<String>();
public Queue<Node> q = new LinkedList<>();
public Node initialNode;
public char[] goalState;
public String strGoal;
public Solver(String initialState, String goalState){
this.initialNode = new Node(initialState.toCharArray(), null, null);
this.goalState = goalState.toCharArray();
this.strGoal = goalState;
}
//BFS Traversal of the Queue q
String BFS(){
//Add the root node to the Queue
q.add(this.initialNode);
while(!q.isEmpty()){
//Remove the top node in the Queue
Node current = q.remove();
//If current Nodes boardstate == solved board state
//Traverse back up the tree and record all moves applied to
//The parent nodes
if(String.valueOf(current.boardState).equals(strGoal)){
return genPreviousMoves(current);
}else{
//Add current board state to the HashMap of already
//Visited board states
closed.add(String.valueOf(current.boardState));
//Create all children of node by applying the eight legal moves
List<Node> children=createChildren(current);
for(Node n:children){
//If closed contains the board state of the child
//Don't add the child to the Queue
if(closed.contains(String.valueOf(n.boardState)))
continue;
closed.add(String.valueOf(n.boardState));
q.add(n);
}
}
}return null;
}
//Generates a String of previos moves to the node
String genPreviousMoves(Node n){
List<Node> parentNodes= new LinkedList();
parentNodes.add(n);
//Add all parents to parentNodes
while(n.hasParent()){
parentNodes.add(n.getParent());
n=n.getParent();
}
String[] prevMoves = new String[parentNodes.size()];
//collect the sequence of prev moves
for(int i=0; i<parentNodes.size(); i++){
prevMoves[parentNodes.size()-1 -i]= parentNodes.get(i).prevMove;
}
String moveSeq = Arrays.toString(prevMoves);
String Solution= ("This Board can be solved by"+"\n"+
"First Move: "+ moveSeq+"\n"+
"Considered a total of "+Integer.toString(closed.size())+"\n"+
"Fringe Still contains "+Integer.toString(q.size()));
return Solution;
}
//created ArrayList of Nodes generated by doing all moves on
//the rootNode
private List<Node> createChildren(Node rootNode) {
List<Node> children = new ArrayList<>();
children.add(rootNode.rotAc());
children.add(rootNode.rotAcc());
children.add(rootNode.rotBc());
children.add(rootNode.rotBcc());
children.add(rootNode.rotCc());
children.add(rootNode.rotCcc());
children.add(rootNode.rotDc());
children.add(rootNode.rotDcc());
return children;
}
}