int PlaceQueens(int** chessboard, int size, int level) { count++; int Q_placed = 0; /* Q_placed is a flag reflecting that whether a Queen is placed on the current level or not. This value is returned to preceeding levels. */ int QNextLevel; /* QNextLevel is a flag reflecting that whether a Queen is placed on the next level or not. During recurssion this Flag will allow recurssion */ int flag; /* Determines whether a Queen can be placed at current position or not. */ int i; for (i = 0; i < size; i++) { /*Breaks the loops after all the Queens have been placed*/ if (Q_placed == 1) break; /*Check whether Queen can be placed or not*/ flag = CheckKill(chessboard, size, level, i); /*If Queen can be placed*/ if (flag == 0) { /*Queen Placed. This will be changed if Next level Queen is not placed succesfully*/ Q_placed = 1; /*Queen Placed. */ chessboard[level][i] = 1; PlaceQueen(level, i); /*Check for Last level*/ if (level == size - 1) ; else /*Place a next level Queen*/ { QNextLevel = PlaceQueens(chessboard, size, level + 1); /*If next level Queen is not Placed.*/ if (QNextLevel == 0) { /*Mark this level Queen as Not Placed*/ Q_placed = 0; chessboard[level][i] = 0; RemoveQueen(level, i); if (i == size - 1) HomeQueen(level); } } } else if (flag == 1) { PlaceQueen(level, i); RemoveQueen(level, i); if (i == size - 1) HomeQueen(level); } } return Q_placed; }
char deploy_the_Queen() { int pos_x,pos_y,_x,_y,x,y,Queen_Counter_Confirm=0,Bspace=0; char AllowDeploy1 = 'F',AllowDeploy2 = 'F',Space = 'F'; for(y=0;y<N;y++) { for(x=0;x<N;x++) { if(C_board[y][x] == 'Q') { Queen_Counter_Confirm++; } } } if(Queen_Counter_Confirm != QCounter) { printf("\nQCounter Error\nQueens %i QCounter %i",Queen_Counter_Confirm,QCounter); } if(QCounter < N) { for(y=0;y<N;y++) { for(x=0;x<N;x++) { if(C_board[y][x] == '_') { Space = 'T'; break; } } if(Space == 'T') { break; } } if(Space == 'T') { do { pos_x = Get_Rand((N-1),(N-N)); pos_y = Get_Rand((N-1),(N-N)); for(y=0;y<(QCounter+1);y++) { if(QueenHistory[y][0] != -1 ) { if(QueenHistory[y][1] == pos_y && QueenHistory[y][2] == pos_x) { AllowDeploy1 = 'F'; printf("\nDont Allow\n"); for(_y=0;_y<N;_y++) { for(_x=0;_x<N;_x++) { if(C_board[_y][_x]=='_') { Bspace++; } } } if(Bspace<3) { printf("\nNo other space on the board\nQueen Removal Function initiated\n\n"); RemoveQueen(); } break; } else { continue; } } } if(C_board[pos_y][pos_x] == '_') { AllowDeploy2 = 'T'; } else { AllowDeploy2 = 'F'; } }while(AllowDeploy2 != 'T' && AllowDeploy1 != 'T'); printf("\n\nx = %i y = %i\n\n",pos_x,pos_y); C_board[pos_y][pos_x] = 'Q'; Queens_LOC[QCounter][0] = QCounter; Queens_LOC[QCounter][1] = pos_y; Queens_LOC[QCounter][2] = pos_x; QueenHistory[QHCounter][0] = QHCounter; QueenHistory[QHCounter][1] = pos_y; QueenHistory[QHCounter][2] = pos_x; QCounter++; QHCounter++; Queens_Attack_range(pos_y,pos_x); print_Board(); printf("\nQueen Added\n"); printQueenHistory(); printf("\nQueen Counter = %i\n",QCounter); system("pause"); } else { if(Remove_Queen == 0 && QCounter > 0) { RemoveQueen(); Remove_Queen++; } else { if(QCounter > 0) { for(x=0;x<(Remove_Queen);x++) { RemoveQueen(); if(Remove_Queen < 8) { Remove_Queen++; } else { Remove_Queen = 0; } } } } } } else { system("cls"); printf("\n\n\n\t\tProblem Solved\n\n"); print_Board(); system("pause"); exit(-1); } }