B4A Library RubikCube - See post #6 of this thread

EDIT 21/03/2022: Go to post #6 for a project that can also automatically solve a scrambled cube

A "partial" wrap for this Github project. Extract and copy the library files (jars and xml) to your additional library folder (B4A).

Attached:
Java code - change it to your liking
B4A sample project
B4A libs - extract and copy to your additional libs folder.

LibRes (attached) should be on the same folder level as that of the B4A /Files and /Objects folder.

1.png


2.png


3.png


Read/study this post to understand the manner that commands should be send to the lib to execute the commands.
See the commands hard coded in the EditText box - study and change it

Rotate the entire cube by touching the view
Rotate a single layer inside the cube - use your finger to do such.

Sure you will figure it out.

Sample code:

B4A sample code:
#Region  Project Attributes
    #ApplicationLabel: b4aRubikCube
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#AdditionalRes: ..\LibRes

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Private rc1 As RubikCube
 
    Private Button1 As Button
    Dim move As String
    Private et1 As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
 
    'using smart string literals for the move commands
    rc1.Editable = True                   'set to true to rotate planes manually
    rc1.BackFaceDistance = 4              'set the distance of the back faces from the cube -> a value (0) zero -> no back faces are shown
    et1.Text = $"R2' U3 M2 U3' R3' U2 M1' U1'"$
    rc1.DoubleRotationSpeed = 25
    rc1.SingleRotationSpeed = 25
 
 
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub



Private Sub Button1_Click
 
    move = et1.Text                            'get the move sequence from the EditText box
    rc1.MoveSequence = move                    'pass the move sequence to be performed
    rc1.AnimateMoveSequence                    'execute the move as defined above
    Wait For rc1_animation_finished            'with for animation to finish
    Log("now going to do move sequence in reverse")       
    rc1.AnimateMoveSequenceReversed            'do the same move sequence but in reverse order

 
End Sub

Private Sub rc1_animation_finished
    Log("gotcha")
    rc1.CubeModel = "000000000111111111222222222333333333444444444555555555"    'define a model to set when animation is completed
    rc1.ResetToInitialState                                                     'set the cube state as defined in rc1.CubeModel = "012345012012345012012345012012345012012345012012345012"
End Sub

Private Sub rc1_cube_model(model As String)
    Log("in rc1_cube_model")
    Log(model)
End Sub
 

Attachments

  • b4aLibFiles.zip
    66.4 KB · Views: 148
  • b4aRubikCube.zip
    10 KB · Views: 158
  • JavaCode.zip
    45.6 KB · Views: 152
  • LibRes.zip
    804 bytes · Views: 164
Last edited:

udg

Expert
Licensed User
Longtime User
👍
If the solver could show the sequence of moves along with a slow (or somehow controlled) animation..
I see it as a learning tool, similar to the explanations spread over the Internet, but graphical, step-by-step and easy to use :)
 

Johan Schoeman

Expert
Licensed User
Longtime User
👍
If the solver could show the sequence of moves along with a slow (or somehow controlled) animation..
I see it as a learning tool, similar to the explanations spread over the Internet, but graphical, step-by-step and easy to use :)
I am sure we will figure it out one way or another - need a bit of time to figure out the code and how to wrap it.
 

Johan Schoeman

Expert
Licensed User
Longtime User
I am sure we will figure it out one way or another - need a bit of time to figure out the code and how to wrap it.
The mapping of the sides of the cube model to the sides of what the solver expects is different from one another. So, busy writing some code to map the cube model to what the solver model expects. Tedious coding....faces of the cube and the solver do not align with each other.
 

Johan Schoeman

Expert
Licensed User
Longtime User
Here it is - with the ability to solve the Rubik Cube. Attached the following:
1. b4aLibFiles.zip - extract the jars and xml and copy them to your additional library folder
2. LibRes.zip - extract folder LibRes and copy the folder with its contents to be on the same folder level as that of the /Files and /Objects folders of the B4A project
3. b4aRubikCube.zip - the B4A project (unzip it)

Take note of the following:
1. you can add moves into the EditText box and then click on button "Scramble" to scramble the cube
2. You can also use your finger(s) to scramble the cube.
3. Once satisfied with the scramble, click on button "Solve" - it will solve the cube.

