Saw some similar posts about this, but they used something called Django?
First of all, this is school assignment, i know stack overflow isnt to fond of us students asking for stuff. I just wanted to clarify that.
First code runs, but when I close it, the error occurs. And in my tkinter window nothing appears.
The issue lies in the following line of code:
board2 = EQ([0, 4, 7, 5, 2, 6, 1, 3])
The main function is there to test class for a correct solution. This is a Eight Queens problem. Hope someone can help me out.
Here is the rest of my code for context: If the code doesnt make sense, here is an image of the assignment, https://imgur.com/a/G5PdrRQ.
from tkinter import *
SIZE = 8
class EQ:
def __init__(self):
self.queens = SIZE * [-1]
window = Tk()
window.title("Eight Queens")
window.mainloop()
def get(self, i):
return self.queens[i]
def set(self, i, j):
self.queens[i] = j
def is_solved(self):
for i in range(0, 8):
#If two queens are in same row
for j in range(i 1, 8):
if self.queens[i] == self.queens[j]:
return False
#Diagonal down
count = 1
for j in range(i 1, 8):
if count self.queens[i] == self.queens[j]:
return
count = 1
#Diagonal up
count = 1
for j in range(i 1, 8):
if self.queens[i] - count == self.queens[j]:
return False
count = 1
return True
def print_board(self):
for i in range(0, 8):
for j in range(0,8):
print("|", end = " ")
if self.queens[j] == i:
print("X", end = " ")
else:
print(" ", end = " ")
print("|")
def main():
board1 = EQ()
board1.set(0, 2)
board1.set(1, 4)
board1.set(2, 7)
board1.set(3, 1)
board1.set(4, 0)
board1.set(5, 3)
board1.set(6, 6)
board1.set(7, 5)
print("Is board1 a correct eight queen placement?",
board1.is_solved())
board2 = EQ([0, 4, 7, 5, 2, 6, 1, 3])
if board2.is_solved():
print("Eight queens are placed correctly in board2")
board2.print_board()
else:
print("Eight queens are placed incorrectly in board2")
main()
CodePudding user response:
You don't need tkinter at all as the application has just console output.
As the current constructor of EQ
does not accept argument, so the second instance creation will raise the error.
To fix it, just modify constructor of EQ
to accept optional list:
class EQ:
def __init__(self, queens=None):
self.queens = queens if queens else SIZE*[-1]
# removed tkinter stuff
...