how does the regex engine not choose a free square in d4 when looking
for free squares in d3?
I do not know what you mean by this. Do you mean "why doesn't the
regex engine never try to place a queen on both d4 and d3?" In that
case, the answer is simple - it will never even attempt to place two
queens on the same column. The first part of the string is:
,a1,a2,a3,a4,a5,a6,a7,a8,
,b1,b2,b3,b4,b5,b6,b7,b8,
,c1,c2,c3,c4,c5,c6,c7,c8,
,d1,d2,d3,d4,d5,d6,d7,d8,
,e1,e2,e3,e4,e5,e6,e7,e8,
,f1,f2,f3,f4,f5,f6,f7,f8,
,g1,g2,g3,g4,g5,g6,g7,g8,
,h1,h2,h3,h4,h5,h6,h7,h8,
is being matched against the first part of the regex:
.*,(\w+),.*
.*,(\w+),.*
.*,(\w+),.*
.*,(\w+),.*
.*,(\w+),.*
.*,(\w+),.*
.*,(\w+),.*
.*,(\w+),.*
The newlines in the string and regex are significant here (. won't match
a newline). This part will make \1 one of 'a1' .. 'a8', \2 one of 'b1' ..
'b8', \3 one of 'c1' .. 'c8', etc. So, it will never try to place two
queens on the 'd' column.
But you may ask, "why doesn't it put a queen on d3 and one on e4?". That's
where the second part of the string and regex come in. \4 contains the
position of the queen on the d column, so, in this case, \4 equals 'd3'.
Then there's this line in the second part of the regex:
[\x00-\xFF]*\n\4:.*\1,.*\2,.*\3,.*\5,.*\6,.*\7,.*\8,.*
We know that \4 equals 'd3', so part of this lines read '\nd3:'. Looking
back at the second part of the string, there is only one line that starts
with 'd3:':
d3:a1,a2,a4,a5,a7,a8,b2,b4,b6,b7,b8,c1,c5,c6,c7,c8,e1,e5,e6,e7,e8,f2,f
+4,f6,f7,f8,g1,g2,g4,g5,g7,g8,h1,h2,h4,h5,h6,h8,
If you look carefully, after the colon are all the fields that aren't
attacked by a queen on d3. Specifically, the field 'e4' is missing.
But the line
[\x00-\xFF]*\n\4:.*\1,.*\2,.*\3,.*\5,.*\6,.*\7,.*\8,.*
is saying "match a line that starts with 'd3', and has after the colon
a list of fields that include the positions of all other 7 queens".
The
[\x00-\xFF]* just skips enough lines to get to the next
queen.
Abigail