It sometimes look as if the solver has come to a standstill before the cube has been solved. Just leave it - it is busy THINKING very hard for the next move(s) to solve the cube. It will eventually solve the cube.


I have combined this Github project with this Github project to display and solve the cube. You will notice in Private Sub rc1_animation_finished that I have added some code to map the cube surfaces between the two Github projects as their mapping are different to one another.

Enjoy! 🇿🇦

B4A code:
#Region  Project Attributes
    #ApplicationLabel: b4aRubikCube
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#AdditionalRes: ..\LibRes

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Private rc1 As RubikCube
 
    Private Button1 As Button
    Dim move As String
    Private et1 As EditText
    Private Button2 As Button
 
    Dim mystring As String
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    mystring = ""
 
    'using smart string literals for the move commands
    rc1.Editable = True                   'set to true to rotate planes manually
    rc1.BackFaceDistance = 4              'set the distance of the back faces from the cube -> a value (0) zero -> no back faces are shown
    et1.Text = $"R2' U3 M2 U3' R3' U2 M1' U1'"$
    rc1.DoubleRotationSpeed = 25
    rc1.SingleRotationSpeed = 25
 
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub



Private Sub Button1_Click
 
    move = et1.Text                            'get the move sequence from the EditText box
    rc1.MoveSequence = move                    'pass the move sequence to be performed
    rc1.AnimateMoveSequence                    'execute the move as defined above
    Wait For rc1_animation_finished            'with for animation to finish

End Sub

Private Sub rc1_animation_finished
    Log("GOTCHA GOTCHA")
'    rc1.CubeModel = "000000000111111111222222222333333333444444444555555555"    'define a model to set when animation is completed
'    rc1.ResetToInitialState                                                     'set the cube state as defined in rc1.CubeModel = "012345012012345012012345012012345012012345012012345012"
End Sub

Private Sub rc1_cube_model(model As String)
    '            TOP          RIGHT           FRONT         BOTTOM        LEFT          BACK
    '        DUU;BUL;DBF - RBF;RRU;LLL -  BRD;FFF;BLU - RDB;FDF;DRF - RUL;BLU;FDU - RRB;LBD;UDL

    '            0 = top     1 = bottom     2 = front      3 = back      4 = left      5 = right
    '         132;304;100 - 521;115;322 -  323;524;120 - 540;531;314 - 405;043;012 - 554;354;204
    '        DBF;BUL;DUU - RFD;DDR;BFF -  BFB;RFL;DFU - RLU;RBD;BDL - LUR;ULB;UDF - RRL;BRL;FUL

    '        <item>#ffffff</item> - White  - 0 - U
    '        <item>#ffff00</item> - Yellow - 1 - D
    '        <item>#ff6020</item> - Orange - 2 - F
    '        <item>#d00000</item> - Red    - 3 - B
    '        <item>#2040d0</item> - Blue   - 4 - L
    '        <item>#009000</item> - Green  - 5 - R
 
 
