السلام عليكم :)
اخر مشروع ليا بعديها مش هكون موجود لمدة اسبوعين عشان الامتحانات :D
البرنامج مش متنظم نهائي ( لان مفيش وقت انا عايز اذاكر :D ) ان شاء الله في اجازة نصف السنة هتعلم شوية عن عمل الواجهات وهعمل البرنامج ده و connect 4 من تاني
المهم
البرنامج ده تطبيق ل back tracking algorithm وديه الخوارزمية المستعملة لعمل ال grid لاول مرة .. وتطبيق لل classes ايضا لاني لسا متعلمها امبارح :D
البرنامج بيقوم بعمل grid يستوفي كل الشروط لل sudoku في وقت قصييير جدا ... وبعد كدة بيلغي كل اللي ممكن يتشال وده بقي اللي بياخد وقت
لانو بيجرب يشيل كل واحدة وفي كل مرة يقوم بحل ال grid بال back tracking algorith بردو ولو كان لل grid اكتر من حل بيستبعد انو يشيل الجزء ده ... في النهاية بيقوم بعمل grid فيه من 20 - 30 رقم من ال 81 وبيكون ليها حل وحيد
الكود
#include <iostream>#include <ctime>#include <cstdlib>#include <algorithm>using namespace std;void Board(bool solution);void clean();int number[81];int number_solution[81];class check{private: void information(int index , int *row , int *column , int *block ) // first 2 are rows .. last 2 are columns { int temp = index; *block=0; while(temp > 8) temp = temp - 9 ; *column = temp; *row = int(index/9); if( *row <= 2 ) { if(*column <=2) { *block = 0; return; } else if(*column > 2 && *column <= 5) { *block = 1; return; } else { *block = 2; return; } } if(*row > 2) { if(*row <= 5 ) { if(*column <=2) { *block = 3; return; } else if(*column > 2 && *column <= 5) { *block = 4; return; } else { *block = 5; return; } } if(*row > 5) { if(*column <=2) { *block = 6; return; } else if(*column > 2 && *column <= 5) { *block = 7; return; } else { *block = 8; return; } } } }public : bool ValidOrNot(int index) { int row,column,block,limits[4],temp; information( index,&row,&column,&block ); //check rows int check_rows[10]={0,0,0,0,0,0,0,0,0,0}; for(int i = 0 ; i <= 8 ; i++) { check_rows[number[row*9+i]]++; } for(int T = 1 ; T<=9 ; T++) if(check_rows[T]>1) return false; //check column int check_column[10]={0,0,0,0,0,0,0,0,0,0}; for(int i = 0 ; i <= 8 ; i++) { check_column[number[column+i*9]]++; } for(int T = 1 ; T<=9 ; T++) if(check_column[T]>1) return false; //check blocks int Check_Block[10]={0,0,0,0,0,0,0,0,0,0}; // 0 - 1 rows 2 - 3 columns temp=block; while( temp > 2 ) temp = temp - 3; limits[2]=temp*3; limits[3]=temp*3 + 2; if(block <= 2) { limits[0] = 0; limits[1] = 2; } if (block >=3 && block <= 5 ) { limits[0] = 3; limits[1] = 5; } else if (block >= 6 && block <= 8) { limits[0] = 6 ; limits[1] = 8 ; } for(int R = limits[0] ; R <= limits[1] ; R++) { for(int C = limits[2] ; C <= limits[3] ; C ++ ) { Check_Block[number[9*R + C]]++; } } for(int T = 1 ; T<=9 ; T++) if(Check_Block[T]>1) return false;return true; }};class generator{private:int solution = 0 ;void BTSolver(int index , check helper){ if(solution > 1 ) return ; if(number[index] != 0 ) { if( index == 80 ) {solution++; return; } else BTSolver(index+1 , helper); } else for(int i = 0 ; i <= 8 ; i++) { if(solution > 1 ) return ; number[index]=i+1; if(helper.ValidOrNot(index) == true ) { if(index == 80) solution++; else BTSolver(index+1 , helper ); } number[index]=0; } return ;}int RandomRemover(check helper){ int Remover[81]; for(int i = 0 ; i<= 80 ; i++) { Remover=i; } random_shuffle(&Remover[0],&Remover[81]); for(int i = 0 ; i <= 80 ; i++) { solution = 0; int temp = number[Remover]; number[Remover]=0; BTSolver(0,helper); if(solution > 1) number[Remover]=temp; system("cls"); cout<<(i*100)/(80)<<" %" ; }return true;} bool BTCreator(int index , check helper) // back tracking algorithm to creat full grid{ int AV[9]={1,2,3,4,5,6,7,8,9}; random_shuffle(&AV[0],&AV[9]); for(int i = 0 ; i <= 8 ; i++) { number[index]=AV; if(helper.ValidOrNot(index) == true ) { if(index == 80) return true; if(BTCreator(index+1 , helper )==true) return true; } number[index]=0; } return false;}public:bool BTManager () // Back tracking algorithm Manager{ check helper; BTCreator(0,helper); for(int i = 0 ; i<=80 ; i++) { number_solution=number; } RandomRemover(helper);}};int main(){ srand(time(NULL)); clean(); generator generates; generates.BTManager(); /* for(int index = 0 ; index <= 80 ; index++) { if(index%9 == 0) cout<<endl<<endl; if(number[index]==0) cout<<" "<<" "; else cout<<number[index]<<" "; }*/ Board(false); cout<<endl<<"SEE SOLUTION : "; system( "pause >nul" ); Board(true);}void Board(bool solution) // Draw board{ int temp[81]; for(int i = 0 ; i<= 80 ; i++) if(solution == true) temp=number_solution; else temp=number; cout<<endl; int j = 0; for(int row = 0 ; row<= 35 ; row++) { if(row % 4 == 0) cout<<string(73,'-'); else { if( (row - 2) % 4 == 0) { for(int i = 0 ; i<=8 ; i++) { cout<<"|"<<" "; if( temp[j] != 0 ) cout<< temp[j]; else cout<<" "; cout<<" "; j++; } cout<<"|"; } else { for(int i = 0 ; i<=8 ; i++) cout<<"|"<<string(7,' '); cout<<"|"; } } cout<<endl; } cout<<string(73,'-');}void clean(){ for(int i = 0 ; i<= 80 ; i++) { number=0; }}

