8 queen/game of life
Frank Niessink
frankn=nuws at cs.vu.nl
Fri Jun 16 10:22:51 EDT 2000
root <root at sirius.ftsm.ukm.my> wrote:
> newbie question.
> 2. 8 non-attacking queen.
> use backtracking. the function call itself back. how to do recursive call
> in python?
I used a Board class that can solve itself. The board keeps track of
where the queens are.
class Board:
""" Class implementing a chess board. """
def __init__(self, size=8, display=None):
self.size = size
# board
self.board = [0]*size*size
# where are the queens
self.queens = []
# display is a class which supports putQueen, removeQueen
# and a solved method
self.display = display
[snip...]
def solve(self,col=0):
if col >= self.size:
if self.display:
self.display.solved(self)
return
for row in range(self.size):
if not self.putQueen(col, row):
continue
self.solve(col+1)
self.removeQueen(col, row)
Board.putQueen returns true if it can place a queen in the given
position, false otherwise.
If you want, I can mail you the complete program.
Cheers, Frank
--
"Hey, Marvin," said Zaphod striding over towards to him, "Hey, kid, are we
pleased to see you." Marvin turned, and in so far as it is possible for a
totally inert metal face to look reproachfully, this is what it did.
"No you're not," he said, "no one ever is."
-- Douglas Adams, 'The Restaurant at the End of the Universe'
More information about the Python-list
mailing list