Example #1
0
 def testGetSolution(self):
     for _ in range(10):
         cube = Cube()
         cube = scrambler._randomlyScrambleCube(cube)
         solution = scrambler._getSolution(cube)
         for move in solution.split():
             cube(move)
         self.assertTrue(cube.isSolved())
Example #2
0
 def play(self, color: Color, position: Point):
     if position in self.cases:
         return Exception('position is already taken')
     fresh_cube = Cube(color, position)
     self.cases[position] = fresh_cube
     surrounding_position = position.surrounding()
     for key, value in surrounding_position.items():
         if key in self.cases:
             fresh_cube.occupied_side(value)
             self.cases[key].occupied_side(value.get_inverse())
     return self.win(color, position)
Example #3
0
def predict(stickers):
    c = Cube(stickers)
    s = np.reshape(c.stickers, (1, 54)) 
    soln = ""
    count = 0

    while not c.isSolved():
        count += 1
        if count > 50:
            return "Solution could not be found."
        pred = predictMove(s, getTrainedModel())
        soln += " " + pred
        c(pred)

    return soln
Example #4
0
 def testToStickerStringUnsolved(self):
     cube = Cube()
     for move in superflip.split():
         cube(move)
     expected = "UBULURUFURURFRBRDRFUFLFRFDFDFDLDRDBDLULBLFLDLBUBRBLBDB"
     stickers = scrambler._toStickerString(cube.stickers)
     self.assertEqual(stickers, expected)
    def _solve_cube(self, *args):
        move_list = self.cube._move_list[:]
        for (face, n, layer) in move_list[::-1]:
            self.rotate_face(face, -n, layer, steps=3)
        self.cube._move_list = []

        self._trainCube = TrainCube()
Example #6
0
 def testSameCube(self):
   cube = Cube()
   new_cube = cube.copy()
   self.assertEqual(cube, new_cube)
   cube.rotate(0)
   self.assertNotEqual(cube, new_cube)
   new_cube.rotate(1)
   self.assertEqual(cube, new_cube)
   cube.rotate(0)
   self.assertNotEqual(cube, new_cube)
   new_cube.rotate(1)
   self.assertEqual(cube, new_cube)
Example #7
0
def randomScrambles():

    cube = Cube()
    cube = _randomlyScrambleCube(cube)

    # Attempt to get solution
    try:
        solution = _getSolution(cube)
    # Get another scramble if solution cannot be found
    except RuntimeError:
        print("Solution not found. Attempting another scramble.")
        return randomScrambles()

    return _getDataFromSolution(cube, solution)
Example #8
0
 def testToStickerStringSolved(self):
     cube = Cube()
     stickers = scrambler._toStickerString(cube.stickers)
     self.assertEqual(
         stickers, "UUUUUUUUURRRRRRRRRFFFFFFFFFDDDDDDDDDLLLLLLLLLBBBBBBBBB")
Example #9
0
 def testRandomlyScrambleCube(self):
     cube = Cube()
     cube = scrambler._randomlyScrambleCube(cube)
     self.assertFalse(cube.isSolved())
Example #10
0
 def setUp(self):
     self.cube1 = Cube()
     self.cube2 = Cube()
     for move in superflip.split():
         self.cube2(move)
