Hello, I'm writing a rubiks cube solving program, and I would like to condense all of the turn definitions into functions like rightTurn();
I'm using Windows XP and Code::Blocks.

An example of a turn definition is :

cout << "Executing bottom turn: " << endl;

a = front[6];
b = front[7];
c = front[8];

front[6] = left[0];
front[7] = left[3];
front[8] = left[6];

left[0] = back[2];
left[3] = back[1];
left[6] = back[0];

back[0] = right[2];
back[1] = right[5];
back[2] = right[8];

right[2] = c;
right[5] = b;
right[8] = a;

aa = bottom[0];
bb = bottom[1];


bottom[0] = bottom[6];
bottom[6] = bottom[8];
bottom[8] = bottom[2];
bottom[2] = aa;
bottom[1] = bottom[3];
bottom[3] = bottom[7];
bottom[7] = bottom[5];
bottom[5] = bb;
bottom[4] = bottom[4];

This is basically telling the program which squares replace which after a certeain turn. So can anyone please tell me how to condense this into functions?

Thank you

Recommended Answers

All 8 Replies

If you want to manually refactor, I like to start with a simple function call and cut the code into a new function with that name:

void BottomTurn()
{
    a = front[6];
    b = front[7];
    c = front[8];

    front[6] = left[0];
    front[7] = left[3];
    front[8] = left[6];

    left[0] = back[2];
    left[3] = back[1];
    left[6] = back[0];

    back[0] = right[2];
    back[1] = right[5];
    back[2] = right[8];

    right[2] = c;
    right[5] = b;
    right[8] = a;

    aa = bottom[0];
    bb = bottom[1];

    bottom[0] = bottom[6];
    bottom[6] = bottom[8];
    bottom[8] = bottom[2];
    bottom[2] = aa;
    bottom[1] = bottom[3];
    bottom[3] = bottom[7];
    bottom[7] = bottom[5];
    bottom[5] = bb;
    bottom[4] = bottom[4];
}

The code almost never compiles like that because there are dependencies that need to be passed to the function, like the front, left, back, right, and bottom arrays. Variables that are only used for that operation become local, like a, b, c, aa, and bb:

template <typename T>
void BottomTurn(T front[], T left[], T back[], T right[], T bottom[])
{
    T a = front[6];
    T b = front[7];
    T c = front[8];

    front[6] = left[0];
    front[7] = left[3];
    front[8] = left[6];

    left[0] = back[2];
    left[3] = back[1];
    left[6] = back[0];

    back[0] = right[2];
    back[1] = right[5];
    back[2] = right[8];

    right[2] = c;
    right[5] = b;
    right[8] = a;

    T aa = bottom[0];
    T bb = bottom[1];

    bottom[0] = bottom[6];
    bottom[6] = bottom[8];
    bottom[8] = bottom[2];
    bottom[2] = aa;
    bottom[1] = bottom[3];
    bottom[3] = bottom[7];
    bottom[7] = bottom[5];
    bottom[5] = bb;
    bottom[4] = bottom[4];
}

At this point the code usually compiles, but could stand for some cleaning up. :) Most of the time I start objectifying the parameters so there are fewer and the operations on them are easier. Maybe something like this:

void BottomTurn(RubiksCube& cube)
{
    Row oldFront = cube.Front();
    cube.Front() = cube.Left();
    cube.Left() = cube.Back();
    cube.Back() = cube.Right();
    cube.Right() = oldFront;
    cube.Bottom().Rotate90();
}

But when I do this, it is easier to see better ways of solving the problem. The RubiksCube object should support this as a method and the need for a separate function goes away:

cout << "Executing bottom turn: " << endl;
cube.BottomTurn();

And that is called abstraction. ;)

wow, thanks a lot. :) .

I'm a beggining programmer (As you could probably tell) so allthat kinda blew my mind but thanks anyways

wow, thanks a lot. :) .

I'm a beggining programmer (As you could probably tell) so allthat kinda blew my mind but thanks anyways

Good explanation by Tom Gunn. Some of it may not make much sense if you haven't run across templates or classes or structures. We don't know what the types are in the arrays, so he kind of had to use a template (not to say that using a template would be a bad idea even if one did know the types).

But the overall idea is a good idea. Cut the code verbatim from your main function and then pass it to a function. Not knowing what a, b, c, aa, and bb represent, or their scope/where they were defined, but presuming they are temporary variables, you'd redeclare them in the function.

