Project

General

Profile

Download (2.07 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / solvers / cube3 / Tools.java @ b95ceafc

1
package org.distorted.solvers.cube3;
2

    
3
import java.util.Random;
4

    
5
public class Tools {
6

    
7
	// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
8
	// Check if the cube string s represents a solvable cube.
9
	// 0: Cube is solvable
10
	// -1: There is not exactly one facelet of each colour
11
	// -2: Not all 12 edges exist exactly once
12
	// -3: Flip error: One edge has to be flipped
13
	// -4: Not all corners exist exactly once
14
	// -5: Twist error: One corner has to be twisted
15
	// -6: Parity error: Two corners or two edges have to be exchanged
16
	// 
17
	/**
18
	 * Check if the cube definition string s represents a solvable cube.
19
	 * 
20
	 * @param s is the cube definition string , see {@link Facelet}
21
	 * @return 0: Cube is solvable<br>
22
	 *         -1: There is not exactly one facelet of each colour<br>
23
	 *         -2: Not all 12 edges exist exactly once<br>
24
	 *         -3: Flip error: One edge has to be flipped<br>
25
	 *         -4: Not all 8 corners exist exactly once<br>
26
	 *         -5: Twist error: One corner has to be twisted<br>
27
	 *         -6: Parity error: Two corners or two edges have to be exchanged
28
	 */
29
	public static int verify(String s) {
30
		int[] count = new int[6];
31
		try {
32
			for (int i = 0; i < 54; i++)
33
				count[Color.toInt(s.substring(i, i + 1))]++;
34
		} catch (Exception e) {
35
			return -1;
36
		}
37

    
38
		for (int i = 0; i < 6; i++)
39
			if (count[i] != 9)
40
				return -1;
41

    
42
		FaceCube fc = new FaceCube(s);
43
		CubieCube cc = fc.toCubieCube();
44

    
45
		return cc.verify();
46
	}
47

    
48
	/**
49
	 * Generates a random cube.
50
	 * @return A random cube in the string representation. Each cube of the cube space has the same probability.
51
	 */
52
	public static String randomCube() {
53
		CubieCube cc = new CubieCube();
54
		Random gen = new Random();
55
		cc.setFlip((short) gen.nextInt(CoordCube.N_FLIP));
56
		cc.setTwist((short) gen.nextInt(CoordCube.N_TWIST));
57
		do {
58
			cc.setURFtoDLB(gen.nextInt(CoordCube.N_URFtoDLB));
59
			cc.setURtoBR(gen.nextInt(CoordCube.N_URtoBR));
60
		} while ((cc.edgeParity() ^ cc.cornerParity()) != 0);
61
		FaceCube fc = cc.toFaceCube();
62
		return fc.to_String();
63
	}
64
}
(9-9/9)