Example #11
0
class TestCube(unittest.TestCase):

    def setUp(self):
        self.cube1 = Cube()
        self.cube2 = Cube()
        for move in superflip.split():
            self.cube2(move)

    def testInitSolved(self):
        for i in range(6):
            self.assertTrue(np.array_equal(
                self.cube1.stickers[i], np.full((3, 3), i)))

    def testIsSolved(self):
        self.assertTrue(self.cube1.isSolved())

    def testRotateDSolved(self):
        self.cube1.rotateD()
        for i in range(2):
            self.assertTrue(np.array_equal(
                self.cube1.stickers[i], np.full((3, 3), i)))
        for i in range(2, 4):
            self.assertTrue(np.array_equal(
                self.cube1.stickers[i, :2, :], np.full((2, 3), i)))
            self.assertTrue(np.array_equal(
                self.cube1.stickers[i, 2, :], np.full((3), i + 2)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[4, :2, :], np.full((2, 3), 4)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[4, 2, :], np.full((3), 3)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[5, :2, :], np.full((2, 3), 5)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[5, 2, :], np.full((3), 2)))

    def testRotateD2Solved(self):
        self.cube1.rotateD2()
        for i in range(2):
            self.assertTrue(np.array_equal(
                self.cube1.stickers[i], np.full((3, 3), i)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[2, :2, :], np.full((2, 3), 2)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[2, 2, :], np.full((3), 3)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[3, :2, :], np.full((2, 3), 3)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[3, 2, :], np.full((3), 2)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[4, :2, :], np.full((2, 3), 4)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[4, 2, :], np.full((3), 5)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[5, :2, :], np.full((2, 3), 5)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[5, 2, :], np.full((3), 4)))

    def testRotateDPrimeSolved(self):
        self.cube1.rotateDprime()
        for i in range(2):
            self.assertTrue(np.array_equal(
                self.cube1.stickers[i], np.full((3, 3), i)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[2, :2, :], np.full((2, 3), 2)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[2, 2, :], np.full((3), 5)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[3, :2, :], np.full((2, 3), 3)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[3, 2, :], np.full((3), 4)))
        for i in range(4, 6):
            self.assertTrue(np.array_equal(
                self.cube1.stickers[i, :2, :], np.full((2, 3), i)))
            self.assertTrue(np.array_equal(
                self.cube1.stickers[i, 2, :], np.full((3), i - 2)))

    def testRotateUSolved(self):
        self.cube1.rotateU()
        for i in range(2):
            self.assertTrue(np.array_equal(
                self.cube1.stickers[i], np.full((3, 3), i)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[2, 1:, :], np.full((2, 3), 2)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[2, 0, :], np.full((3), 5)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[3, 1:, :], np.full((2, 3), 3)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[3, 0, :], np.full((3), 4)))
        for i in range(4, 6):
            self.assertTrue(np.array_equal(
                self.cube1.stickers[i, 1:, :], np.full((2, 3), i)))
            self.assertTrue(np.array_equal(
                self.cube1.stickers[i, 0, :], np.full((3), i - 2)))

    def testRotateU2Solved(self):
        self.cube1.rotateU2()
        for i in range(2):
            self.assertTrue(np.array_equal(
                self.cube1.stickers[i], np.full((3, 3), i)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[2, 1:, :], np.full((2, 3), 2)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[2, 0, :], np.full((3), 3)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[3, 1:, :], np.full((2, 3), 3)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[3, 0, :], np.full((3), 2)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[4, 1:, :], np.full((2, 3), 4)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[4, 0, :], np.full((3), 5)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[5, 1:, :], np.full((2, 3), 5)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[5, 0, :], np.full((3), 4)))

    def testRotateUPrimeSolved(self):
        self.cube1.rotateUprime()
        for i in range(2):
            self.assertTrue(np.array_equal(
                self.cube1.stickers[i], np.full((3, 3), i)))
        for i in range(2, 4):
            self.assertTrue(np.array_equal(
                self.cube1.stickers[i, 1:, :], np.full((2, 3), i)))
            self.assertTrue(np.array_equal(
                self.cube1.stickers[i, 0, :], np.full((3), i + 2)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[4, 1:, :], np.full((2, 3), 4)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[4, 0, :], np.full((3), 3)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[5, 1:, :], np.full((2, 3), 5)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[5, 0, :], np.full((3), 2)))

    def testRotateFSolved(self):
        self.cube1.rotateF()
        self.assertTrue(np.array_equal(
            self.cube1.stickers[0, 1:, :], np.full((2, 3), 0)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[0, 0, :], np.full((3), 5)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[1, :2, :], np.full((2, 3), 1)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[1, 2, :], np.full((3), 4)))
        for i in range(2, 4):
            self.assertTrue(np.array_equal(
                self.cube1.stickers[i], np.full((3, 3), i)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[4, :, :2], np.full((3, 2), 4)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[4, :, 2], np.full((3), 0)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[5, :, 1:], np.full((3, 2), 5)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[5, :, 0], np.full((3), 1)))

    def testRotateF2Solved(self):
        self.cube1.rotateF2()
        self.assertTrue(np.array_equal(
            self.cube1.stickers[0, 1:, :], np.full((2, 3), 0)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[0, 0, :], np.full((3), 1)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[1, :2, :], np.full((2, 3), 1)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[1, 2, :], np.full((3), 0)))
        for i in range(2, 4):
            self.assertTrue(np.array_equal(
                self.cube1.stickers[i], np.full((3, 3), i)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[4, :, :2], np.full((3, 2), 4)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[4, :, 2], np.full((3), 5)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[5, :, 1:], np.full((3, 2), 5)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[5, :, 0], np.full((3), 4)))

    def testRotateFPrimeSolved(self):
        self.cube1.rotateFprime()
        self.assertTrue(np.array_equal(
            self.cube1.stickers[0, 1:, :], np.full((2, 3), 0)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[0, 0, :], np.full((3), 4)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[1, :2, :], np.full((2, 3), 1)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[1, 2, :], np.full((3), 5)))
        for i in range(2, 4):
            self.assertTrue(np.array_equal(
                self.cube1.stickers[i], np.full((3, 3), i)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[4, :, :2], np.full((3, 2), 4)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[4, :, 2], np.full((3), 1)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[5, :, 1:], np.full((3, 2), 5)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[5, :, 0], np.full((3), 0)))

    def testRotateBSolved(self):
        self.cube1.rotateB()
        self.assertTrue(np.array_equal(
            self.cube1.stickers[0, :2, :], np.full((2, 3), 0)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[0, 2, :], np.full((3), 4)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[1, 1:, :], np.full((2, 3), 1)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[1, 0, :], np.full((3), 5)))
        for i in range(2, 4):
            self.assertTrue(np.array_equal(
                self.cube1.stickers[i], np.full((3, 3), i)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[4, :, 1:], np.full((3, 2), 4)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[4, :, 0], np.full((3), 1)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[5, :, :2], np.full((3, 2), 5)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[5, :, 2], np.full((3), 0)))

    def testRotateB2Solved(self):
        self.cube1.rotateB2()
        self.assertTrue(np.array_equal(
            self.cube1.stickers[0, :2, :], np.full((2, 3), 0)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[0, 2, :], np.full((3), 1)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[1, 1:, :], np.full((2, 3), 1)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[1, 0, :], np.full((3), 0)))
        for i in range(2, 4):
            self.assertTrue(np.array_equal(
                self.cube1.stickers[i], np.full((3, 3), i)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[4, :, 1:], np.full((3, 2), 4)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[4, :, 0], np.full((3), 5)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[5, :, :2], np.full((3, 2), 5)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[5, :, 2], np.full((3), 4)))

    def testRotateBprimeSolved(self):
        self.cube1.rotateBprime()
        self.assertTrue(np.array_equal(
            self.cube1.stickers[0, :2, :], np.full((2, 3), 0)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[0, 2, :], np.full((3), 5)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[1, 1:, :], np.full((2, 3), 1)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[1, 0, :], np.full((3), 4)))
        for i in range(2, 4):
            self.assertTrue(np.array_equal(
                self.cube1.stickers[i], np.full((3, 3), i)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[4, :, 1:], np.full((3, 2), 4)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[4, :, 0], np.full((3), 0)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[5, :, :2], np.full((3, 2), 5)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[5, :, 2], np.full((3), 1)))

    def testRotateLSolved(self):
        self.cube1.rotateL()
        for i in range(0, 2):
            self.assertTrue(np.array_equal(
                self.cube1.stickers[i, :, 1:], np.full((3, 2), i)))
            self.assertTrue(np.array_equal(
                self.cube1.stickers[i, :, 0], np.full((3), i + 2)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[2, :, 1:], np.full((3, 2), 2)))
        # inverted
        self.assertTrue(np.array_equal(
            self.cube1.stickers[2, :, 0], np.full((3), 1)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[3, :, :2], np.full((3, 2), 3)))
        # inverted
        self.assertTrue(np.array_equal(
            self.cube1.stickers[3, :, 2], np.full((3), 0)))
        for i in range(4, 6):
            self.assertTrue(np.array_equal(
                self.cube1.stickers[i], np.full((3, 3), i)))

    def testRotateL2Solved(self):
        self.cube1.rotateL2()
        self.assertTrue(np.array_equal(
            self.cube1.stickers[0, :, 1:], np.full((3, 2), 0)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[0, :, 0], np.full((3), 1)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[1, :, 1:], np.full((3, 2), 1)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[1, :, 0], np.full((3), 0)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[2, :, 1:], np.full((3, 2), 2)))
        # inverted
        self.assertTrue(np.array_equal(
            self.cube1.stickers[2, :, 0], np.full((3), 3)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[3, :, :2], np.full((3, 2), 3)))
        # inverted
        self.assertTrue(np.array_equal(
            self.cube1.stickers[3, :, 2], np.full((3), 2)))
        for i in range(4, 6):
            self.assertTrue(np.array_equal(
                self.cube1.stickers[i], np.full((3, 3), i)))

    def testRotateLPrimeSolved(self):
        self.cube1.rotateLprime()
        self.assertTrue(np.array_equal(
            self.cube1.stickers[0, :, 1:], np.full((3, 2), 0)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[0, :, 0], np.full((3), 3)))    # inverted
        self.assertTrue(np.array_equal(
            self.cube1.stickers[1, :, 1:], np.full((3, 2), 1)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[1, :, 0], np.full((3), 2)))    # inverted
        self.assertTrue(np.array_equal(
            self.cube1.stickers[2, :, 1:], np.full((3, 2), 2)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[2, :, 0], np.full((3), 0)))    # inverted
        self.assertTrue(np.array_equal(
            self.cube1.stickers[3, :, :2], np.full((3, 2), 3)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[3, :, 2], np.full((3), 1)))    # inverted
        for i in range(4, 6):
            self.assertTrue(np.array_equal(
                self.cube1.stickers[i], np.full((3, 3), i)))

    def testRotateRSolved(self):
        self.cube1.rotateR()
        self.assertTrue(np.array_equal(
            self.cube1.stickers[0, :, :2], np.full((3, 2), 0)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[0, :, 2], np.full((3), 3)))    # inverted
        self.assertTrue(np.array_equal(
            self.cube1.stickers[1, :, :2], np.full((3, 2), 1)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[1, :, 2], np.full((3), 2)))    # inverted
        self.assertTrue(np.array_equal(
            self.cube1.stickers[2, :, :2], np.full((3, 2), 2)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[2, :, 2], np.full((3), 0)))    # inverted
        self.assertTrue(np.array_equal(
            self.cube1.stickers[3, :, 1:], np.full((3, 2), 3)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[3, :, 0], np.full((3), 1)))    # inverted
        for i in range(4, 6):
            self.assertTrue(np.array_equal(
                self.cube1.stickers[i], np.full((3, 3), i)))

    def testRotateR2Solved(self):
        self.cube1.rotateR2()
        self.assertTrue(np.array_equal(
            self.cube1.stickers[0, :, :2], np.full((3, 2), 0)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[0, :, 2], np.full((3), 1)))    # inverted
        self.assertTrue(np.array_equal(
            self.cube1.stickers[1, :, :2], np.full((3, 2), 1)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[1, :, 2], np.full((3), 0)))    # inverted
        self.assertTrue(np.array_equal(
            self.cube1.stickers[2, :, :2], np.full((3, 2), 2)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[2, :, 2], np.full((3), 3)))    # inverted
        self.assertTrue(np.array_equal(
            self.cube1.stickers[3, :, 1:], np.full((3, 2), 3)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[3, :, 0], np.full((3), 2)))    # inverted
        for i in range(4, 6):
            self.assertTrue(np.array_equal(
                self.cube1.stickers[i], np.full((3, 3), i)))

    def testRotateRPrimeSolved(self):
        self.cube1.rotateRprime()
        for i in range(0, 2):
            self.assertTrue(np.array_equal(
                self.cube1.stickers[i, :, :2], np.full((3, 2), i)))
            self.assertTrue(np.array_equal(
                self.cube1.stickers[i, :, 2], np.full((3), i + 2)))    # inverted
        self.assertTrue(np.array_equal(
            self.cube1.stickers[2, :, :2], np.full((3, 2), 2)))
        # inverted
        self.assertTrue(np.array_equal(
            self.cube1.stickers[2, :, 2], np.full((3), 1)))
        self.assertTrue(np.array_equal(
            self.cube1.stickers[3, :, 1:], np.full((3, 2), 3)))
        # inverted
        self.assertTrue(np.array_equal(
            self.cube1.stickers[3, :, 0], np.full((3), 0)))
        for i in range(4, 6):
            self.assertTrue(np.array_equal(
                self.cube1.stickers[i], np.full((3, 3), i)))

    def testInitUnsolved(self):
        pattern = np.array([
            [[0, 2, 0], [4, 0, 5], [0, 3, 0]],
            [[1, 3, 1], [4, 1, 5], [1, 2, 1]],
            [[2, 1, 2], [4, 2, 5], [2, 0, 2]],
            [[3, 1, 3], [5, 3, 4], [3, 0, 3]],
            [[4, 1, 4], [3, 4, 2], [4, 0, 4]],
            [[5, 1, 5], [2, 5, 3], [5, 0, 5]]
        ])
        self.assertEqual(pattern.tolist(), self.cube2.stickers.tolist())

    def testIsSolvedNotSolved(self):
        self.assertFalse(self.cube2.isSolved())

    def testRotateDUnsolved(self):
        self.cube2("D")
        expected = np.array([
            [[0, 4, 0], [3, 0, 2], [0, 5, 0]],
            [[1, 3, 1], [4, 1, 5], [1, 2, 1]],
            [[2, 1, 2], [4, 2, 5], [4, 0, 4]],
            [[3, 1, 3], [5, 3, 4], [5, 0, 5]],
            [[4, 1, 4], [3, 4, 2], [3, 0, 3]],
            [[5, 1, 5], [2, 5, 3], [2, 0, 2]]
        ])
        self.assertEqual(expected.tolist(),
                         self.cube2.stickers.astype(int).tolist())

    def testRotateD2Unsolved(self):
        self.cube2("D2")
        expected = np.array([
            [[0, 3, 0], [5, 0, 4], [0, 2, 0]],
            [[1, 3, 1], [4, 1, 5], [1, 2, 1]],
            [[2, 1, 2], [4, 2, 5], [3, 0, 3]],
            [[3, 1, 3], [5, 3, 4], [2, 0, 2]],
            [[4, 1, 4], [3, 4, 2], [5, 0, 5]],
            [[5, 1, 5], [2, 5, 3], [4, 0, 4]]
        ])
        self.assertEqual(expected.tolist(),
                         self.cube2.stickers.astype(int).tolist())

    def testRotateDPrimeUnsolved(self):
        self.cube2("D'")
        expected = np.array([
            [[0, 5, 0], [2, 0, 3], [0, 4, 0]],
            [[1, 3, 1], [4, 1, 5], [1, 2, 1]],
            [[2, 1, 2], [4, 2, 5], [5, 0, 5]],
            [[3, 1, 3], [5, 3, 4], [4, 0, 4]],
            [[4, 1, 4], [3, 4, 2], [2, 0, 2]],
            [[5, 1, 5], [2, 5, 3], [3, 0, 3]]
        ])
        self.assertEqual(expected.tolist(),
                         self.cube2.stickers.astype(int).tolist())

    def testRotateUUnsolved(self):
        self.cube2("U")
        expected = np.array([
            [[0, 2, 0], [4, 0, 5], [0, 3, 0]],
            [[1, 4, 1], [2, 1, 3], [1, 5, 1]],
            [[5, 1, 5], [4, 2, 5], [2, 0, 2]],
            [[4, 1, 4], [5, 3, 4], [3, 0, 3]],
            [[2, 1, 2], [3, 4, 2], [4, 0, 4]],
            [[3, 1, 3], [2, 5, 3], [5, 0, 5]]
        ])
        self.assertEqual(expected.tolist(),
                         self.cube2.stickers.astype(int).tolist())

    def testRotateU2Unsolved(self):
        self.cube2("U2")
        expected = np.array([
            [[0, 2, 0], [4, 0, 5], [0, 3, 0]],
            [[1, 2, 1], [5, 1, 4], [1, 3, 1]],
            [[3, 1, 3], [4, 2, 5], [2, 0, 2]],
            [[2, 1, 2], [5, 3, 4], [3, 0, 3]],
            [[5, 1, 5], [3, 4, 2], [4, 0, 4]],
            [[4, 1, 4], [2, 5, 3], [5, 0, 5]]
        ])
        self.assertEqual(expected.tolist(),
                         self.cube2.stickers.astype(int).tolist())

    def testRotateUPrimeUnsolved(self):
        self.cube2("U'")
        expected = np.array([
            [[0, 2, 0], [4, 0, 5], [0, 3, 0]],
            [[1, 5, 1], [3, 1, 2], [1, 4, 1]],
            [[4, 1, 4], [4, 2, 5], [2, 0, 2]],
            [[5, 1, 5], [5, 3, 4], [3, 0, 3]],
            [[3, 1, 3], [3, 4, 2], [4, 0, 4]],
            [[2, 1, 2], [2, 5, 3], [5, 0, 5]]
        ])
        self.assertEqual(expected.tolist(),
                         self.cube2.stickers.astype(int).tolist())

    def testRotateFUnsolved(self):
        self.cube2("F")
        expected = np.array([
            [[5, 2, 5], [4, 0, 5], [0, 3, 0]],
            [[1, 3, 1], [4, 1, 5], [4, 2, 4]],
            [[2, 4, 2], [0, 2, 1], [2, 5, 2]],
            [[3, 1, 3], [5, 3, 4], [3, 0, 3]],
            [[4, 1, 0], [3, 4, 2], [4, 0, 0]],
            [[1, 1, 5], [2, 5, 3], [1, 0, 5]]
        ])
        self.assertEqual(expected.tolist(),
                         self.cube2.stickers.astype(int).tolist()) 

    def testRotateF2Unsolved(self):
        self.cube2("F2")
        expected = np.array([
            [[1, 2, 1], [4, 0, 5], [0, 3, 0]],
            [[1, 3, 1], [4, 1, 5], [0, 2, 0]],
            [[2, 0, 2], [5, 2, 4], [2, 1, 2]],
            [[3, 1, 3], [5, 3, 4], [3, 0, 3]],
            [[4, 1, 5], [3, 4, 2], [4, 0, 5]],
            [[4, 1, 5], [2, 5, 3], [4, 0, 5]]
        ])
        self.assertEqual(expected.tolist(),
                         self.cube2.stickers.astype(int).tolist())   

    def testRotateFPrimeUnsolved(self):
        self.cube2("F'")
        expected = np.array([
            [[4, 2, 4], [4, 0, 5], [0, 3, 0]],
            [[1, 3, 1], [4, 1, 5], [5, 2, 5]],
            [[2, 5, 2], [1, 2, 0], [2, 4, 2]],
            [[3, 1, 3], [5, 3, 4], [3, 0, 3]],
            [[4, 1, 1], [3, 4, 2], [4, 0, 1]],
            [[0, 1, 5], [2, 5, 3], [0, 0, 5]]
        ])
        self.assertEqual(expected.tolist(),
                         self.cube2.stickers.astype(int).tolist()) 

    def testRotateBUnsolved(self):
        self.cube2("B")
        expected = np.array([
            [[0, 2, 0], [4, 0, 5], [4, 3, 4]],
            [[5, 3, 5], [4, 1, 5], [1, 2, 1]],
            [[2, 1, 2], [4, 2, 5], [2, 0, 2]],
            [[3, 5, 3], [0, 3, 1], [3, 4, 3]],
            [[1, 1, 4], [3, 4, 2], [1, 0, 4]],
            [[5, 1, 0], [2, 5, 3], [5, 0, 0]]
        ])
        self.assertEqual(expected.tolist(),
                         self.cube2.stickers.astype(int).tolist()) 

    def testRotateB2Unsolved(self):
        self.cube2("B2")
        expected = np.array([
            [[0, 2, 0], [4, 0, 5], [1, 3, 1]],
            [[0, 3, 0], [4, 1, 5], [1, 2, 1]],
            [[2, 1, 2], [4, 2, 5], [2, 0, 2]],
            [[3, 0, 3], [4, 3, 5], [3, 1, 3]],
            [[5, 1, 4], [3, 4, 2], [5, 0, 4]],
            [[5, 1, 4], [2, 5, 3], [5, 0, 4]]
        ])
        self.assertEqual(expected.tolist(),
                         self.cube2.stickers.astype(int).tolist()) 

    def testRotateBPrimeUnsolved(self):
        self.cube2("B'")
        expected = np.array([
            [[0, 2, 0], [4, 0, 5], [5, 3, 5]],
            [[4, 3, 4], [4, 1, 5], [1, 2, 1]],
            [[2, 1, 2], [4, 2, 5], [2, 0, 2]],
            [[3, 4, 3], [1, 3, 0], [3, 5, 3]],
            [[0, 1, 4], [3, 4, 2], [0, 0, 4]],
            [[5, 1, 1], [2, 5, 3], [5, 0, 1]]
        ])
        self.assertEqual(expected.tolist(),
                         self.cube2.stickers.astype(int).tolist()) 

    def testRotateLUnsolved(self):
        self.cube2("L")
        expected = np.array([
            [[2, 2, 0], [4, 0, 5], [2, 3, 0]],
            [[3, 3, 1], [4, 1, 5], [3, 2, 1]],
            [[1, 1, 2], [4, 2, 5], [1, 0, 2]],
            [[3, 1, 0], [5, 3, 4], [3, 0, 0]],
            [[4, 3, 4], [0, 4, 1], [4, 2, 4]],
            [[5, 1, 5], [2, 5, 3], [5, 0, 5]]
        ])
        self.assertEqual(expected.tolist(),
                         self.cube2.stickers.astype(int).tolist()) 

    def testRotateL2Unsolved(self):
        self.cube2("L2")
        expected = np.array([
            [[1, 2, 0], [4, 0, 5], [1, 3, 0]],
            [[0, 3, 1], [4, 1, 5], [0, 2, 1]],
            [[3, 1, 2], [4, 2, 5], [3, 0, 2]],
            [[3, 1, 2], [5, 3, 4], [3, 0, 2]],
            [[4, 0, 4], [2, 4, 3], [4, 1, 4]],
            [[5, 1, 5], [2, 5, 3], [5, 0, 5]]
        ])
        self.assertEqual(expected.tolist(),
                         self.cube2.stickers.astype(int).tolist())

    def testRotateLPrimeUnsolved(self):
        self.cube2("L'")
        expected = np.array([
            [[3, 2, 0], [4, 0, 5], [3, 3, 0]],
            [[2, 3, 1], [4, 1, 5], [2, 2, 1]],
            [[0, 1, 2], [4, 2, 5], [0, 0, 2]],
            [[3, 1, 1], [5, 3, 4], [3, 0, 1]],
            [[4, 2, 4], [1, 4, 0], [4, 3, 4]],
            [[5, 1, 5], [2, 5, 3], [5, 0, 5]]
        ])
        self.assertEqual(expected.tolist(),
                         self.cube2.stickers.astype(int).tolist()) 

    def testRotateRUnsolved(self):
        self.cube2("R")
        expected = np.array([
            [[0, 2, 3], [4, 0, 5], [0, 3, 3]],
            [[1, 3, 2], [4, 1, 5], [1, 2, 2]],
            [[2, 1, 0], [4, 2, 5], [2, 0, 0]],
            [[1, 1, 3], [5, 3, 4], [1, 0, 3]],
            [[4, 1, 4], [3, 4, 2], [4, 0, 4]],
            [[5, 2, 5], [0, 5, 1], [5, 3, 5]]
        ])
        self.assertEqual(expected.tolist(),
                         self.cube2.stickers.astype(int).tolist()) 

    def testRotateR2Unsolved(self):
        self.cube2("R2")
        expected = np.array([
            [[0, 2, 1], [4, 0, 5], [0, 3, 1]],
            [[1, 3, 0], [4, 1, 5], [1, 2, 0]],
            [[2, 1, 3], [4, 2, 5], [2, 0, 3]],
            [[2, 1, 3], [5, 3, 4], [2, 0, 3]],
            [[4, 1, 4], [3, 4, 2], [4, 0, 4]],
            [[5, 0, 5], [3, 5, 2], [5, 1, 5]]
        ])
        self.assertEqual(expected.tolist(),
                         self.cube2.stickers.astype(int).tolist()) 

    def testRotateRPrimeUnsolved(self):
        self.cube2("R'")
        expected = np.array([
            [[0, 2, 2], [4, 0, 5], [0, 3, 2]],
            [[1, 3, 3], [4, 1, 5], [1, 2, 3]],
            [[2, 1, 1], [4, 2, 5], [2, 0, 1]],
            [[0, 1, 3], [5, 3, 4], [0, 0, 3]],
            [[4, 1, 4], [3, 4, 2], [4, 0, 4]],
            [[5, 3, 5], [1, 5, 0], [5, 2, 5]]
        ])
        self.assertEqual(expected.tolist(),
                         self.cube2.stickers.astype(int).tolist()) 
