8-puzzle - Functions not working correctly

Fixed it by rewriting the Move() function entirely, as well as a few others.

int a=0;
int b=0;
int g=0;
int w=0;
int c=0;
int d=0;
int e=0;
int Bpos=0;
int AposTemp=0;
int dir=0;
int action=0;
int n=0;
int f=0;
int h=0;
boolean isValid=true;

int puzzle[8];

void SetState(){
  Bpos=0;
  for (a=0;a<9;a++){
    puzzle[a]=a;
  }
}

void printState(){
  for (g=0;g<9;g++){
    if(puzzle[g]!=0){
      Serial.print(puzzle[g]);
    }
    else{
      Serial.print("b");
    }
    if((g==2)||(g==5)||(g==8)){
      Serial.println();
    }
  }
  Serial.println("(" + String(Bpos) + ")");
}

void Change (int dir){
  if (dir==1){
    if((Bpos!=0)&&(Bpos!=3)&&(Bpos!=6)){
      isValid=true;
      AposTemp=puzzle[Bpos-1];
      puzzle[Bpos]=AposTemp;
      puzzle[Bpos-1]=0;
      Bpos--;
    }
    else{
      isValid=false;
    }
  }
  if (dir==2){
    if((Bpos!=2)&&(Bpos!=5)&&(Bpos!=8)){
      isValid=true;
      AposTemp=puzzle[Bpos+1];
      puzzle[Bpos]=AposTemp;
      puzzle[Bpos+1]=0;
      Bpos++;
    }
    else{
      isValid=false;
    }
  }
  if (dir==3){//up
    if((Bpos!=0)&&(Bpos!=1)&&(Bpos!=2)){
      isValid=true;
      AposTemp=puzzle[Bpos-3];
      puzzle[Bpos]=AposTemp;
      puzzle[Bpos-3]=0;
      Bpos-=3;
    }
    else{
      isValid=false;
    }
  }
  if (dir==4){//down
    if((Bpos!=6)&&(Bpos!=7)&&(Bpos!=8)){
      isValid=true;
      AposTemp=puzzle[Bpos+3];
      puzzle[Bpos]=AposTemp;
      puzzle[Bpos+3]=0;
      Bpos+=3;
    }
    else{
      isValid=false;
    }
  }
  if(isValid==false){
    if(dir==4){
      dir=1;
    }
    else{
      Change(random(1,5));//BUG: Has 25% chance of making reverse
      //move, thus resulting in no action for this iteration.
    }
  }
  printState();
}

void Move(int dir){
  if (dir==1){
    if((Bpos!=0)&&(Bpos!=3)&&(Bpos!=6)){
      AposTemp=puzzle[Bpos-1];
      puzzle[Bpos]=AposTemp;
      puzzle[Bpos-1]=0;
      Bpos--;
    }
    else{
      Serial.println("Invalid move.");
    }
  }
  if (dir==2){
    if((Bpos!=2)&&(Bpos!=5)&&(Bpos!=8)){
      AposTemp=puzzle[Bpos+1];
      puzzle[Bpos]=AposTemp;
      puzzle[Bpos+1]=0;
      Bpos++;
    }
    else{
      Serial.println("Invalid move.");
    }
  }
  if (dir==3){//up
    if((Bpos!=0)&&(Bpos!=1)&&(Bpos!=2)){
      AposTemp=puzzle[Bpos-3];
      puzzle[Bpos]=AposTemp;
      puzzle[Bpos-3]=0;
      Bpos-=3;
    }
    else{
      Serial.println("Invalid move.");
    }
  }
  if (dir==4){//down
    if((Bpos!=6)&&(Bpos!=7)&&(Bpos!=8)){
      AposTemp=puzzle[Bpos+3];
      puzzle[Bpos]=AposTemp;
      puzzle[Bpos+3]=0;
      Bpos+=3;
    }
    else{
      Serial.println("Invalid move.");
    }
  }
  printState();
}

void randomizeState(int n){
  int randomizer[n];
  for(f=n;f>0;f--){
    randomizer[f]=random(1,5);
    Change(randomizer[f]);
  }
}

void solveAstar(int heur){
  
}

void solvebeam(int k){
  
}

void MaxNodes(int n){
  
}

void setup(){
  randomSeed(analogRead(A0));
  SetState();
  Serial.begin(9600);
  printState();
  randomizeState(6);
}

void loop(){
  /*if (Serial.available() > 0) {
    action = Serial.read();
  }
  if (action=='1'){
    Move('left');
    action=0;
  }
  if (action=='2'){
    Move('right');
    action=0;
  }*/
}

Change() is a variant of Move() that is specifically used by the randomizeState(n) function, as it does not return an 'Invalid move' message but instead makes another random move to continue randomizing the puzzle. However, I've noticed that the code I put in there to change the move to another random move will occasionally result in nothing happening (the blank tile stays right where it is for that particular step). At first I thought it was making a move and then moving back, but it would never make the first move in the first place if the direction it attempted to move in was impossible, so now I'm confused.

The goal of randomizeState(n) is to move the blank tile 'b' a pre-specified number of moves n in random directions away from the goal state (b12 345 678). This ensures that for later parts of the assignment, where the quickest solution is calculated, that a solution actually exists for that 'random' puzzle (as apparently not all configurations of this puzzle have the goal state as a viable solution). In moving n number of times, each movement should be an actual move in a viable direction, as otherwise moves would be 'skipped' and the actual number of moves away from the goal state would be lower. In order to move randomly, randomizeState sets up a queue array of moves it will make, with an array length of n. It fills these array values with random numbers containing either 1, 2, 3, or 4 (the respective directions), and then executes them by passing the numbers as an argument to Change() in reverse order (from randomizer[n] to randomizer[0]). Why is there an occasional move where nothing happens?

Example output for randomizeStates(n) of n=6:

(3x3 is the puzzle matrix, and the number in parentheses is the position of the blank tile from 0 to 8)

b12
345
678
(0)//Start

312
b45
678
(3)//moved down

b12
345
678
(0)//moved up

b12
345
678
(0)//did nothing!

1b2
345
678
(1)//moved right

1b2
345
678
(1)//did nothing!

1b2
345
678
(1)//did nothing!

142
3b5
678
(4)//moved down

1b2
345
678
(1)//moved up

142
3b5
678
(4)//moved down