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])