-5

I am currently working with individuals chars and that have to be updated after every turn that is made. we do this by setting temporary values then returning the new values. the issue we are having is that after we call an algorithm the output does not output the same thing as the cube.

    char greenCCW(char& g1, char& g2, char& g3, char& g4, char& g5, char& g6, char& g7,
     char& g8, char& g9, char& w1, char& w2, char& w3, char& r1, char& r4,
    char& r7, char& y7, char& y8, char& y9, char& o3, char& o6, char& o9);

char redCW(char& r1, char& r2, char& r3, char& r4, char& r5, char& r6, char& r7,char& r8, 
    char& r9,char& w1, char& w4, char& w7,char& g3, char& g6, char& g9,
    char& y1, char& y4, char& y7,char& b1, char& b4, char& b7);



int main()
{

    //The cube starts here
    char w1 = 'w';
    char w2 = 'w';
    char w3 = 'w';
    char w4 = 'w';
    char w5 = 'w';
    char w6 = 'w';
    char w7 = 'w';
    char w8 = 'w';
    char w9 = 'w';

    char y1 = 'y';
    char y2 = 'y';
    char y3 = 'y';
    char y4 = 'y';
    char y5 = 'y';
    char y6 = 'y';
    char y7 = 'y';
    char y8 = 'y';
    char y9 = 'y';

    char b1 = 'b';
    char b2 = 'b';
    char b3 = 'b';
    char b4 = 'b';
    char b5 = 'b';
    char b6 = 'b';
    char b7 = 'b';
    char b8 = 'b';
    char b9 = 'b';

    char o1 = 'o';
    char o2 = 'o';
    char o3 = 'o';
    char o4 = 'o';
    char o5 = 'o';
    char o6 = 'o';
    char o7 = 'o';
    char o8 = 'o';
    char o9 = 'o';

    char g1 = 'g';
    char g2 = 'g';
    char g3 = 'g';
    char g4 = 'g';
    char g5 = 'g';
    char g6 = 'g';
    char g7 = 'g';
    char g8 = 'g';
    char g9 = 'g';

    char r1 = 'r';
    char r2 = 'r';
    char r3 = 'r';
    char r4 = 'r';
    char r5 = 'r';
    char r6 = 'r';
    char r7 = 'r';
    char r8 = 'r';
    char r9 = 'r';


    //Prints inital cube state
    printCube(w1,w2,w3,w4,w5,w6,w7,w8,w9,y1,y2,y3,y4,y5,y6,y7,y8,y9,b1,b2,b3,b4,b5,b6,b7,b8,b9,o1,o2,o3,o4,o5,o6,o7,o8,o9,g1,g2,g3,g4,g5,g6,g7,g8,g9,r1,r2,r3,r4,r5,r6,r7,r8,r9);
    cout << endl;
    
    
    // a test algorithm
    cross(w1, w2, w3, w4, w5, w6, w7, w8, w9, y1, y2, y3, y4, y5, y6, y7, y8,
        y9, b1, b2, b3, b4, b5, b6, b7, b8, b9, r1, r2, r3, r4, r5, r6, r7, r8,
        r9, o1, o2, o3, o4, o5, o6, o7, o8,
        o9, g1, g2, g3, g4, g5, g6, g7, g8, g9);

   
    //Prints cube state after the turns
    printCube(w1, w2, w3, w4, w5, w6, w7, w8, w9, y1, y2, y3, y4, y5, y6, y7,
        y8, y9, b1, b2, b3, b4, b5, b6, b7, b8, b9, o1, o2, o3, o4, o5,
        o6, o7, o8, o9, g1, g2, g3, g4, g5, g6, g7, g8, g9, r1, r2, r3,
        r4, r5, r6, r7, r8, r9);

}

char greenCCW(char& g1, char& g2, char& g3, char& g4, char& g5, char& g6, char& g7,
    char& g8, char& g9, char& w1, char& w2, char& w3, char& r1, char& r4,
    char& r7, char& y7, char& y8, char& y9, char& o3, char& o6, char& o9)
{

    char tg1, tg2, tg3, tg4, tg5, tg6, tg7, tg8, tg9, to3, to6, to9, tr1, tr4,
        tr7, tw1, tw2, tw3, ty7, ty8, ty9;


    //Temp values are set
    tg1 = g1;
    tg2 = g2;
    tg3 = g3;
    tg4 = g4;
    tg5 = g5;
    tg6 = g6;
    tg7 = g7;
    tg8 = g8;
    tg9 = g9;

    to3 = o3;
    to6 = o6;
    to9 = o9;

    ty7 = y7;
    ty8 = y8;
    ty9 = y9;

    tr1 = r1;
    tr4 = r4;
    tr7 = r7;

    tw1 = w1;
    tw2 = w2;
    tw3 = w3;


    //Cube values are corrected for turn
    g1 = tg3;
    g2 = tg6;
    g3 = tg9;
    g4 = tg2;
    g5 = tg5;
    g6 = tg8;
    g7 = tg1;
    g8 = tg4;
    g9 = tg7;

    w1 = tr7;
    w2 = tr4;
    w3 = tr1;

    y7 = to3;
    y8 = to6;
    y9 = to9;

    o3 = tw1;
    o6 = tw2;
    o9 = tw3;

    r1 = ty7;
    r4 = ty8;
    r7 = ty9;


    //Return values for the turn
    return g1, g2, g3, g4, g5, g6, g7, g8, g9, o3, o6, o9, r7, r4, r1, w1, w2,
        w3, y7, y8, y9;


}



char
redCW(char& r1, char& r2, char& r3, char& r4, char& r5, char& r6, char& r7,
    char& r8, char& r9, 
    char& w1, char& w4, char& w7,
    char& g3, char& g6, char& g9,
    char& y1, char& y4, char& y7,
    char& b1, char& b4, char& b7)
{

    char tr1, tr2, tr3, tr4, tr5, tr6, tr7, tr8, tr9, tw1, tw4, tw7, tb1, tb4,
        tb7, ty1, ty4, ty7, tg3, tg6, tg9;


    //temp values are set
    tr1 = r1;
    tr2 = r2;
    tr3 = r3;
    tr4 = r4;
    tr5 = r5;
    tr6 = r6;
    tr7 = r7;
    tr8 = r8;
    tr9 = r9;

    tw1 = w1;
    tw4 = w4;
    tw7 = w7;

    tb1 = b1;
    tb4 = b4;
    tb7 = b7;

    ty1 = y1;
    ty4 = y4;
    ty7 = y7;

    tg3 = g3;
    tg6 = g6;
    tg9 = g9;


    //cube values are corrected for turn
    r1 = tr7;
    r2 = tr4;
    r3 = tr1;
    r4 = tr8;
    r5 = tr5;
    r6 = tr2;
    r7 = tr9;
    r8 = tr6;
    r9 = tr3;

    w1 = tg9;
    w4 = tg6;
    w7 = tg3;

    y1 = tb1;
    y4 = tb4;
    y7 = tb7;

    b1 = tw1;
    b4 = tw4;
    b7 = tw7;

    g9 = ty7;
    g6 = ty4;
    g3 = ty1;


    return r1, r2, r3, r4, r5, r6, r7, r8, r9, w1, w4, w7, y1, y4, y7, b1,
        b4, b7, g3, g6, g9;

}

//test code

char cross(char& w1, char& w2, char& w3, char& w4, char& w5, char& w6, char& w7,
    char& w8, char& w9,
    char& y1, char& y2, char& y3, char& y4, char& y5,
    char& y6, char& y7, char& y8, char& y9,
    char& b1, char& b2, char& b3,
    char& b4, char& b5, char& b6, char& b7, char& b8, char& b9,
    char& o1,
    char& o2, char& o3, char& o4, char& o5, char& o6, char& o7, char& o8,
    char& o9,
    char& g1, char& g2, char& g3, char& g4, char& g5, char& g6,
    char& g7, char& g8, char& g9,
    char& r1, char& r2, char& r3, char& r4,
    char& r5, char& r6, char& r7, char& r8, char& r9)
{


    if (w1 == 'w')

    {

        greenCCW(g1, g2, g3, g4, g5, g6, g7, g8, g9, o3, o6, o9, r7, r4, r1,
            w7, w8, w9, y1, y2, y3);

        redCW(r1, r2, r3, r4, r5, r6, r7, r8, r9, w3, w6, w9, g3, g6, g9, y3,
            y6, y9, b1, b4, b7);


    }

    return w1, w2, w3, w4, w5, w6, w7, w8, w9, y1, y2, y3, y4, y5, y6, y7, y8,
        y9, b1, b2, b3, b4, b5, b6, b7, b8, b9, o1, o2, o3, o4, o5, o6, o7, o8,
        o9, g1, g2, g3, g4, g5, g6, g7, g8, g9, r1, r2, r3, r4, r5, r6, r7, r8,
        r9;

}

Output:

White Side:    wwbwwboob

Yellow Side:   yygyygyyg

Blue Side:     bbybbobbo

Orange Side:   yyrooroor

Green Side:    wwwgggggg


Red Side:      wrrwrrorr

what it's supposed to display

White Side:    grrgwwgww 

Yellow Side:   byybyyboo

Blue Side:     rbbwbbwbb

Orange Side:   oowoowoow

Green Side:    ggoggyggy

Red Side:      yyyrrrrrr
CiaPan
  • 9,188
  • 2
  • 19
  • 32
  • 7
    if you have an issue with lots of chars printing the wrong value, I suppose you can also reproduce the issue with 2 or 3 chars printing unexpected values. Please read about [mcve]. And I strongly suggest to read about arrays and/or vectors. – 463035818_is_not_a_number May 05 '22 at 14:53
  • 4
    I would have to think there is a simpler and more organized way to write what you are trying to write that would not be a sea of two and three letter variable names. – crashmstr May 05 '22 at 14:56
  • Hint: what do you _want_ to return from your functions? And what do you _declare_ to return from your functions? – CiaPan May 05 '22 at 14:59
  • not sure if there are other issues (the code is not easy to read), but the duplicate addresses one of your issues – 463035818_is_not_a_number May 05 '22 at 15:01
  • 1
    Rubrik's cube? Best use arrays (`std::array`) or other data structures, possibly multidimensional, at least structs within structs (e.g. struct for row of 3, struct with 3 rows for side, struct for 3 sides for cube). Then you can use loops or higher-level types and functions. If this is not possible, you can at least use a preprocessor (the builtin preprocessor or some other language) to autogenerate code, much less error-prone, compact and simpler. – Sebastian May 05 '22 at 15:03
  • 6
    `return w1, w2, w3, w4, w5, w6, w7, w8, w9, y1, y2, y3, y4, y5, y6, y7, y8, y9, b1, b2, b3, b4, b5, b6, b7, b8, b9, o1, o2, o3, o4, o5, o6, o7, o8, o9, g1, g2, g3, g4, g5, g6, g7, g8, g9, r1, r2, r3, r4, r5, r6, r7, r8, r9;` does not do what it appears you think it does. – Eljay May 05 '22 at 15:05
  • 4
    kudos for endurance to write this code, though a little less endurance and more "wtf there must be a simpler way" would have helped. When you want to name variables `r1`, `r2`, `r3`,`r4` etc, chances are high that you acutally want a `std::vector` or `std::array` – 463035818_is_not_a_number May 05 '22 at 15:06
  • 1
    Seconding @crashmstr I'd suggest **1.** to put all 6×3×3 values into some structure or 1-dimensional array, and **2.** to add a function `void rot(char&,char&, char&, char&);` to shift four values in a cycle, then **3.** represent each side rotation as five cyclic shifts: two for edges and three for corners. – CiaPan May 05 '22 at 15:07
  • Apart from the single line with `cout << .....` your code looks like plain C, not C++. – CiaPan May 05 '22 at 15:09
  • @CiaPan and the reference parameters – Sebastian May 05 '22 at 15:12
  • 1
    @Sebastian Whoops, you're right! I sometimes need to work with both languages simultaneously and I got used to switch between them in my head just at the moment I press Ctrl+Tab, so I missed the ampersand / asterisk difference here. :D – CiaPan May 05 '22 at 15:19
  • when use the functions individually they work. its only when the i use the functions one after another. – user19043150 May 05 '22 at 15:20
  • 1
    @user19043150 Possibly you're wrong. How did you make sure 'they work'? If you copy two, say, green tiles and switch their destinations, you will not notice they got swapped, because you get green-and-green anyway. However, if one of them was already replaced with a yellow one, the result will be some kind of yellow-green instead of green-yellow. Did you test _every single tile_ is copied to the correct position in every possible turn? – CiaPan May 05 '22 at 15:24
  • 2
    The message **This question already has answers here:** is patently **wrong!** The code presented makes no use of the returned values whatsoever, so either 'working' or 'not working' of the comma operator in the `return` statements have **nothing to do** with the OP's problem. – CiaPan May 05 '22 at 15:29
  • can you further explain? – user19043150 May 05 '22 at 15:32
  • So either use `return` wth `tie` or `tuple` or use reference parameters and `void` returning functions. Or better use a class and member variables of that class. – Sebastian May 05 '22 at 15:36
  • how would I be able to change the values if it has to update a lot? – user19043150 May 05 '22 at 18:25
  • See for example here for a calculation with rotation matrices: https://softwareengineering.stackexchange.com/questions/142760/how-to-represent-a-rubiks-cube-in-a-data-structure/262847#262847 – Sebastian May 05 '22 at 20:34

0 Answers0