loading...

cube solver

about

this is a Rubik's cube solver written in pyra , and compiled to GDScript for Godot . here's the code for the main solving algorithm , which was directly modeled after the beginner's method :

~ up:    y y y y y y y y y
~ front: o o o o o o o o o
~ left:  g g g g g g g g g
~ back:  r r r r r r r r r
~ right: b b b b b b b b b
~ down:  w w w w w w w w w


* solve the cube
  - sides checked: 0
  - form a cross on the bottom
  - position the white corners
  - position the middle layer
  - form a cross on the top
  - position the yellow edge pieces
  - position the yellow corner pieces
  - orient the yellow corner pieces
  - play the victory animation


* form a cross on the bottom
* sides checked: 4
  - sides checked: 0

| form a cross on the bottom
| sides checked: Amount
  - increment [sides checked]
  - check top edge
  - check middle edge
  - check bottom edge
  ~ algorithm y

* check top edge
\ up: _ _ _
      _ _ _
      _ w _
  - drop the edge into place

* check top edge
\ front: _ w _
         _ _ _
         _ _ _
  ~ algorithm u ' f r ' f ' r
  - drop the edge into place

* check top edge

* check middle edge
\ front: _ _ _
         _ _ w
         _ _ _
  ~ algorithm r u r '
  - drop the edge into place

* check middle edge
\ right: _ _ _
         w _ _
         _ _ _
  ~ algorithm f ' u ' f u
  - drop the edge into place

* check middle edge

* check bottom edge
\ front: _ _ _
         _ _ _
         _ w _
  ~ algorithm f ' r u r '
  - drop the edge into place

* check bottom edge
\ front: _ _ _
         _ A _
         _ A _
\ down:  _ w _
         _ w _
         _ _ _

* check bottom edge
\ front: _ _ _
         _ A _
         _ B _
\ down:  _ w _
         _ w _
         _ _ _
  ~ algorithm f f
  - drop the edge into place

* check bottom edge


* drop the edge into place
  - check if it matches the center

* check if it matches the center
* sides checked: Amount
\ front: _ A _
         _ A _
         _ _ _
  ~ algorithm f f
  - sides checked: 0

* check if it matches the center
  ~ algorithm y u '
  - drop the edge into place


* position the white corners
* sides checked: 4
  - sides checked: 0

| position the white corners
| sides checked: Amount
  - increment [sides checked]
  - check top right corner
  - check bottom right corner
  ~ algorithm y

* check top right corner
\ front: _ _ w
         _ _ _
         _ _ _
  ~ algorithm f ' u u f u
  - drop the corner into place

* check top right corner
\ right: w _ _
         _ _ _
         _ _ _
  - drop the corner into place

* check top right corner
\ up: _ _ _
      _ _ _
      _ _ w
  ~ algorithm r u u r ' u '
  - drop the corner into place

* check top right corner

* check bottom right corner
\ front: _ _ _
         _ A _
         _ A A
\ right: _ _ _
         _ B _
         B B _
\ down:  _ w w
         w w w
         _ w _

* check bottom right corner
\ front: _ _ _
         _ _ _
         _ _ w
  ~ algorithm r u r ' u ' r u u r ' u '
  - drop the corner into place

* check bottom right corner
\ right: _ _ _
         _ _ _
         w _ _
  ~ algorithm r u r ' u '
  - drop the corner into place

* check bottom right corner
\ down: _ _ w
        _ _ _
        _ _ _
  ~ algorithm f ' u ' f u
  - drop the corner into place

* check bottom right corner


* drop the corner into place
  - check if it matches the centers

* check if it matches the centers
* sides checked: Amount
\ front: _ _ A
         _ A _
         _ _ _
  ~ algorithm r u r '
  - sides checked: 0

* check if it matches the centers
  ~ algorithm y u '
  - drop the corner into place


* position the middle layer
* sides checked: 4
  - sides checked: 0

| position the middle layer
| sides checked: Amount
  - increment [sides checked]
  - check the top edge
  - check the right edge
  ~ algorithm y

* check the top edge
\ front: _ y _
         _ _ _
         _ _ _

* check the top edge
\ up: _ _ _
      _ y _
      _ y _

* check the top edge
  - put the edge in place

* check the right edge
\ front: _ _ _
         _ _ y
         _ _ _

* check the right edge
\ right: _ _ _
         y _ _
         _ _ _

* check the right edge
\ front: _ _ _
         _ A A
         _ _ _
\ right: _ _ _
         B B _
         _ _ _

