• Welcome to SpeedSolving.com — the world’s largest puzzle community!

    You’re currently browsing as a guest, which means you have limited access to discussions, resources, member profiles, and community features.

    Join 50,000+ cubers and puzzle enthusiasts from around the world to ask questions, share solves, improve faster, and be part of the community.

    Registration is free, fast, and easy — create your account today.

    Already a member? Log in here to hide this message and start participating.

creating mobile cube scrambler; java problem

d4m4s74

Member
Joined
Nov 12, 2008
Messages
726
Location
Holland
WCA
2009ZALI01
YouTube
Visit Channel
I have the feeling atleast one of you knows java, so I'll ask it here
I'm making a mobile cube scramber, but whatever I do with the code, I still get duplicate moves or moves reversing the prevous

the code that generates the algorithm:
Code:
 [COLOR="Blue"]import[/COLOR] java.io.[COLOR="Silver"]*[/COLOR];
[COLOR="Blue"] import[/COLOR] java.util.[COLOR="Silver"]*[/COLOR];
 
[COLOR="Blue"]public class[/COLOR] cubescrambler {
    
    [COLOR="Blue"]private static[/COLOR] [COLOR="Navy"]BufferedReader[/COLOR] stdin[COLOR="Silver"]=[/COLOR]
    	[COLOR="Blue"]new[/COLOR] [COLOR="Navy"]BufferedReader[/COLOR]([COLOR="Blue"]new[/COLOR] [COLOR="Navy"]InputStreamReader[/COLOR]([COLOR="Navy"]System[/COLOR].in));
    	
    [COLOR="Blue"]public static void main[/COLOR]([COLOR="Navy"]String[/COLOR] args[]) [COLOR="Blue"]throws[/COLOR] [COLOR="Navy"]IOException[/COLOR] {
	[COLOR="Navy"]String[/COLOR] s;
	[COLOR="Blue"]int[/COLOR] i, j;
	[COLOR="Navy"]System[/COLOR].out.print([COLOR="DarkOrchid"]"Pick your length, "[/COLOR]);
	s[COLOR="Silver"]=[/COLOR]stdin.readLine();
	j[COLOR="Silver"]=[/COLOR][COLOR="Navy"]Integer[/COLOR].parseInt(s);
	[COLOR="Navy"]String[/COLOR] alg [COLOR="Silver"]=[/COLOR] [COLOR="DarkOrchid"]""[/COLOR];
	[COLOR="Blue"]int[/COLOR] num;
	[COLOR="Blue"]int[/COLOR] num0;
	[COLOR="Blue"]int[/COLOR] num2;
	[COLOR="Blue"]int[/COLOR] numx [COLOR="Silver"]=[/COLOR] 0;
		[COLOR="Navy"]Random[/COLOR] number [COLOR="Silver"]=[/COLOR] [COLOR="Blue"]new[/COLOR] [COLOR="Navy"]Random[/COLOR]();
	[COLOR="Blue"]for[/COLOR](i[COLOR="Silver"]=[/COLOR]1; i[COLOR="Silver"]<=[/COLOR]j; i[COLOR="Silver"]++[/COLOR]){
		num=number.nextInt(6)[COLOR="Silver"]+[/COLOR]1;
		[COLOR="Blue"]while[/COLOR] (num [COLOR="Silver"]==[/COLOR] numx) [COLOR="Lime"]//the problem is in this part[/COLOR]
		{num [COLOR="Silver"]=[/COLOR] number.nextInt(6)[COLOR="Silver"]+[/COLOR]1;}
		numx [COLOR="Silver"]=[/COLOR] num;
		num2 [COLOR="Silver"]=[/COLOR] number.nextInt(3);
		[COLOR="Blue"]switch[/COLOR] (num){
			[COLOR="Blue"]case[/COLOR] 1: alg [COLOR="Silver"]=[/COLOR] alg [COLOR="Silver"]+[/COLOR] [COLOR="DarkOrchid"]" F"[/COLOR];[COLOR="Blue"] break[/COLOR];
			[COLOR="Blue"]case[/COLOR] 2: alg [COLOR="Silver"]=[/COLOR] alg [COLOR="Silver"]+[/COLOR] [COLOR="DarkOrchid"]" B"[/COLOR]; [COLOR="Blue"]break[/COLOR];
			[COLOR="Blue"]case[/COLOR] 3: alg [COLOR="Silver"]=[/COLOR] alg [COLOR="Silver"]+[/COLOR] [COLOR="DarkOrchid"]" R"[/COLOR]; [COLOR="Blue"]break[/COLOR];
			[COLOR="Blue"]case[/COLOR] 4: alg [COLOR="Silver"]=[/COLOR] alg [COLOR="Silver"]+[/COLOR] [COLOR="DarkOrchid"]" L"[/COLOR]; [COLOR="Blue"]break[/COLOR];
			[COLOR="Blue"]case[/COLOR] 5: alg [COLOR="Silver"]=[/COLOR] alg [COLOR="Silver"]+[/COLOR] [COLOR="DarkOrchid"]" U"[/COLOR]; [COLOR="Blue"]break[/COLOR];
			[COLOR="Blue"]case[/COLOR] 6: alg [COLOR="Silver"]=[/COLOR] alg [COLOR="Silver"]+[/COLOR] [COLOR="DarkOrchid"]" B"[/COLOR]; [COLOR="Blue"]break[/COLOR];
		}
		[COLOR="Blue"]switch[/COLOR] (num2){
			[COLOR="Blue"]case[/COLOR] 0: [COLOR="Blue"]break[/COLOR];
			[COLOR="Blue"]case[/COLOR] 1: alg [COLOR="Silver"]=[/COLOR] alg [COLOR="Silver"]+[/COLOR] [COLOR="DarkOrchid"]"'"[/COLOR]; [COLOR="Blue"]break[/COLOR];
			[COLOR="Blue"]case[/COLOR] 2: alg [COLOR="Silver"]=[/COLOR] alg [COLOR="Silver"]+[/COLOR] [COLOR="DarkOrchid"]"2"[/COLOR]; [COLOR="Blue"]break[/COLOR];
		}
	}
	[COLOR="Navy"]System[/COLOR].out.println(alg);
	
    }
}