'    /** prepare scrambledCube As
'    *
'    *             |************|
'    *             |*U1**U2**U3*|
'    *             |************|
'    *             |*U4**U5**U6*|
'    *             |************|
'    *             |*U7**U8**U9*|
'    *             |************|
'    * ************|************|************|************|
'    * *L1**L2**L3*|*F1**F2**F3*|*R1**R2**R3*|*B1**B2**B3*|
'    * ************|************|************|************|
'    * *L4**L5**L6*|*F4**F5**F6*|*R4**R5**R6*|*B4**B5**B6*|
'    * ************|************|************|************|
'    * *L7**L8**L9*|*F7**F8**F9*|*R7**R8**R9*|*B7**B8**B9*|
'    * ************|************|************|************|
'    *             |************|
'    *             |*D1**D2**D3*|
'    *             |************|
'    *             |*D4**D5**D6*|
'    *             |************|
'    *             |*D7**D8**D9*|
'    *             |************|
'    *
'    * -> U1 U2 ... U9 R1 ... R9 F1 ... F9 D1 ... D9 L1 ... L9 B1 ... B9
'    */
 
    'MAP THE CUBE LAYOUT TO THE WHAT THE SOLVER EXPECTS THE LAYOUT TO BE
    mystring = model.SubString2(6,9) & model.SubString2(3,6) & model.SubString2(0,3) & _                               'top 1, top 2, top 3
                             model.SubString2(45,46) & model.SubString2(48,49) & model.SubString2(51,52) & _           'right 1
                             model.SubString2(46,47) & model.SubString2(49,50) & model.SubString2(52,53) & _           'right 2
                             model.SubString2(47,48) & model.SubString2(50,51) & model.SubString2(53,54) & _           'right 3
                             model.SubString2(18,19) & model.SubString2(21,22) & model.SubString2(24,25) & _           'front 1
                             model.SubString2(19,20) & model.SubString2(22,23) & model.SubString2(25,26) & _           'front 2
                             model.SubString2(20,21) & model.SubString2(23,24) & model.SubString2(26,27) & _           'front 3
                             model.SubString2(9,10) & model.SubString2(12,13) & model.SubString2(15,16) & _            'bottom 1
                             model.SubString2(10,11) & model.SubString2(13,14) & model.SubString2(16,17) & _           'bottom 2
                             model.SubString2(11,12) & model.SubString2(14,15) & model.SubString2(17,18) & _           'bottom 3    
                             model.SubString2(38,39) & model.SubString2(37,38) & model.SubString2(36,37) & _           'left 1
                             model.SubString2(41,42) & model.SubString2(40,41) & model.SubString2(39,40) & _           'left 2
                             model.SubString2(44,45) & model.SubString2(43,44) & model.SubString2(42,43) & _           'left 3
                             model.SubString2(27,28) & model.SubString2(30,31) & model.SubString2(33,34) & _           'back 1
                             model.SubString2(28,29) & model.SubString2(31,32) & model.SubString2(34,35) & _           'back 2
                             model.SubString2(29,30) & model.SubString2(32,33) & model.SubString2(35,36)               'back 3                                                                                    
                         
    mystring = mystring.Replace("0", "U")
    mystring = mystring.Replace("1", "D")
    mystring = mystring.Replace("2", "F")
    mystring = mystring.Replace("3", "B")
    mystring = mystring.Replace("4", "L")
    mystring = mystring.Replace("5", "R")

'   Log("MYSTRING = " & mystring)

'    Log("in rc1_cube_model")
    Log(model)
'    Log("LENGTH = " & model.Length)
 
 
End Sub

Private Sub Button2_Click
 
    Do Until mystring.Contains("000000000") And mystring.Contains("111111111") And mystring.Contains("222222222") & _
             mystring.Contains("333333333") And mystring.Contains("444444444") And mystring.Contains("555555555")
        rc1.SimpleSolve = mystring
        Wait For rc1_animation_finished
    Loop
    ToastMessageShow("DONE!", False)

End Sub

4.png
 

Attachments

  • b4aLibFiles.zip
    91.3 KB · Views: 143
  • b4aRubikCube.zip
    11.3 KB · Views: 154
  • LibRes.zip
    804 bytes · Views: 149
Last edited:
Now need to add this to the library to allow for auto solving of a scrambled cube.....
Hello Johan

I'm new to the forum and I'm trying to send you a message about the QrCode reader. However, in the forums on this topic, I don't see any button to submit a question.

I also didn't understand how to send a message directly to a member.

Can you help me?
 

ilan

Expert
Licensed User
Longtime User
this is really cool. i was looking for a 2x2 rubik cube solver. can i use your app also for 2x2 cube?

thanx
 

Johan Schoeman

Expert
Licensed User
Longtime User
this is really cool. i was looking for a 2x2 rubik cube solver. can i use your app also for 2x2 cube?

thanx
I dont think so @ilan. Both models (Github projects) are geared for 3x3x3. Will send you the java code if you would like to give it a go.
 

ilan

Expert
Licensed User
Longtime User
I dont think so @ilan. Both models (Github projects) are geared for 3x3x3. Will send you the java code if you would like to give it a go.
Thanx @Johan Schoeman, i found also 2x2 sources in github but have difficulties to port them to b4x. Some are written in phyton some in c and java and i am struggling to create a b4x class from them.

will keep searching.
 
Top