3×3 Matrix Puzzle.
In This game Play 3×3 matrix Puzzle A user enter a numbers between 1-8 in Random order but not in Repeat and zero is default set to 9th Position so user have arranged in Ascending Order.
CODE
#include<iostream.h> #include<conio.h> #include<dos.h> int Arry[10]; void draw(); void num_move(int n); void move(char x,int m); int num=0,list=-1; int step; void swap(int a,int b); void main() { clrscr(); char word; //Reading Elements From User cout<<" Enter the Elements in ARRRY OF Between 1-8 \n "; for(int i=0;i<8;i++) { cin>>Arry[i]; } Arry[9]='0';//Last Element set to Zero //Display Arry elements cout<<"Arry is Store "; for(i=0;i<9;i++) cout<<" "<<Arry[i]; while(1) //INFIITY set to true { if(word=='E' || word == 'e') break; //END LOOP when want TO EXIT draw(); if(Arry[0]==0 && Arry[1]==1 &&Arry[2]==2 &&Arry[3]==3 &&Arry[4]==4 &&Arry[5]==5 &&Arry[6]==6 &&Arry[7]==7 &&Arry[8]==8) { cout<<"\n Completed Sorted "; cout<<"\n Number of Step "<<step; break; } else { cout<<"\n Which Number to move "; cin>>num; for(i=0;i<9;i++) { //cout<<i<<" "; if(Arry[i]==num){list = i;break;} else {list = -1;} }//for loop if(list>-1) { num_move(list); cout<<"\n ONLY WAY TO MOVE OR EXIT(e,E) "; cin>>word; if(word=='l')word='L'; if(word=='r')word='R'; if(word=='t')word='T'; if(word=='b')word='B'; move(word,list); step++; } else { cout<<"\n NOT FOUND "; } }//Else Function }//while Loop delay(1000); cout<<" \n ENTER FOR EXIT "; getch(); } //ALL FUNCTION HERE void draw() { cout<<"\n\n"; cout<<" _____________"<<endl; cout<<" | |"<<"| |"<<"| |"<<endl; cout<<" | "<<Arry[0]<<" || "<<Arry[1]<<" || "<<Arry[2]<<" |"<<endl; cout<<" |___|"<<"|___|"<<"|___|"<<endl; cout<<" | |"<<"| |"<<"| |"<<endl; cout<<" | "<<Arry[3]<<" || "<<Arry[4]<<" || "<<Arry[5]<<" |"<<endl; cout<<" |___|"<<"|___|"<<"|___|"<<endl; cout<<" | |"<<"| |"<<"| |"<<endl; cout<<" | "<<Arry[6]<<" || "<<Arry[7]<<" || "<<Arry[8]<<" |"<<endl; cout<<" |___|"<<"|___|"<<"|___|"<<endl; } //Move Number in box void move(char x,int m) { switch(m)//arry Serial { case 0: if(x=='R'){ swap(0,1); } else if(x=='B'){ swap(0,3);} else {cout<<"\nWRONG MOVEMENT";} break; case 1: if(x=='L'){ swap(1,0); } else if(x=='R'){ swap(1,2);} else if(x=='B'){ swap(1,4);} else {cout<<"\nWRONG MOVEMENT";} break; case 2: if(x=='L'){ swap(2,1); } else if(x=='B'){ swap(2,5);} else {cout<<"\nWRONG MOVEMENT";} break; case 3: if(x=='T'){ swap(3,0); } else if(x=='R'){ swap(3,4);} else if(x=='B'){ swap(3,6);} else {cout<<"\nWRONG MOVEMENT";} break; case 4: if(x=='L'){ swap(4,3); } else if(x=='R'){ swap(4,5);} else if(x=='T'){ swap(4,1);} else if(x=='B'){ swap(4,7);} else {cout<<"\nWRONG MOVEMENT";} break; case 5: if(x=='L'){ swap(5,4); } else if(x=='T'){ swap(5,2);} else if(x=='B'){ swap(5,8);} else {cout<<"\nWRONG MOVEMENT";} break; case 6: if(x=='T'){ swap(6,3); } else if(x=='R'){ swap(6,7);} else {cout<<"\nWRONG MOVEMENT";} break; case 7: if(x=='L'){ swap(7,6); } else if(x=='R'){ swap(7,8);} else if(x=='T'){ swap(7,4);} else {cout<<"\nWRONG MOVEMENT";} break; case 8: if(x=='T'){ swap(8,5); } else if(x=='L'){ swap(8,7);} else {cout<<"\nWRONG MOVEMENT";} break; }//End Switch }//end MOVE //Checking Which way to move void num_move(int x) { switch(x) { case 0:cout<<" R OR B"; break; case 1:cout<<" L , R OR B"; break; case 2:cout<<" L OR B"; break; case 3:cout<<" U , B OR R"; break; case 4:cout<<" U , D , L OR R"; break; case 5:cout<<" L , T OR B";break; case 6:cout<<" T OR R"; break; case 7:cout<<" L , R OR T"; break; case 8:cout<<" L OR T"; break; }//End Switch }//end num_move void swap(int a,int b) { int temp; temp = Arry[a]; Arry[a] = Arry[b]; Arry[b] = temp; }//Swapping Arry
OUTPUT
(Visited 493 times, 1 visits today)
Written by:
Thank you a lot for giving everyone an extremely memorable opportunity to read articles and blog posts from here. It is always so nice and also stuffed with a lot of fun for me personally and my office acquaintances to visit your blog at minimum thrice weekly to read through the new issues you will have. Not to mention, I’m just at all times satisfied concerning the impressive tips you give. Certain 3 points in this post are particularly the most efficient we’ve had.