This is my start on this brute force solver:
Code:
/* Will solve via brute force, the 8 tile 2D "rubik's cube", puzzle
Adak, Nov. 2008
Status: just started 7/11/08
*/
#include <stdio.h>
void copyB(int);
int leaves(int);
int move1(int);
int move2(int);
int move3(int);
void showB(int);
int isSolved(int);
int b[2][4][20]; //b is shorthand for the board
int t[1][4]; //t is a temporary holding row
int line[20]; //the line of moves that are played
int main(void) {
int i, r, row, c, col, move, deep, solved, gar;
// test right shift
/*
b[0][0][0] = 2; b[0][1][0] = 3; b[0][2][0] = 4; b[0][3][0] = 1;
b[1][0][0] = 7; b[1][1][0] = 6; b[1][2][0] = 5; b[1][3][0] = 8;
*/
// test row swap
/*
b[0][0][0] = 8; b[0][1][0] = 7; b[0][2][0] = 6; b[0][3][0] = 5;
b[1][0][0] = 1; b[1][1][0] = 2; b[1][2][0] = 3; b[1][3][0] = 4;
*/
// test Rotate 4 inner tiles
b[0][0][0] = 1; b[0][1][0] = 3; b[0][2][0] = 6; b[0][3][0] = 4;
b[1][0][0] = 8; b[1][1][0] = 2; b[1][2][0] = 7; b[1][3][0] = 5;
printf("\n Starting position:\n");
showB(0);
getch();
move = deep = solved = 0;
do {
move++; //deep++;
for(deep = 1; deep < 11; deep++) {
//88888888888888 working here 8888888888888
//get next perm here
//decode it and make the move
solved = leaves(deep);
}
for(i = 1; i < 4; i++) { //handles the leaf nodes
if(i == 1)
solved = move1(deep);
else if(i == 2)
solved = move2(deep);
else
solved = move3(deep);
if(solved)
break;
}
}while(!solved);
printf("\n\t This program is done ");
gar = getchar(); gar++;
return 0;
}
int leaves(int d) {
int i, solved;
solved = 0;
for(i = 1; i < 4; i++) { //handles the leaf nodes
if(i == 1)
solved = move1(d);
else if(i == 2)
solved = move2(d);
else
solved = move3(d);
if(solved)
break;
}
return solved;
}
void copyB(int d) { //copy the old board position, into the new depth
int r, c, temp;
for(r = 0; r < 2; r++)
for(c = 0; c < 4; c++)
b[r][c][d+1] = b[r][c][d];
}
int move1(int d) { //shifts all numbers one column to the right & wraps
int r, c, solved, temp;
solved = 0;
printf("\n Right shift:");
copyB(d);
++d;
for(r = 0; r < 2; r++) {
temp = b[r][3][d];
for(c = 2; c >-1 ; c--)
b[r][c+1][d] = b[r][c][d];
b[r][0][d] = temp;
}
line[d-1] = 1;
solved = isSolved(d);
if(!solved)
line[--d] = 0;
return solved;
}
int move2(int d) { //swaps rows
int r, c, solved;
solved = 0;
printf("\n Swap rows:");
copyB(d);
++d;
for(c = 0; c < 4; c++) {
t[0][c] = b[0][c][d];
b[0][c][d] = b[1][c][d];
b[1][c][d] = t[0][c];
}
line[d-1] = 2;
solved = isSolved(d);
if(!solved)
line[--d] = 0;
return solved;
}
int move3(int d) { //turns the 4 inner tiles, clockwise
int r, c, solved, temp;
solved = 0;
printf("\n Rotate 4 inner tiles:");
copyB(d);
++d;
temp = b[0][1][d];
b[0][1][d] = b[1][1][d];
b[1][1][d] = b[1][2][d];
b[1][2][d] = b[0][2][d];
b[0][2][d] = temp;
line[d-1] = 3;
solved = isSolved(d);
if(!solved)
line[--d] = 0;
return solved;
}
void showB(int d) {
int r, c;
printf("\n");
for(r = 0; r < 2; r++) {
for(c = 0; c < 4; c++)
printf(" %d", b[r][c][d]);
putchar('\n');
}
}
int isSolved(int d) {
int i, gar;
int done = 0;
if(b[0][0][d]==1 && b[0][1][d]==2 && b[0][2][d]==3 && b[0][3][d]==4)
if(b[1][0][d]==8 && b[1][1][d]==7 && b[1][2][d]==6 && b[1][3][d]==5) {
done = 1;
printf("\n\n\t\t It's solved!");
showB(d);
printf(" line: ");
for(i = 0; i < d; i++)
printf(" %d", line[i]);
//printf("\n\t Press Enter to Continue");
//gar = getchar(); ++gar;
}
return done;
}
What do you think about asking a mod to move this thread to the game programming forum?
The main C programming forum is very busy, and this is constantly being pushed off the first page, in a single day or two. The game forum (on this same board), is much less busy.
And what did your instructor say about solving this puzzle?