[Tutor] checking diagonals on a chessboard

Andre Roberge andre.roberge at gmail.com
Thu Apr 13 21:35:58 CEST 2006


On 4/13/06, Matthew Singletary <matt.singletary at gmail.com> wrote:
>
> first, A disclaimer:
> This isn't directly homework but is related to an assignment of mine
>
> I'm building a genetic algorithm to solve the queen placement problem, the
> complicated stuff I can do on my own, but I'm not getting one part.
> suppose the queen is on a square, I can check that it is in the same row or
> same col but the diagonals, are less straight-forward.
> I know I could solve this by just smashing away at it, but I was wondering
> if anyone could suggest some tips on directions to work
>
> I'm NOT looking for any answers, just some tips to an _elegant_ method to
> solve this.

The answer you are looking for most likely depends on how you
represent your chessboard (i.e. single list, list of list, etc.).

Not an answer to your question directly, but a Python solution using
recursion can be found at:
http://en.wikipedia.org/wiki/Eight_queens_puzzle
Of course, it has to include how to check for diagonals, which you can
use for what you are looking for.  I don't know if it can be
considered to be "elegant".

[As an aside, I used this solution to write an undocumented "visual"
solution of this problem using rur-ple (rur-ple.sourceforge.net),
where 8 "queens" move on a screen to illustrate one such solution,
with colored lines showing which square is attacked by each queen. 
For those that have downloaded rur-ple, you can find this solution
under rur_programs/temp/queen.rur]

André

>
> The reason I started teaching myself python was it seemed a little more
> concise, and it seems there should be an elegant way to do this but I guess
> I'm not used to thinking the pythonic way
>
> thanks for any help
> matt
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
>


More information about the Tutor mailing list