Μέθοδος main σε κώδικα

Συζητήσεις για την Java

Συντονιστές: WebDev Moderators, Super-Moderators

Απάντηση
Άβαταρ μέλους
TheBrainyMan
Δημοσιεύσεις: 5
Εγγραφή: 14 Ιουν 2011 23:37
Τοποθεσία: Athens

Μέθοδος main σε κώδικα

Δημοσίευση από TheBrainyMan » 28 Ιουν 2011 21:43

Ψάχνω να βρώ στο παρακάτω παράδειγμα κώδικα, πού πρέπει να μπεί η μέθοδος main ώστε να τρέξω το πρόγραμμα, οποιαδήποτε βοήθεια θα σας ήμουν ευγνώμον ! Είναι μεγάλος ο κώδικας, όποιος έχει τη διάθεση και το χρόνο έχει καλώς...Το παλεύω και μόνος μου με δοκιμές :)


Κώδικας: Επιλογή όλων

package manhattan;


import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;

public class Manhattan {
    
String str = " "; // initial state
String goal = " "; //goal state
    
PriorityQueue <StateOrder> queue;

Map<String,Integer> levelDepth;

Map<String,String> stateHistory;
    
int nodes = 0; //counter for node generation
int limit = 100; //counter for limit
int unique = -1;//counter for uniq state
int newValue; //counter depth limit
int a; //position of blank
int h; //heuristic

String currState;
boolean solution = false;


Manhattan &#40;String str,String goal&#41;&#123;
queue = new PriorityQueue <StateOrder> &#40;&#41;;
levelDepth = new HashMap<String, Integer>&#40;&#41;;
stateHistory = new HashMap<String,String>&#40;&#41;;
this.str = str;
this.goal = goal;
addToQueue&#40;str,null&#41;;
&#125;

void doSearch &#40;&#41;&#123;

while &#40;!queue.isEmpty&#40;&#41;&#41;&#123;

currState = queue.poll&#40;&#41;.toString&#40;&#41;;//RETRIEVE then remove first node

if &#40;currState.equals&#40;goal&#41;&#41;&#123; // check if current state is goal state
solution = true;
printSolution&#40;currState&#41;;// print solutions
break;
&#125;

if &#40;levelDepth.get&#40;currState&#41; == limit&#41;&#123;//check if under limit
solution = false;
printSolution&#40;currState&#41;;// print solutions
break;
&#125;

else &#123;
//expand currentstate then add expanded node to the of openlist

a = currState.indexOf&#40;"0"&#41;;// get index position of 0 &#40;blank&#41;

//left
while &#40;a != 0 && a != 3 && a != 6&#41;&#123;// if blank not in the left most column then it able move left
String nextState = currState.substring&#40;0,a-1&#41;+"0"+currState.charAt&#40;a-1&#41;+currState.substring&#40;a+1&#41;;//swap blank with destination
addToQueue&#40;nextState, currState&#41;;//add expanded node to openlist
nodes++;
break;
&#125;

//up
while &#40;a!=0 && a!=1 && a!=2&#41;&#123;//if blank not in the very top of row then it able to move up
String nextState = currState.substring&#40;0,a-3&#41;+"0"+currState.substring&#40;a-2,a&#41;+currState.charAt&#40;a-3&#41;+currState.substring&#40;a+1&#41;;//swap blank with destination
addToQueue&#40;nextState, currState&#41;;//add expanded node to openlist
nodes++; //nodes = nodes + 1; a node is being genereted add it to counter
break;
&#125;

//right
while&#40;a != 2 && a != 5 && a != 8&#41;&#123;// if blank not in the right most column then it able to move right
String nextState = currState.substring&#40;0,a&#41;+currState.charAt&#40;a+1&#41;+"0"+currState.substring&#40;a+2&#41;;//swap blank with destination
addToQueue&#40;nextState, currState&#41;;//add expanded node to openlist
nodes++;
break;
&#125;

//down
while &#40;a != 6 && a != 7 && a != 8&#41; &#123;// if blank not in the very bottom row then it able to move down
String nextState = currState.substring&#40;0,a&#41;+currState.substring&#40;a+3,a+4&#41;+currState.substring&#40;a+1,a+3&#41;+"0"+currState.substring&#40;a+4&#41;;//swap blank with destination
addToQueue&#40;nextState, currState&#41;;//add expanded node to openlist
nodes++;
break;
&#125;

&#125;

&#125;

if &#40;solution&#41;&#123;
System.out.println&#40;"Solution Exist"&#41;;
&#125;

else &#123;
System.out.println&#40;"Solution not yet found! My suggestion are&#58;"&#41;;
System.out.println&#40;"1. Try to increse level depth limit "&#41;;
System.out.println&#40;"2. Use other heuristc "&#41;;
System.out.println&#40;"3. Maybe it is physically impossible"&#41;;
&#125;

&#125;

private void addToQueue &#40;String newState, String oldState&#41;&#123;
if&#40;!levelDepth.containsKey&#40;newState&#41;&#41;&#123;// check repeated state
newValue = oldState == null ? 0 &#58; levelDepth.get&#40;oldState&#41; + 1;
unique ++;
levelDepth.put&#40;newState, newValue&#41;;
h = calcManhattan&#40;newState,goal&#41;; // calculate heuristic from newstate
//h= 0;
queue.add&#40;new StateOrder&#40;h,newState&#41;&#41;;//add to priority queue
stateHistory.put&#40;newState, oldState&#41;;
&#125;

&#125;

int calcManhattan&#40;String currState, String goalState&#41;&#123;
//lookup table for manhattan distance
int &#91;&#93;&#91;&#93; manValue = &#123;
&#123;0,1,2,1,2,3,2,3,4&#125;,
&#123;1,0,1,2,1,2,3,2,3&#125;,
&#123;2,1,0,3,2,1,4,3,2&#125;,
&#123;1,2,3,0,1,2,1,2,3&#125;,
&#123;2,1,2,1,0,1,2,1,2&#125;,
&#123;3,2,1,2,1,0,3,2,1&#125;,
&#123;2,3,4,1,2,3,0,1,2&#125;,
&#123;3,2,3,2,1,2,1,0,1&#125;,
&#123;4,3,2,3,2,1,2,1,0&#125;,
&#125;;
//calculate manhattan distance
int heu = 0 ;
int result = 0;
//String a = null;
for &#40;int i=1; i<9;i++&#41;&#123;
heu = manValue&#91;currState.indexOf&#40;String.valueOf&#40;i&#41;&#41;&#93;&#91;goalState.indexOf&#40;String.valueOf&#40;i&#41;&#41;&#93;;
result = result + heu;

&#125;
return result;
&#125;

void printSolution &#40;String currState&#41;&#123;
if &#40;solution&#41;&#123;
System.out.println&#40;"Solution found in " +levelDepth.get&#40;currState&#41;+" step&#40;s&#41;"&#41;;
System.out.println&#40;"Node generated&#58; "+ nodes&#41;;
System.out.println&#40;"Unique Node generated&#58; "+ unique&#41;;
&#125;
else &#123;
System.out.println&#40;"Solution not found!"&#41;;
System.out.println&#40;"Depth Limit Reached!"&#41;;
System.out.println&#40;"Node generated&#58; "+ nodes&#41;;
System.out.println&#40;"Unique Node generated&#58; "+ unique&#41;;
&#125;

String traceState = currState;
while &#40;traceState != null&#41; &#123;
System.out.println&#40;traceState + " at " + levelDepth.get&#40;traceState&#41;&#41;;
try&#123;
for&#40;int z=0;z<9;z++&#41;&#123;
System.out.print&#40;" " + String.valueOf&#40;traceState.charAt&#40;z&#41;&#41; + " "&#41;;
if &#40;&#40;z+1&#41; % 3 == 0&#41;&#123;System.out.println&#40;&#41;;&#125;
&#125;
&#125;
catch &#40;NullPointerException e&#41; &#123;&#125;
traceState = stateHistory.get&#40;traceState&#41;;
&#125;

&#125;
&#125;

Κώδικας: Επιλογή όλων




public class StateOrder implements Comparable
&#123;

/**

Constructs a state order with a given priority and description.

@param aPriority the priority of state

@param aDescription the description of state

*/

private int priority;

private String description;

public StateOrder &#40;int aPriority, String aDescription&#41;

&#123;
priority = aPriority;
description = aDescription;
&#125;

@Override
public String toString&#40;&#41;

&#123;

return description;

&#125;

public int compareTo&#40;Object otherObject&#41;

&#123;

StateOrder other = &#40;StateOrder&#41; otherObject;

if &#40;priority < other.priority&#41; return -1;

if &#40;priority > other.priority&#41; return 1;

return 0;
&#125;
&#125;





Απάντηση

Επιστροφή στο “Java”

Μέλη σε σύνδεση

Μέλη σε αυτήν τη Δ. Συζήτηση: Δεν υπάρχουν εγγεγραμμένα μέλη και 0 επισκέπτες