Due to recent prodding by lestrrat i decided to take up some code i had been writing that solves the nqueens problem. Eventually this code will get a Tk interface (hopefully using the nifty new module from clintp) and become a screensaver. The algorithms that do the behind the scenes stuff are almost done, and when implementing the last major optimization i ran into a brick wall.
This is implemented using OOP and i decided that it would make the most sense to have the chessboard and the queens be objects. The recent optimization involved moving some code that checks all the contested values for all the queens from main into Board.pm; now i run this code once directly after set-up, as opposed to in between every queen swap (there's a new code snippet in main to deal with changed contested values that runs in O(N) time :). In main it worked fine, but in Board.pm it fails in one interesting way. contested is a method in Queen.pm defined as thus:
sub contested : lvalue { my $self = shift; $self->{_contested} }
When going through Board.pm to find out why i ran into another interesting quandery; i had not required the Queen object in Board, yet it made new Queens, changed their location and other things anyway. The require line has since been added to Board.pm (and i smacked myself for forgetting it) but it did not fix any problems. So, with that set up, here is my snippet for setting the number of contentions (for reference) quickly followed by my questions:
# works in package main; but not package Board; foreach my $queen1 (@queens) { LOOP: foreach my $queen2 (@queens) { next LOOP if $queen1->duplicate($queen2->location); $queen1->contested++ if $linear->($queen1->location, $queen2->lo +cation); } }
In reply to Why does my method not lvalue properly? by jynx
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |