Can anyone check my code in placing queens in a chessboard game?
@electrokid
what do you mean placing queens? how?
can you look at my code? i'll post it
which language are you using?
c++
well, Check mate....
its been 11hrs, I slept, woke up and no code yet.
bool ChessBoard::PlaceQueens (int c) { int num=n, rows=0; if (0<=c && c<n) { while (rows<n) { if (Safe (rows,c)==true) { SetQueen (rows, c); if (PlaceQueens(c+1)) return true; else { RemoveQueen (rows, c); rows++; } } else if (Safe(rows,c)==false && rows==(n-1)) { rows--; c--; RemoveQueen (rows, c); } row++; } return false; } else return true; } ======================================= int* queen; bool* row;
what is the condition for placing the queens? any restriction .. any order?
Any order as long as it's always safe
and you have a member function for "Safe(int, int)"
yes, but i'm not sure if it's right =============================================== bool ChessBoard::Safe (int r, int c) const { int i=1; while (0<=r-i && 0<=c-i) { if (queen[c-1]==0 && row[r-1]==false) if (queen[c-1]==0 && row[r]==false) if (queen[c]==0 && row[r-1]==false) return true; i++; } i=1; while (r+i<n && c+i<n) { if (queen[c+i]==0 && row[r+i]==false) if (queen[c+i]==0 && row[r]==false) if (queen[c]==0 && row [r+1]==false) return true; i++; } return false; }
hey is it okay if u can look at my program in my question?
this one sounds correct. the easy way to check would be to use a "putchar('*')" at a grid location that is safe. and check by logic
to begin with, try with a 5x5 grid
I tried 4, this is how it looks
um pretty
Join our real-time social learning platform and learn together with your friends!