Cycling through some numbers

I posted a question about Rubik's cube here (You don't need to read it though): A simple console Rubik's cube in C (No need to solve, only design)

Basically, I just need help with this, so I can implement this in my Rubik's cube program. Suppose, there are 4 numbers like this [1,2,3,4] and I push from the left, so now it becomes [4,1,2,3]. If I push it again, it should become [3,4,1,2].

Basically, what I'm trying to achieve is rotating a row on the cube. And my rotating function is something like this (I'm still changing this because this doesn't work that well):

void swapRow(int row)
    int placeHolder[3];  //Consider it as a 'blank' row
        for(int j=0; j<3; j++)
            placeHolder[j] = cube[front][row][j];  

        for(int j=0; j<3; j++)
            cube[front][row][j] = cube[front+1][row][j]; 

        for(int j=0; j<3; j++)
            cube[front-1][row][j] = placeHolder[j]; 


This is my cube array:

int cube[6][3][3] = {{{1,1,1},{1,1,1},{1,1,1}}, 

front is a variable and is 1. This is where the problem lies, If I swap again and again, it will go to 4, then 5, then 6 etc. when I want it to go to 4, then 1, then 2, then 3, then 4, then 1 again because that's how much sides it has (not considering top and bottom).

How do you think I can accomplish this? I'm using C, not C++. I'll add any other information required. :)


Suggest to use a rotator object like:

typedef struct
  int *values;
  int mod;
  int size; //if you want to generalize the rubiks..

void rotate(rotator *rotator)
  rotator->mod = (rotator->mod + 1) % rotator->size;

void print_rotator(rotator *rotator)
  int index;
  fprintf(stdout, "[");
  for(index = 0; index < rotator->size; ++index)
    fprintf(stdout, "%d,", rotator->values[(index + rotator->mod) % rotator->size]);
  fprintf(stdout, "]\n");

Then you can have a think about how to build this into a n-ary cube..

Need Your Help

Producing CCITT compressed TIFF from CGImage

c++ macos core-graphics cgimage

I have a CGImage (core graphics, C/C++). It's grayscale. Well, originally it was B/W, but the CGImage may be RGB. That shouldn't matter. I want to create a CCITT-Group 4 TIFF.

Conditional JsonProperty using Jackson with Spring Boot

java json spring spring-boot jackson

A Spring Boot application is tasked with updating a remote integration API every so many minutes. This application can be deployed to a test or prod environment, the application is informed of the ...