* check the right edge
  ~ algorithm r u r ' u ' f ' u ' f u u
  - put the edge in place


* put the edge in place
  - check if it's lined up

* check if it's lined up
* sides checked: Amount
\ front: _ A _
         _ A _
         _ _ _
\ up:    _ _ _
         _ _ _
         _ B _
\ left:  _ _ _
         _ B _
         _ _ _
  ~ algorithm u ' l ' u l u f u ' f '
  - sides checked: 0

* check if it's lined up
* sides checked: Amount
\ front: _ A _
         _ A _
         _ _ _
  ~ algorithm u r u ' r ' u ' f ' u f
  - sides checked: 0

* check if it's lined up
  ~ algorithm y u '
  - put the edge in place


* form a cross on the top
| sides checked: 4
  - cycle top edge orientation
  ~ algorithm u u
  - cycle top edge orientation
  - cycle top edge orientation

* form a cross on the top
* sides checked: Amount
\ up: _ y _
      y y y
      _ y _
  - sides checked: 0

* form a cross on the top
\ up: _ y _
      y y _
      _ _ _
  - cycle top edge orientation
  - cycle top edge orientation

* form a cross on the top
\ up: _ _ _
      y y y
      _ _ _
  - cycle top edge orientation

| form a cross on the top
| sides checked: Amount
  - increment [sides checked]
  ~ algorithm u

* cycle top edge orientation
* sides checked: Amount
  ~ algorithm f r u r ' u ' f '
  - sides checked: 0


* position the yellow edge pieces
* sides checked: Amount
\ left:  _ A _
         _ A _
         _ _ _
\ front: _ B _
         _ B _
         _ _ _
\ right: _ C _
         _ C _
         _ _ _
  - sides checked: 0

* position the yellow edge pieces
* sides checked: Amount
\ right: _ A _
         _ A _
         _ _ _
\ back:  _ B _
         _ B _
         _ _ _
  ~ algorithm r u r ' u r u u r ' u
  - sides checked: 0

| position the yellow edge pieces
* sides checked: Amount
\ right: _ A _
         _ A _
         _ _ _
\ left:  _ B _
         _ B _
         _ _ _
  ~ algorithm r u r ' u r u u r ' u
  - sides checked: 0

| position the yellow edge pieces
* sides checked: 4
  ~ algorithm y
  - sides checked: 0

| position the yellow edge pieces
| sides checked: Amount
  - increment [sides checked]
  ~ algorithm u


* position the yellow corner pieces
* top right corner matches:
  - position the remaining three corners

| position the yellow corner pieces
* top right corner doesn't match:
  ~ algorithm y

| position the yellow corner pieces
* sides checked: 4
  - cycle the corners
  - sides checked: 0

| position the yellow corner pieces
| sides checked: Amount
  - increment [sides checked]
  - check the top right corner

* position the remaining three corners
* top right corner matches:
* sides checked: Amount
  - sides checked: 0

| position the remaining three corners
* top right corner doesn't match:
  - cycle the corners

| position the remaining three corners
  ~ algorithm y '
  - check the top right corner
  ~ algorithm y

* check the top right corner
\ front: _ A A
         _ _ _
         _ _ _
\ right: B B _
         _ _ _
         _ _ _
  - top right corner matches:

* check the top right corner
\ up:    _ y _
         y y y
         _ y B
\ front: _ A y
         _ _ _
         _ _ _
\ right: A B _
         _ _ _
         _ _ _
  - top right corner matches:

* check the top right corner
\ up:    _ y _
         y y y
         _ y A
\ front: _ A B
         _ _ _
         _ _ _
\ right: y B _
         _ _ _
         _ _ _
  - top right corner matches:

* check the top right corner
  - top right corner doesn't match:

* cycle the corners
  ~ algorithm u r u ' l ' u r ' u ' l


* orient the yellow corner pieces
* sides checked: 4

| orient the yellow corner pieces
| sides checked: Amount
  - increment [sides checked]
  - orient the corner

* orient the corner
\ front: _ A A
         _ _ _
         _ _ _
\ right: B B _
         _ _ _
         _ _ _
  ~ algorithm u

| orient the corner
  ~ algorithm r ' d ' r d


* play the victory animation
  - animations: victory

pyra is particularly nice for this problem because you can pattern match directly on each cube face .

the full Godot project files can be found at pyra/examples/cube_solver ( for now , you must have lua installed for the project to run ) . a pyra plugin for Godot is planned , to simplify the compilation workflow !

← return home