I know this is kinda long but do you know why my 8 queen algorithm doesn't return anything. I created an empty board, similar neighbour board, and queens (for following where they are) I put one queen for each column and put them into random rows then I calculated total collision and then I put queens at other rows (at the same column) one by one and calculated total collision again. After that, I found the position that would create the minimal collision, and until I run out of positions that I can minimise the collision, and after breaking the first(calculates neighbours collision) loop I break the second loop(resets all queens positions) if collision is 0.
import random
from array import array
board = [[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0]]
neighbour = [[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0]]
queens = [0,0,0,0,0,0,0,0]
def collision_count(column,row):
coll = 0
for j in range(8):
if j == row:
continue
if board[column][j] == 1 :
coll += 1
while(column < 7 and row < 7):
row += 1
column +=1
if board[column][row] == 1:
coll += 1
while(column > 0 and row > 0):
row -= 1
column -=1
if board[column][row] == 1:
coll += 1
while(column > 0 and row < 7):
row += 1
column -=1
if board[column][row] == 1:
coll += 1
while(column < 7 and row > 0):
row -= 1
column +=1
if board[column][row] == 1:
coll += 1
return coll
def totalcoll():
totcoll = 0
for i in range(8):
totcoll += collision_count(i,queens[i])
return totcoll
while True:
for i in range(8):
queens[i] = random.randrange(0,8)
board[i][queens[i]] = 1
totalcollision = totalcoll()
while True:
for i in range(8):
oldqueen = queens[i]
for j in range(8):
queens[i] = j
neighbour[i][j] = totalcoll()
queens[i] = oldqueen
min = neighbour[0][0]
minqueencol = 0
minqueenrow = 0
for i in range(8):
for j in range(8):
if(neighbour[i][j]<min):
min = neighbour[i][j]
minqueenrow = j
minqueencol = i
if min<totalcollision:
totalcollision = min
queens[minqueencol] = minqueenrow
else:
break
if totalcollision == 0:
break
print("a")
for i in range(8):
for j in range(8):
print(board[i][j])
You've put infinite loop with
while True
. Your loops never break. So your program flow never reached toprint(a)
andprint(board[i][j])
lines. I checked your code and your secondwhile
loop never breaks and works continuously. Please check your break conditions.Actually I noticed a problem in your code: as far as I read the algorithm, if I understood correctly, you're miscalculating the number of collisions.
This picture is your board status.if I understood correctly the algorithm, there is 4 collision in there. (correct me if I'm wrong) But your totalcoll() function calculated it as 18. I ran the code again just to be sure. and I saw this: while there are 3 collisions, totalcoll() returns 13 collision.
There is also another sample:
This is your board with 5 collision. But this is what says your program about collision (23 collision):
You should check your totalcoll() func.