Skip to content

Instantly share code, notes, and snippets.

@farebord
Created Mar 16, 2019
Embed
What would you like to do?
An 8 queens chess problem checker with Python.
# This is the first version I did.
# Its pretty much ugly and I can improve it a lot.
# It should work but I didn't test it.
valid = [
# a b c d e f g h
[ 0, 0, 0, 0, 0, 1, 0, 0 ], # 1
[ 1, 0, 0, 0, 0, 0, 0, 0 ], # 2
[ 0, 0, 0, 0, 1, 0, 0, 0 ], # 3
[ 0, 1, 0, 0, 0, 0, 0, 0 ], # 4
[ 0, 0, 0, 0, 0, 0, 0, 1 ], # 5
[ 0, 0, 1, 0, 0, 0, 0, 0 ], # 6
[ 0, 0, 0, 0, 0, 0, 1, 0 ], # 7
[ 0, 0, 0, 1, 0, 0, 0, 0 ] # 8
]
def is_a_valid_8_queen(board):
attacked_pos = []
for row, columns in enumerate(board):
for column, piece in enumerate(columns):
if piece == 1:
if set(attacked_pos).intersection(set([number_to_column(column) + str(row + 1)])):
print("Invalid")
return False
attacked_pos += get_queen_attacked_pos(row,column)
print("Valid")
return False
def number_to_column(number):
columns = ["a","b","c","d","e","f","g","h"]
return columns[number]
def get_queen_attacked_pos(row, column):
return set([number_to_column(column) + str(row + 1)] + \
get_vertical(column) + \
get_horizontal(row) + \
get_main_diagonal(row, column) + \
get_main_diagonal_back(row, column) + \
get_anti_diagonal(row, column) + \
get_anti_diagonal_back(row, column))
def get_vertical(column):
return [number_to_column(column) + str(row) for row in range(1, 9)]
def get_horizontal(row):
return [number_to_column(column) + str(row + 1) for column in range(0,8)]
def get_main_diagonal(row, column):
column_counter = column
attacked_pos = []
for row_number in range(row + 2, 9):
column_counter += 1
if column_counter > 7:
break
attacked_pos.append(number_to_column(column_counter) + str(row_number))
return attacked_pos
def get_main_diagonal_back(row, column):
column_counter = column
attacked_pos = []
for row_number in range(row, 0, -1):
column_counter -= 1
if column_counter < 0:
break
attacked_pos.append(number_to_column(column_counter) + str(row_number))
return attacked_pos
def get_anti_diagonal(row, column):
column_counter = column
attacked_pos = []
for row_number in range(row + 2, 9):
column_counter -= 1
if column_counter < 0:
break
attacked_pos.append(number_to_column(column_counter) + str(row_number))
return attacked_pos
def get_anti_diagonal_back(row, column):
column_counter = column
attacked_pos = []
for row_number in range(row, 0, -1):
column_counter += 1
if column_counter > 7:
break
attacked_pos.append(number_to_column(column_counter) + str(row_number))
return attacked_pos
is_a_valid_8_queen(valid)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment