I have a problem where eight queens are to be placed on an 8x8 chessboard in such a way that no queens will be able to attack each other (i.e., lie along same row, column or diagonal). The program will prompt the user to enter, for each column, the row that contains a queen. For the given configuration, the program should print "Safe" if no queen can attack any other queen (none are sharing the same row, column or diagonal), or print "Unsafe" if an attack is possible. I'm not entirely sure how to do this however, and I'd like some help. I am working from this base code:
By your definition, you do already know that each queen is on different column. That leaves the rows and diagonals.
What if you had an array of bool values that are initially all false? When you look at new queen, you know its row number. If that position in the bool array is already true, then the board is Unsafe. If the position is still false, you update it to true and continue.
Two similar bool arrays for the two diagonal directions. Calculating the position within those arrays is a bit more complicated though.
If no queen meets and Unsafe situation, the board is Safe.
When a queen is placed, check if all the values in the row, column and the two diagonals are false.
If any of them is true, print "unsafe"
If all of them are false, mark all the values in the row, column and the two diagonals as true and print "safe"
haha, lot of bumps!
normally it takes several hours to get reply.
it will be more reasonable question:
suppose one of your friend trying to solve eight queen puzzle in his chessboard. he is trying many variations and asking you if he could solved it!
this is one board position:
What? I don't know how to fill in from files or any of this. I just want to get the program I detailed in the OP working.
I will stop bumping so often though.
keltonfan2: the program you gave us is hardly even able to do anything. JLBorges and anup30 are giving you advice on how to build up your board by reading it in from file. this way you wont need to manually do it each time.
keskiverto, JLBorges and anup30 are giving you advice to so you can make you program work. think about what JLBorges and anup30 are saying and try incorporating that design into your program.