Professional Documents
Culture Documents
LOL
LOL
h>
#define N 3
void displayPuzzle(char NPuzzle[N][N]);
void displayPuzzle(char NPuzzle[N][N]){
for(int i=0;i<N;i++){
printf("+-----+-----+-----+\n");
for(int j=0;j<N;j++){
printf("| %c ",NPuzzle[i][j]);
}
printf("|");
printf("\n");
}
printf("+-----+-----+-----+\n");
}
return finish;
}
int checkMove(char move, int r, int c); //r gramm c stili p briskete to 0 ston
pinaka move kinisi
int checkMove(char move, int r, int c){
int valid=1;
/*An o xristis dosi tin timi move=L tha afero 1 apo tin thesi tou x aksona tou
'o', se periptosi pou to apotelesma tha exei arnitiki timi simeni pos ine invalid
An o xristis dosi tin timi move=R tha prostheto 1 apo tin thesi tou x aksona
tou 'o', se periptosi pou to apotelesma einai megalitero tou 2(size array-1) simeni
pos ine invalid
An o xristis dosi tin timi move=U tha afero 1 apo tin thesi tou y aksona tou
'o', se periptosi pou to apotelesma tha exei arnitiki timi simeni pos ine invalid
An o xristis dosi tin timi move=D tha prostheto 1 apo tin thesi tou y aksona
tou 'o', se periptosi pou to apotelesma einai megalitero tou 2(size array-1) simeni
pos ine invalid
An edose kapia timi ektos tou 'l' 'r' 'u' 'd' 'e' tote ine invalid
*/
if(move=='l')
{
r=r-1;
if(r<0)
valid=0;
}
else if(move=='r')
{
r=r+1;
if(r>2)
valid=0;
}
else if (move=='u')
{
c=c-1;
if(c<0)
valid=0;
}
else if(move=='d')
{
c=c+1;
if(c>2)
valid=0;
}
if (move!='l'&& move!='r'&& move!='u'&& move!='d'&& move!='e')
{
valid=0;
}
return valid;
}
displayPuzzle(NPuzzle);
printf("move> ");
do{
scanf(" %c",&move);
valid=checkMove(move,r,c); //kaloume checkmove gia na eleksei an einai
sosti i kinisi
if(valid==0){ //an i kinisi einai lathos tiponei minima error kai ksanazita
kinisi
printf("This is not a valid move\n ");
displayPuzzle(NPuzzle);
printf("move> ");
}
}while(valid==0);
if(move=='e')
{
printf("Exete epileksi na termatisti to pexnidi sas!\n Bye!");
break;
}
playMove(NPuzzle,move,r,c);
displayPuzzle(NPuzzle);
finish=isSolution(NPuzzle);
}
if(finish==1){
printf("Congratulations, you solved the 3-puzzle\n");
displayPuzzle(NPuzzle);
}
do
{
//zitaei apo ton xristi ta stoixeia tou pinaka
printf("Dwse to stoixeio gia tin thesi (0,0):\n");
scanf(" %c",&NPuzzle[0][0]);
printf("Dwse to stoixeio gia tin thesi (0,1):\n");
scanf(" %c",&NPuzzle[0][1]);
printf("Dwse to stoixeio gia tin thesi (0,2):\n");
scanf(" %c",&NPuzzle[0][2]);
printf("Dwse to stoixeio gia tin thesi (1,0):\n");
scanf(" %c",&NPuzzle[1][0]);
printf("Dwse to stoixeio gia tin thesi (1,1):\n");
scanf(" %c",&NPuzzle[1][1]);
printf("Dwse to stoixeio gia tin thesi (1,2):\n");
scanf(" %c",&NPuzzle[1][2]);
printf("Dwse to stoixeio gia tin thesi (2,0):\n");
scanf(" %c",&NPuzzle[2][0]);
printf("Dwse to stoixeio gia tin thesi (2,1):\n");
scanf(" %c",&NPuzzle[2][1]);
printf("Dwse to stoixeio gia tin thesi (2,2):\n");
scanf(" %c",&NPuzzle[2][2]);
lathos=isValidPuzzle(NPuzzle);
} while(lathos==1);
play(NPuzzle);
int main()
{
char move,NPuzzle[N][N];
int r,c;
createPuzzle(NPuzzle);
return 0;
}