Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- void the_big_to_the_first(int &n1, int &n2){
- if(n1<n2){
- int tmp = n1;
- n1 = n2;
- n2 = tmp;
- }
- }
- class HomeArray {
- public:
- int arr[9];
- void reset(){
- int i;
- for(i=0; i<9; i++)
- arr[i]=0;
- }
- void show(){
- for (int i=0; i<9; i++) cout<<arr[i]<<" ";
- cout<<"\n";
- }
- bool is_it_legal(){
- for (int i=0; i<8; i++){
- if(arr[i]==arr[i+1]) return false;
- switch (arr[i]){
- case 0: if(arr[i+1]!=1 && arr[i+1]!=2){ return false;} break;
- case 3: if(arr[i+1]==0){ return false;} break;
- case 4: if(arr[i+1]==0){ return false;} break;
- }
- // עד כאן ווידאנו שיש קווים בצורה של הבית בלבד
- // מכאן מתחילה בדיקה שאין שני מקלות זהים
- int an1 = arr[i], an2 = arr[i+1];
- the_big_to_the_first(an1, an2);
- // עד כאן המספר הגדול מבניהם נמצא במשתנה 1 והקטן במשתנה 2
- int ab1, ab2;
- for(int j=i+1; j<8; j++){
- ab1 = arr[j], ab2 = arr[j+1];
- the_big_to_the_first(ab1, ab2);
- if(ab1==an1 && ab2==an2) return false;
- }
- // עד כאן הבדיקה
- }
- return true;
- }
- };
- int Increase_the_array(int *Array, int max, int ind){ // (הפונקציה הזו מתאימה לכל גודל של מערך! (חייבת לקבל מערך שמאותחל כולו לאפסים
- if(ind<0) return -1;
- if (Array[ind] == max) {
- Array[ind] = 0;
- return Increase_the_array(Array, max, ind-1);
- }
- Array[ind]++;
- return 1;
- }
- void main(){
- cout<<"\n 0\n * *\n * *\n1*****2\n* *\n* *\n3*****4\n\n\n";
- cout<<"\n 0\n * *\n * *\n1*****2\n** **\n* * * *\n* * *\n* * * *\n** **\n3*****4\n";
- HomeArray HM[200], htmp;
- int ind=0;
- htmp.reset();
- do{
- if(htmp.is_it_legal() == true){
- HM[ind] = htmp;
- ind++;
- }
- }while(Increase_the_array(htmp.arr, 4, 8)==1);
- cout<<"\nThis program checks for all possible solutions to the game.\nFound: "<<ind<<"\n\n";
- for(int i=0; i<ind; i++){
- if(i<9)cout<<" ";
- cout<<i+1<<"@ ";
- HM[i].show();
- }
- cout<<"\n -END-\n\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment