Anonymous avatar Anonymous committed c1a52b6

more step and set buttons

Comments (0)

Files changed (1)

 // globals
 id = "UF UR UB UL DF DR DB DL FR FL BR BL UFR URB UBL ULF DRF DFL DLB DBR" ;
 pos = id ;
+start_pos = pos;
 sol = "";
 st = 0;
 brush = "U";
    var i, j, s=0, p=0, t ;
    for (i=0; i<11; i++) {
       j = i + Math.floor((12-i)*mt.random()) ;
-      solution.innerHTML = "hi!!!! " + j + "...";
       if (i != j) {
          t = a[i] ; a[i] = a[j] ; a[j] = t ; // swap
          p++ ;
       t = a[0] ; a[0] = a[1] ; a[1] = t ;
    }
    pos = a.join(" ") ;
-   solution.innerHTML = "" ;
+   document.forms[0].elements["solution"].value = "" ;
    showpos() ;
    return false ;
 }
 function solve() {
-   solution.innerHTML = "Working..." ;
-   setTimeout(function(){solution.innerHTML=sol=solvecube(pos, 0);st=0;},0) ;
+   document.forms[0].elements["solution"].value = "Working..." ;
+   setTimeout(function(){document.forms[0].elements["solution"].value=sol=solvecube(pos, 0);st=0;},0) ;
+}
+function endstep() {
+    while (st < sol.length)
+	nextstep();
 }
 function nextstep() {
     var i, j, str = "";
     var num = ["+", "2", "-"];
 
+    if (st < 0) st = 0;
     if (st > sol.length) {
-	solution.innerHTML = sol;
+	document.forms[0].elements["solution"].value = sol;
 	return;
     }
     for (i = 0; i < sol.length; i++) {
 		if (sol.charAt(i + 1) == num[j])
 		    break;
 	    }
-	    str += "<b><i>" + sol.charAt(i) + sol.charAt(i + 1) + "</i></b>";
+	    str += sol.charAt(i) + sol.charAt(i + 1) + "  | ";
 	    i++;
 	} else {
 	    str += sol.charAt(i);
     }
     st += 3;
 
-    solution.innerHTML = str;
+    document.forms[0].elements["solution"].value = str;
+}
+function firststep() {
+    while (st > 0)
+	priorstep();
+}
+function priorstep() {
+    var i, j, str = "";
+    var num = ["-", "2", "+"];
+
+    if (st > sol.length) st = sol.length;
+    if (st < 0) {
+	document.forms[0].elements["solution"].value = sol;
+	return;
+    }
+    st -= 3;
+    for (i = 0; i < sol.length; i++) {
+	if (i == st) {
+	    for (j = 0; j < 3; j++) {
+		mov(sol.charAt(i));
+		if (sol.charAt(i + 1) == num[j])
+		    break;
+	    }
+	    str += " |  " + sol.charAt(i) + sol.charAt(i + 1);
+	    i++;
+	} else {
+	    str += sol.charAt(i);
+	}
+    }
+
+    document.forms[0].elements["solution"].value = str;
 }
 function generate() {
-   solution.innerHTML = "Working..." ;
-   setTimeout(function(){solution.innerHTML=sol=solvecube(pos, 1);st=0;},0) ;
+   document.forms[0].elements["solution"].value = "Working...";
+   setTimeout(function(){document.forms[0].elements["solution"].value=sol=solvecube(pos, 1);st=sol.length;},0) ;
 }
 function dobenchmark() {
-   solution.innerHTML = "Working..." ;
-   setTimeout(function(){solution.innerHTML=benchmark();},0) ;
+   document.forms[0].elements["solution"].value = "Working...";
+   setTimeout(function(){document.forms[0].elements["solution"].value=benchmark();},0) ;
 }
 function cubeclear() {
    st = 0;
-   solution.innerHTML = sol = "";
+   document.forms[0].elements["solution"].value = sol = "";
    pos = id ;
    showpos() ;
    return false ;
     document.forms[0].elements["pos_entry"].value = "" ;
     showpos() ;
 }
+function setsol() {
+    var i, j, k, str = document.forms[0].elements["solution"].value ;
+
+    k = 2;
+    for (i = 0; i < str.length; i++) {
+	switch (str.charAt(i)) {
+	    case 'U':
+	    case 'D':
+	    case 'F':
+	    case 'B':
+	    case 'R':
+	    case 'L': j = 0; break;
+	    case '+':
+	    case '2':
+	    case '-': j = 1; break;
+	    case ' ': j = 2; break;
+	    default : j = 3; break;
+	}
+	if (j == 3 || j != (k + 1) % 3) {
+	    // put better error messages in...
+	    validation.innerHTML = "Bad character in sequence at pos " + i + " : " + str.charAt(i);
+	    return;
+	}
+	k = j;
+    }
+    if (j == 0)
+	validation.innerHTML = "Missing direction at end of sequence";
+    if (j == 1) str += " ";
+    sol = str;
+    st = 0;
+}
 function changecss(cls, atr, val) {
     // thanks to Shawn Olson & http://www.shawnolson.net for the general mechanics behind this
     var rules, tmp;
 <td id="C56" onclick="paint(this);"></td><td id="C12" onclick="paint(this);"></td><td id="C52" onclick="paint(this);"></td><td class="space"></td>
 <td class="big_r" colspan="7" rowspan="7" valign="top">
 	Click on a color in the palette then click on squares in the cube to paint them.
-	Paint center squares first. Click step to step through a solution (does not work
-	with generate). You can manually edit or enter a cube in the text box and click
-	set when done. Benchmark reports initialization and solving times to compare
+	Paint center squares first. You can manually edit or enter a cube or move sequence
+	in the text box and click set when done. Click the |&lt &lt &gt &gt| buttons to step
+	through a move sequence. Benchmark reports initialization and solving times to compare
 	browsers.
 </td>
 </tr>
 <br>
 <input type="button" name="solvebutton" value="solve" onclick="solve();">
 <input type="button" name="generatebutton" value="generate" onclick="generate();">
-<input type="button" value="step" onclick="nextstep();">
 <input type="button" value="benchmark" onclick="dobenchmark();">
 <br>
 <input type="text" name="pos_entry" size="70">
 <input type="button" value="set" onclick="setpos();">
 <br>
-<div id="solution"></div>
+<input type="text" name="solution" size="70">
+<input type="button" value="set" onclick="setsol();">
+<input type="button" value="|<" onclick="firststep();">
+<input type="button" value="<" onclick="priorstep();">
+<input type="button" value=">" onclick="nextstep();">
+<input type="button" value=">|" onclick="endstep();">
 <div id="validation"></div>
 <br><br>
 &copy; 2010 Evan Gates and Tomas Rokicki<br>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.