Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 5

#include <stdio.

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");
}

int isSolution(char NPuzzle[N][N]);


int isSolution(char NPuzzle[N][N]){
int finish;
//*elegxos an exei lithei to puzzle
if((NPuzzle[0][0]=='a')&&(NPuzzle[0][1]=='b')&&(NPuzzle[0][2]=='c')&&(NPuzzle[1]
[0]=='d')&&
(NPuzzle[1][1]=='e')&&(NPuzzle[1][2]=='f')&&(NPuzzle[2][0]=='g')&&(NPuzzle[2]
[1]=='h')&&(NPuzzle[2][2]=='o')){
finish=1;
}
else
finish=0;

return finish;
}

void playMove(char NPuzzle[N][N], char move, int r, int c);


void playMove(char NPuzzle[N][N], char move, int r, int c){
char temp;
//allagi stoixeon meso kiniseon
if(move=='l'){
temp=NPuzzle[r][c-1];
NPuzzle[r][c-1]=NPuzzle[r][c];
NPuzzle[r][c]=temp;
}
else if (move=='u'){
temp=NPuzzle[r-1][c];
NPuzzle[r-1][c]=NPuzzle[r][c];
NPuzzle[r][c]=temp;
}
else if (move=='r'){
temp=NPuzzle[r][c+1];
NPuzzle[r][c+1]=NPuzzle[r][c];
NPuzzle[r][c]=temp;
}
else if (move=='d'){
temp=NPuzzle[r+1][c];
NPuzzle[r+1][c]=NPuzzle[r][c];
NPuzzle[r][c]=temp;
}
}

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;
}

void play(char NPuzzle[N][N]);


void play(char NPuzzle[N][N]){
int i,j,r,c,valid=1,finish=0;
char move;
finish=isSolution(NPuzzle);//Elexos an dothike h lisi san input apo ton xristi
if(finish==0)
{
printf("Enter your command in the following format:\n");
printf("move>direction\n");
printf("Notice: move> is the user prompt\n");
printf("direction is one of: l(left), r(right), u(up), d(down), e(exit)\
n");
}
while(finish==0)
{
//briskoume se pia thesei einai to stoixeio 'o'
for(j=0;j<N;j++){
for(i=0;i<N;i++){
if(NPuzzle[j][i]=='o'){
r=i; //apothikeuoume tis sintetagmenes tou 'o'
c=j;
}
}
}

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);
}

int isValidPuzzle(char NPuzzle[N][N]);


int isValidPuzzle(char NPuzzle[N][N]){
int pla=0,plb=0,plc=0,pld=0,ple=0,plf=0,plg=0,plh=0,plo=0;
int lathos=0;
//*elegxos an exoun dothei sosta stoixeia
for(int i=0;i<N;i++){
for (int j=0;j<N;j++){
if(NPuzzle[i][j]=='a'){
pla++;
}
if(NPuzzle[i][j]=='b'){
plb++;
}
if(NPuzzle[i][j]=='c'){
plc++;
}
if(NPuzzle[i][j]=='d'){
pld++;
}
if(NPuzzle[i][j]=='e'){
ple++;
}
if(NPuzzle[i][j]=='f'){
plf++;
}
if(NPuzzle[i][j]=='g'){
plg++;
}
if(NPuzzle[i][j]=='h'){
plh++;
}
if(NPuzzle[i][j]=='o'){
plo++;
}
}
}
if((pla==1)&&(plb==1)&&(plc==1)&&(pld==1)&&(ple==1)&&(plf==1)&&(plg==1)&&(plh==1)&&
(plo==1)){
lathos=0;
}
else {
printf("To puzzle pou dothike den einai ortho!\n");
lathos=1;
}
return lathos;
}

void createPuzzle(char NPuzzle[N][N]);


void createPuzzle(char NPuzzle[N][N]){
int lathos;

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;
}

You might also like