Changed 'left', 'right', 'up' and 'down' into '1', '2', '3' and '4' respectively. That seems to have helped, but my new issue is that the move functions aren't actually updating the puzzle space for some reason.
New code:
int a=0;
int b=0;
int g=0;
int w=0;
int c=0;
int d=0;
int e=0;
int BposX=0;
int BposY=0;
int AposX=0;
int AposY=0;
int AposTemp=0;
int dir=0;
int action=0;
int n=0;
int f=0;
int h=0;
int puzzle[3][3];
void SetState(){
BposX=0;
BposY=0;
for (a=3;a>0;a--){
for (b=3;b>0;b--){
puzzle[a][b]=c;
c++;
}
}
}
void printState(){
for (g=3;g>0;g--){
for (w=3;w>0;w--){
if (puzzle[g][w]!=0){
Serial.print(puzzle[g][w]);
}
else{
Serial.print("b");
}
}
Serial.println();
}
Serial.println("(" + String(BposX) + ", " + String(BposY) + ")");
}
void Move(int dir){
for (d=2;d>-1;d--){
for (e=2;e>-1;e--){
if (puzzle[d][e]==0){
BposX=d;
BposY=e;
}
}
}
if (dir==1){
if (BposX>0){
AposTemp=puzzle[BposX-1][BposY];
puzzle[BposX][BposY]=AposTemp;
puzzle[BposX-1][BposY]=0;
BposX--;
}
else{
Serial.println("Invalid move.");
}
}
if (dir==2){
if(BposX<2){
AposTemp=puzzle[BposX+1][BposY];
puzzle[BposX][BposY]=AposTemp;
puzzle[BposX+1][BposY]=0;
BposX++;
}
else{
Serial.println("Invalid move.");
}
}
if (dir==3){
if(BposY>0){
AposY=BposY-1;
AposTemp=puzzle[BposX][AposY];
puzzle[BposX][AposY]=0;
puzzle[BposX][BposY]=AposTemp;
}
else{
Serial.println("Invalid move.");
}
}
if (dir==4){
if(BposY<2){
AposY=BposY+1;
AposTemp=puzzle[BposX][AposY];
puzzle[BposX][AposY]=0;
puzzle[BposX][BposY]=AposTemp;
}
else{
Serial.println("Invalid move.");
}
}
printState();
}
void randomizeState(int n){
int randomizer[n];
for(f=n;f>0;f--){
randomizer[f]=random(1,4);
Move(randomizer[f]);
}
}
void solveAstar(int heur){
}
void solvebeam(int k){
}
void MaxNodes(int n){
}
void setup(){
randomSeed(analogRead(A0));
SetState();
Serial.begin(9600);
printState();
Move(2);
Move(1);
Move(2);
Move(2);
}
void loop(){
/*if (Serial.available() > 0) {
action = Serial.read();
}
if (action=='1'){
Move('left');
action=0;
}
if (action=='2'){
Move('right');
action=0;
}*/
}
I added two nested for loops at the beginning of Move() to keep track of where the 'blank tile' is supposed to be, as well as a few empty and partially-complete functions that will be used later in the assignment.
The list of moves in setup() generates this output:
b12
345
678
(0, 0) //Start
b12
345
678
(1, 0) //Move right
Invalid move.
b12
345
678
(0, 0) //Move left
b12
345
678
(1, 0) //Move right
b12
345
678
(1, 0) //Move right again