As a shorter example, here's a simple swap.

int main ()
{
    int a[2];
    a[0] = 5;
    a[1] = 9;
    int temp = a[0];
    a[0] = a[1];
    a[1] = temp;
    return 0;
}

turns into:

void swap (int a[])
{
    int temp = a[0];
    a[0] = a[1];
    a[1] = temp;
}

int main ()
{
    int a[2];
    a[0] = 5;
    a[1] = 9;
    swap (a);
    return 0;
}

Cut and paste, put a function call in main where the original code was.

Thanks, and sorry about the lack of information. Top, Front, Back etc.. are all strings that hold the layout of the cube. a,b,c,aa, and bb are ints to hold parts of the cube. The thing I was confused about is:
I know how to make simple text functions like:

void (instructions)

then define the function at the end of the code, but when I tried it with my code, it gave me errors, and I assumed that they had something to do with not defining the return values like instead of: void instuctions: string instructions? Thanks

Thanks, and sorry about the lack of information. Top, Front, Back etc.. are all strings that hold the layout of the cube. a,b,c,aa, and bb are ints to hold parts of the cube. The thing I was confused about is:
I know how to make simple text functions like:

void (instructions)

then define the function at the end of the code, but when I tried it with my code, it gave me errors, and I assumed that they had something to do with not defining the return values like instead of: void instuctions: string instructions? Thanks

void (instructions)

seems like a strange cross between a function prototype and a function call, with something missing. Regardless it is an error. I don't know what this is supposed to be:

void instuctions: string instructions

I'd have to see more code, plus a description of what you're trying to do, to suggest a fix.

What I'm trying to do: In the simplest way possible, make this:

a = front[6];
b = front[7];
c = front[8];

front[6] = left[0];
front[7] = left[3];
front[8] = left[6];

left[0] = back[2];
left[3] = back[1];
left[6] = back[0];

back[0] = right[2];
back[1] = right[5];
back[2] = right[8];

right[2] = c;
right[5] = b;
right[8] = a;

aa = bottom[0];
bb = bottom[1];


bottom[0] = bottom[6];
bottom[6] = bottom[8];
bottom[8] = bottom[2];
bottom[2] = aa;
bottom[1] = bottom[3];
bottom[3] = bottom[7];
bottom[7] = bottom[5];
bottom[5] = bb;
bottom[4] = bottom[4];

Into this :

bottomTurn();

Thanks, and sorry about the lack of information. Top, Front, Back etc.. are all strings that hold the layout of the cube. a,b,c,aa, and bb are ints to hold parts of the cube. The thing I was confused about is:
I know how to make simple text functions like:

void (instructions)

then define the function at the end of the code, but when I tried it with my code, it gave me errors, and I assumed that they had something to do with not defining the return values like instead of: void instuctions: string instructions? Thanks

You didn't post any variables with names Top, Front, Back earlier. The question/issue is whether a, b, c, aa, and bb hold anything useful or are just temporary variables (everything in your code suggests that they are just temporary variables and that they are the same type as your arrays, hence everything is integers). The second question is whether all of these variables (and the arrays) are local variables or global. It's impossible to say given what you've posted. If everything's global, Tom Gunn's first solution will work fine. If not, go with the second one:

template <typename T>
void BottomTurn(T front[], T left[], T back[], T right[], T bottom[])
{
    T a = front[6];
    T b = front[7];
    T c = front[8];

    front[6] = left[0];
    front[7] = left[3];
    front[8] = left[6];

    left[0] = back[2];
    left[3] = back[1];
    left[6] = back[0];

    back[0] = right[2];
    back[1] = right[5];
    back[2] = right[8];

    right[2] = c;
    right[5] = b;
    right[8] = a;

    T aa = bottom[0];
    T bb = bottom[1];

    bottom[0] = bottom[6];
    bottom[6] = bottom[8];
    bottom[8] = bottom[2];
    bottom[2] = aa;
    bottom[1] = bottom[3];
    bottom[3] = bottom[7];
    bottom[7] = bottom[5];
    bottom[5] = bb;
    bottom[4] = bottom[4];
}

and assuming everything is in fact an integer, you can replace T with int and delete line 1. I don't know if you are familiar with templates or not.

Thank you so much, I guess the answer was staring me in the face the whole time, but you kinda explained it to me. I compiled, and it worked! Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.