Example #12
0
 def begin(self, color):
     point = Point(0, 0, 0)
     self.cases[point] = Cube(color, point)
Example #13
0
    Edge(vertex0, vertex4),
    Edge(vertex2, vertex1),
    Edge(vertex2, vertex3),
    Edge(vertex2, vertex7),
    Edge(vertex6, vertex3),
    Edge(vertex6, vertex4),
    Edge(vertex6, vertex7),
    Edge(vertex5, vertex1),
    Edge(vertex5, vertex4),
    Edge(vertex5, vertex7),
    Edge(Vertex(0, 0, 0), Vertex(-2, 0, 0)),
    Edge(Vertex(0, 0, 0), Vertex(0, 2, 0)),
    Edge(Vertex(0, 0, 0), Vertex(0, 0, -2)),
)

cube = Cube(edges)
camera = Camera()


def resize(width, height):
    glViewport(0, 0, width, height)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(40.0, float(width / height), 0.1, 50.0)
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()


def main():
    global but_id
    pygame.init()
Example #14
0
from model.cube import Cube
from solver.solver import Solver
import numpy as np
from utils.read_cube import read_cube_arr

if __name__ == '__main__':
  c = Cube()
  color = 'gyrgggbbb boyyybrrb obwyryooo yrgrowrgy wwwwwowwr gggobrybo'
  arr = read_cube_arr(color)
  print(arr)
  c.read_cube(arr)
  s = Solver(c)
  s.solve()
  print(s.solution)
    def __init__(self,
                 cube=None,
                 interactive=True,
                 view=(0, 0, 10),
                 fig=None,
                 rect=[0, 0.16, 1, 0.84],
                 **kwargs):
        if cube is None:
            self.cube = Cube(3)
        elif isinstance(cube, Cube):
            self.cube = cube
        else:
            self.cube = Cube(cube)

        self._trainCube = TrainCube()

        self._view = view
        self._start_rot = Quaternion.from_v_theta((1, -1, 0), -np.pi / 6)

        if fig is None:
            fig = plt.gcf()

        # disable default key press events
        callbacks = fig.canvas.callbacks.callbacks
        del callbacks['key_press_event']

        # add some defaults, and draw axes
        kwargs.update(
            dict(aspect=kwargs.get('aspect', 'equal'),
                 xlim=kwargs.get('xlim', (-2.0, 2.0)),
                 ylim=kwargs.get('ylim', (-2.0, 2.0)),
                 frameon=kwargs.get('frameon', False),
                 xticks=kwargs.get('xticks', []),
                 yticks=kwargs.get('yticks', [])))
        super(InteractiveCube, self).__init__(fig, rect, **kwargs)
        self.xaxis.set_major_formatter(plt.NullFormatter())
        self.yaxis.set_major_formatter(plt.NullFormatter())

        self._start_xlim = kwargs['xlim']
        self._start_ylim = kwargs['ylim']

        # Define movement for up/down arrows or up/down mouse movement
        self._ax_UD = (1, 0, 0)
        self._step_UD = 0.01

        # Define movement for left/right arrows or left/right mouse movement
        self._ax_LR = (0, -1, 0)
        self._step_LR = 0.01

        self._ax_LR_alt = (0, 0, 1)

        # Internal state variable
        self._active = False  # true when mouse is over axes
        self._button1 = False  # true when button 1 is pressed
        self._button2 = False  # true when button 2 is pressed
        self._event_xy = None  # store xy position of mouse event
        self._shift = False  # shift key pressed
        self._digit_flags = np.zeros(10, dtype=bool)  # digits 0-9 pressed

        self._current_rot = self._start_rot  #current rotation state
        self._face_polys = None
        self._sticker_polys = None

        self._draw_cube()

        # connect some GUI events
        self.figure.canvas.mpl_connect('button_press_event', self._mouse_press)
        self.figure.canvas.mpl_connect('button_release_event',
                                       self._mouse_release)
        self.figure.canvas.mpl_connect('motion_notify_event',
                                       self._mouse_motion)
        self.figure.canvas.mpl_connect('key_press_event', self._key_press)
        self.figure.canvas.mpl_connect('key_release_event', self._key_release)

        self._initialize_widgets()

        # write some instructions
        self.figure.text(
            0.75,
            0.925, "Use Mouse, Arrows, and Shift keys to adjust view.\n"
            "Press D, U, F, B, L, and R to turn faces clockwise.\n"
            "(Hold Shift with key to turn counter-clockwise.)",
            size=10)

        self.figure.text(0.025,
                         0.925,
                         "Rubik's Cube Solver",
                         size=30,
                         weight="heavy")
