Project Cube Solver #03 – Scrambles generating– tests and first lines of code

Project Cube Solver #03 – Scrambles generating– tests and first lines of code

In my previous article i described theory which is necessary to write generator of scrambling algorithms. Now it’s time to begin first lines of code.
If you didn’t read mentioned theory, it will be the best option if you do it before because it will help you to understand code which will be written today.

Changes made today you can view at this link:
My changes at github

Article is also available in Polish version.

Creating of first necessary classes

We will begin from creating enum which will be responsible for storing all possible moves on cube such:
R, R2, R', L, L2, L', F, F2, F', B, B2, B', U, U2, U', D, D2, D'

Code responsible for that is listed below:

Next, we will create interface for implementation Scramble class. Three methods will be needed.

  • addMove – adds new move to scramble,
  • getMove – gets move by id from scramble
  • getLength – gets length of scramble

And this is code of our interface:

Next necessary interface will be responsible for generator of scrambling algorithms. It will have one method generate, and it will return scrambling algorithm with length acoording to input number.

Basic functionality of generator

Our next step is writing a simple test which checks if generated algorithm has expected length passed as parameter to method. Additionally our test checks if object has been filled by instances of Move enum. I will use parameterized test which makes that the same test will run multiply for various data given by method getScrambleLengthProvider.

Now by running our test we will receive failures which says that ScrambleGenerator class doesn’t exist. It’s necessary to create one and implement generating method. So we do it and code responsible for that you can see listed below. I think it’s nothing to explain here. It creates object of Scramble class and fills it randomly by moves from Move enum.

We wrote basic functionality of generator. But it’s not a finish yet. If you’ve read previous aritcle you know that we need to consider few cases which shouldn’t happen. In current implementation it’s possible to draw the same cube face several times in the row or alternately moving the cube faces which are parallel each other.

Improving the algorithm to avoid shortening movements

It’s time to take care of writing functionality allows avoiding the draw moves that can be easily shortened. So we start again from the test. And here it is:

Tests of course will begin to glow red again. So we have to fix them. In our test we assume that method isPossibleNextMove exists for Scramble class and its interface. And additionally Move enum should support groups ids and faces ids. We are adding these functionalities first.

Tests have begun to pass again but it’s not the end because the generator is still not working properly. Thus we are creating another test for generator which checks if generated algorithm is consistent with requirements.

Method assertFaceNotDoubled checks if cube face is not drawn again. In contrast, the method assertGroupNotDoubled checks if we don’t move alternately parallel faces.
Tests of course, again, will not pass and our next step is to make that they were green again.

First, we are adding toString for Scramble class, which will display generated algorithm in readable form. It’s used in assertions to return drawn algorithm and place that causes error.

Next we are changing implementation of generate method from our generator:

The only change is the addition conditional instruction in lines 21 and 23 which use our new method from Scramble class. Now, running our tests we should see that all of them glow green again and we know that generator works as wanted.

Summary

Today we have created our first code. It was also pushed to github where you can see it in comfortable way. It is available at the link:
My changes at github

What next?

In the next post we will know theory of cube metrics that are use to measure movements on the cube. It will be necessary in order to write the next classes which will calculate length of scrambling algorithms and solutions for scrambled cube.

Leave a Reply

Your email address will not be published. Required fields are marked *