What am I doing wrong?

ps. I'll make a gui later and port it to j2me (mobile phone)
 
Last edited:
I could tell you, but I'd rather help you debug it yourself.

1. Fixate the generation so you can consistently reproduce the problem
=> a. Use a fixed seed for the random number generator: new Random(42);
=> b. Use a fixed length value, don't read it from input: j = 25;
=> c. Make sure these values produce the problem, use other values if not.

2. The direct precursor of the "alg" is the "num" value, so try printing it inside the for-loop and see whether you have duplicates there, too. If yes, dig deeper. If no, find out why.

I hope this will not only let you find the bug but also teach you useful general debugging techniques along the way (eliminating randomness to achieve reproducability, and tracing the error backwards from result to source).
 
Last edited:
yup, the fixed seed gives the following alg:
R F2 B2 R' B2 R F2 R F' B2 B2 L2 U' B U R2 B2 F' B B' B2 F' B' B' R2 every time I run it

I'll check it out.

btw.
I'm making it for java class (pun not intended)
 
I ran the randomizer multiple times (without fixed seed) and noticed the numbers do not "collide" and that the doubles are always B, then I saw I was missing the D's, in other words, the problem was not in my code, it was in my variables (I accidentally put a B in the code where there should have been a D)
 
Yep, that was it. I found it like described above, where I noticed that "num" didn't duplicate where "alg" did.

Next you'll want to prevent stuff like U D U' D'.
 
the code as it's right now
(I'm not going to color it again, it took me 10 minutes the last time)
Code:
/**
 * @(#)cubescrambler.java
 *
 * cubescrambler application
 *
 * @author D4m4s74
 * @version 1.00 2008/11/20
 */
 import java.io.*;
 import java.util.*;
 
public class cubescrambler {
    
    private static BufferedReader stdin=
    	new BufferedReader(new InputStreamReader(System.in));
    	
    public static void main(String args[]) throws IOException {
	//de gebruiker kiest de lengte.
	String s, c;
	int i, j, pos;
System.out.println("is the cube a 2x2x2/3x3x3 (0) or larger? (1)");
c=stdin.readLine();
if (c != null && c.length() == 0) {pos = 6; System.out.println("defaulting to 3x3");} else if (Integer.parseInt(c)== 1) {pos = 12;
} else {pos = 6;}
System.out.println("Pick the length of the algorythm");
s=stdin.readLine();
if (s != null && s.length() == 0) {j = 25;
System.out.println("defaulting to 25");} else {j=Integer.parseInt(s);}

	String alg = "";
	int num = -1;
	int num0;
	int num2;
	int back, back1, back2;
		back = 1;
		back1 = back2 = -1;
		Random number = new Random();
	for(i=1; i<=j; i++){
		num = number.nextInt(pos);
		while ((back1 == num) | (back2 == num))
		{
			num = number.nextInt(pos);
		
		}
		if (back == 1) {
			back = 2;
			back1 = num;
		} else {
			back = 1;
			back2 = num;
		}		
		num2 = number.nextInt(3);
		switch (num){
			case 0: alg = alg + " F"; break;
			case 1: alg = alg + " B"; break;
			case 2: alg = alg + " R"; break;
			case 3: alg = alg + " L"; break;
			case 4: alg = alg + " U"; break;
			case 5: alg = alg + " D"; break;
			case 6: alg = alg + " f"; break;
			case 7: alg = alg + " b"; break;
			case 8: alg = alg + " r"; break;
			case 9: alg = alg + " l"; break;
			case 10: alg = alg + " u"; break;
			case 11: alg = alg + " d"; break;
		}
		switch (num2){
			case 0: break;
			case 1: alg = alg + "'"; break;
			case 2: alg = alg + "2"; break;
		}
	}
	System.out.println(alg);
	
    }
}
I implemented other restrictions and now I'm working on a good 4x4 scramble
 
Last edited:
Back
Top