class InteractiveCube(plt.Axes):
    def __init__(self,
                 cube=None,
                 interactive=True,
                 view=(0, 0, 10),
                 fig=None,
                 rect=[0, 0.16, 1, 0.84],
                 **kwargs):
        if cube is None:
            self.cube = Cube(3)
        elif isinstance(cube, Cube):
            self.cube = cube
        else:
            self.cube = Cube(cube)

        self._trainCube = TrainCube()

        self._view = view
        self._start_rot = Quaternion.from_v_theta((1, -1, 0), -np.pi / 6)

        if fig is None:
            fig = plt.gcf()

        # disable default key press events
        callbacks = fig.canvas.callbacks.callbacks
        del callbacks['key_press_event']

        # add some defaults, and draw axes
        kwargs.update(
            dict(aspect=kwargs.get('aspect', 'equal'),
                 xlim=kwargs.get('xlim', (-2.0, 2.0)),
                 ylim=kwargs.get('ylim', (-2.0, 2.0)),
                 frameon=kwargs.get('frameon', False),
                 xticks=kwargs.get('xticks', []),
                 yticks=kwargs.get('yticks', [])))
        super(InteractiveCube, self).__init__(fig, rect, **kwargs)
        self.xaxis.set_major_formatter(plt.NullFormatter())
        self.yaxis.set_major_formatter(plt.NullFormatter())

        self._start_xlim = kwargs['xlim']
        self._start_ylim = kwargs['ylim']

        # Define movement for up/down arrows or up/down mouse movement
        self._ax_UD = (1, 0, 0)
        self._step_UD = 0.01

        # Define movement for left/right arrows or left/right mouse movement
        self._ax_LR = (0, -1, 0)
        self._step_LR = 0.01

        self._ax_LR_alt = (0, 0, 1)

        # Internal state variable
        self._active = False  # true when mouse is over axes
        self._button1 = False  # true when button 1 is pressed
        self._button2 = False  # true when button 2 is pressed
        self._event_xy = None  # store xy position of mouse event
        self._shift = False  # shift key pressed
        self._digit_flags = np.zeros(10, dtype=bool)  # digits 0-9 pressed

        self._current_rot = self._start_rot  #current rotation state
        self._face_polys = None
        self._sticker_polys = None

        self._draw_cube()

        # connect some GUI events
        self.figure.canvas.mpl_connect('button_press_event', self._mouse_press)
        self.figure.canvas.mpl_connect('button_release_event',
                                       self._mouse_release)
        self.figure.canvas.mpl_connect('motion_notify_event',
                                       self._mouse_motion)
        self.figure.canvas.mpl_connect('key_press_event', self._key_press)
        self.figure.canvas.mpl_connect('key_release_event', self._key_release)

        self._initialize_widgets()

        # write some instructions
        self.figure.text(
            0.75,
            0.925, "Use Mouse, Arrows, and Shift keys to adjust view.\n"
            "Press D, U, F, B, L, and R to turn faces clockwise.\n"
            "(Hold Shift with key to turn counter-clockwise.)",
            size=10)

        self.figure.text(0.025,
                         0.925,
                         "Rubik's Cube Solver",
                         size=30,
                         weight="heavy")

    def _initialize_widgets(self):
        self._ax_reset = self.figure.add_axes([0.1, 0.025, 0.075, 0.05])
        self._btn_reset = widgets.Button(self._ax_reset,
                                         'Reset View',
                                         color="#aaaaaa")
        self._btn_reset.on_clicked(self._reset_view)

        self._ax_solve = self.figure.add_axes([0.025, 0.025, 0.075, 0.05])
        self._btn_solve = widgets.Button(self._ax_solve,
                                         'Reset Cube',
                                         color="#aaaaaa")
        self._btn_solve.on_clicked(self._solve_cube)

        self._ax_gen = self.figure.add_axes([0.875, 0.025, 0.1, 0.05])
        self._btn_gen = widgets.Button(self._ax_gen,
                                       'Generate Solution',
                                       color="#88ff88",
                                       hovercolor="#66dd66")
        self._btn_gen.on_clicked(self._gen_solution)

        self._soln_label = self.figure.text(0.5,
                                            0.025,
                                            "",
                                            size=20,
                                            weight="bold",
                                            ha="center")

    def _project(self, pts):
        return project_points(pts, self._current_rot, self._view, [0, 1, 0])

    def _draw_cube(self):
        stickers = self._project(self.cube._stickers)[:, :, :2]
        faces = self._project(self.cube._faces)[:, :, :2]
        face_centroids = self._project(self.cube._face_centroids[:, :3])
        sticker_centroids = self._project(self.cube._sticker_centroids[:, :3])

        plastic_color = self.cube.plastic_color
        colors = np.asarray(self.cube.face_colors)[self.cube._colors]
        face_zorders = -face_centroids[:, 2]
        sticker_zorders = -sticker_centroids[:, 2]

        if self._face_polys is None:
            # initial call: create polygon objects and add to axes
            self._face_polys = []
            self._sticker_polys = []

            for i in range(len(colors)):
                fp = plt.Polygon(faces[i],
                                 facecolor=plastic_color,
                                 zorder=face_zorders[i])
                sp = plt.Polygon(stickers[i],
                                 facecolor=colors[i],
                                 zorder=sticker_zorders[i])

                self._face_polys.append(fp)
                self._sticker_polys.append(sp)
                self.add_patch(fp)
                self.add_patch(sp)
        else:
            # subsequent call: update the polygon objects
            for i in range(len(colors)):
                self._face_polys[i].set_xy(faces[i])
                self._face_polys[i].set_zorder(face_zorders[i])
                self._face_polys[i].set_facecolor(plastic_color)

                self._sticker_polys[i].set_xy(stickers[i])
                self._sticker_polys[i].set_zorder(sticker_zorders[i])
                self._sticker_polys[i].set_facecolor(colors[i])

        self.figure.canvas.draw()

    def rotate(self, rot):
        self._current_rot = self._current_rot * rot

    def rotate_face(self, face, turns=1, layer=0, steps=5):
        train_turn = face.upper() + ("'" if turns == -1 else "")
        self._trainCube(train_turn)
        if not np.allclose(turns, 0):
            for _ in range(steps):
                self.cube.rotate_face(face, turns * 1. / steps, layer=layer)
                self._draw_cube()

    def _reset_view(self, *args):
        self.set_xlim(self._start_xlim)
        self.set_ylim(self._start_ylim)
        self._current_rot = self._start_rot
        self._draw_cube()

    def _solve_cube(self, *args):
        move_list = self.cube._move_list[:]
        for (face, n, layer) in move_list[::-1]:
            self.rotate_face(face, -n, layer, steps=3)
        self.cube._move_list = []

        self._trainCube = TrainCube()

    def _gen_solution(self, *args):
        if not self._trainCube.isSolved():
            soln = predict(self._trainCube.stickers)
            self._soln_label.set_text(soln)
        elif self._soln_label.get_text() == "The cube is already solved!":
            self._soln_label.set_text("")
        else:
            self._soln_label.set_text("The cube is already solved!")

    def _key_press(self, event):
        """Handler for key press events"""
        if event.key == 'shift':
            self._shift = True
        elif event.key.isdigit():
            self._digit_flags[int(event.key)] = 1
        elif event.key == 'right':
            if self._shift:
                ax_LR = self._ax_LR_alt
            else:
                ax_LR = self._ax_LR
            self.rotate(Quaternion.from_v_theta(ax_LR, 5 * self._step_LR))
        elif event.key == 'left':
            if self._shift:
                ax_LR = self._ax_LR_alt
            else:
                ax_LR = self._ax_LR
            self.rotate(Quaternion.from_v_theta(ax_LR, -5 * self._step_LR))
        elif event.key == 'up':
            self.rotate(Quaternion.from_v_theta(self._ax_UD,
                                                5 * self._step_UD))
        elif event.key == 'down':
            self.rotate(
                Quaternion.from_v_theta(self._ax_UD, -5 * self._step_UD))
        elif event.key.upper() in 'LRUDBF':
            if self._shift:
                direction = -1
            else:
                direction = 1

            if np.any(self._digit_flags[:N]):
                for d in np.arange(N)[self._digit_flags[:N]]:
                    self.rotate_face(event.key.upper(), direction, layer=d)
            else:
                self.rotate_face(event.key.upper(), direction)

        self._draw_cube()

    def _key_release(self, event):
        """Handler for key release event"""
        if event.key == 'shift':
            self._shift = False
        elif event.key.isdigit():
            self._digit_flags[int(event.key)] = 0

    def _mouse_press(self, event):
        """Handler for mouse button press"""
        self._event_xy = (event.x, event.y)
        if event.button == 1:
            self._button1 = True
        elif event.button == 3:
            self._button2 = True

    def _mouse_release(self, event):
        """Handler for mouse button release"""
        self._event_xy = None
        if event.button == 1:
            self._button1 = False
        elif event.button == 3:
            self._button2 = False

    def _mouse_motion(self, event):
        """Handler for mouse motion"""
        if self._button1 or self._button2:
            dx = event.x - self._event_xy[0]
            dy = event.y - self._event_xy[1]
            self._event_xy = (event.x, event.y)

            if self._button1:
                if self._shift:
                    ax_LR = self._ax_LR_alt
                else:
                    ax_LR = self._ax_LR
                rot1 = Quaternion.from_v_theta(self._ax_UD, self._step_UD * dy)
                rot2 = Quaternion.from_v_theta(ax_LR, self._step_LR * dx)
                self.rotate(rot1 * rot2)

                self._draw_cube()

            if self._button2:
                factor = 1 - 0.003 * (dx + dy)
                xlim = self.get_xlim()
                ylim = self.get_ylim()
                self.set_xlim(factor * xlim[0], factor * xlim[1])
                self.set_ylim(factor * ylim[0], factor * ylim[1])

                self.figure.canvas.draw()