# How would I check if a Rubik's Cube is complete or not?

I need to know if the Rubik’s Cube is 100% complete. I researched it, and I wasn’t able to find any solutions.
I tried calling a function at the end of every rotation that loops through all the pieces and checking if Position is equal to the piece’s attribute, Correct Position. I also check if the orientation is equal to 0,0,0. However, this does not work all the time because if the cube is 100% complete, I could turn the cube 90 degrees, changing all the piece’s positions and orientations. If I turn a completed cube, the cube is still complete, but the game doesn’t know since all the positions are not equal to the right positions. here’s the function that I call to check if it’s complete:

``````local function checkCompleted()
local piecesCompleted = 0
local completed = false
for i, v in ipairs(cube:GetChildren()) do
if v.Position == v:GetAttribute("CorrectPosition") and v.Orientation == Vector3.new(0,0,0) then
piecesCompleted += 1
end
end
if piecesCompleted == 26 then
completed = true
end
return completed
end
``````

Have all blocks facing the same direction at the start so they have the exact same CFrame.LookVector at the start (this assumes the blocks actually rotate) You need to consider at least 2 of the directions though, as it’s possible for them to have the same lookVector with the wrong right vector or something. Then just check if all blocks are facing the same direction. If they are it’s solved. If not then it’s not solved.

``````local function checkCompleted()
local firstBlock = cube:GetChildren()[1]
local direction = {
LookVector = firstBlock.LookVector,
RightVector = firstBlock.RightVector,
}
for _, v in ipairs(cube:GetChildren()) do
if not (v.CFrame.LookVector == direction.LookVector and v.CFrame.RightVector == direction.RightVector) then
return false
end
end
return true
end
``````
1 Like