Successful strategies for a queens placing game on an n x n chess board
aa r X i v : . [ m a t h . HO ] A p r Successful strategies for a queens placing game on an n x n chess board Thomas Jenrich2014-04-21
In his list of open problems ([1]), Martin Erickson described a certain game:“Two players alternately put queens on an n x n chess board so that each new queen is not in rangeof any queen already on the board (the color of the queens is unimportant). The last player who canmove wins.”Then he asked: “Who should win?”Obviously, for n up to 3, the first player wins, if he does not miss to start at the central position inthe case n = 3.In this article, we give very simple always winning strategies for the first player if n is 4 or odd.The additionally (in the source package) provided computer program QPGAME3 has been used tocheck that there are successful strategies for the first player if n is 6 or 8, and for the second playerif n is 10, 12, 14, or 16.As discovered during the submission process of the first version of this article, Hassan A Noonpresented consistent results concerning values of n which are odd or at most 10, in his B.A. thesis([2]) and, together with Glen Van Brummelen, in a journal article ([3]). n = 4 The first player places a queen at one of the four inner positions. When * indicates an unavailablecell and . indicates an available cell, we do have this situation (up to meaningless rotations of thecomplete board in steps of 90 degrees): * * * .* * * ** * * .. * . *
No matter which of the four available cells the second player takes, it remains exactly one availablecell for the first player to place the last queen.
The rows and columns of the board are numbered from 0 to n −
1. A position is an (ordered) pairof a row number r and a column number c , written as ( r , c ).If two queens are placed at ( r , c ) and ( r , c ), then the game rules require: r = r (different rows), c = c (different columns), 1 + c = r + c (different falling diagonals), and r − c = r − c (different rising diagonals). n The first player puts the first queen to the cell at the central position (( n − / n − / r , c ), the first playerputs a queen to the cell at position ( R , C ), where R = n − − r and C = n − − c . This is a validposition, received from ( r , c ) by mirroring the two coordinate values with respect to the middle lines,or, with the same effect, by rotating the board a half turn.This strategy is always successful for the following reasons:The central cell lies just at the middle of the straight line between ( r , c ) and ( R , C ). Thus, a conflictbetween queens at these positions exists if and only if there is a conflict between ( r , c ) and the queenat the central position; but this is forbidden by the game rules. If ( R , C ) was available before a queenis put to ( r , c ), then it is still available after that move.After the placing of the first queen, there is (half turn) rotational symmetry. Because of thissymmetry, ( R , C ) is available if and only if ( r , c ) is available. After putting two queens to the cellsat these positions, the (half turn) rotational symmetry exists again.Eventually, no cell is available (for the second player) and the first player wins. As like as its two predecessors, the source code file QPGAME3.PAS has been developed for PASCALcompilers compatible with Turbo Pascal 4.0. Lines are at most 78 characters long. For inspectionsthe use of an ASCII compatible monospaced font is strongly recommended. The intended indentationis by one character per structure level, using blanks (instead of tabs).The program includes a good portion of comments (enclosed in curly braces). So it should be fairlyunderstandable at least by readers knowing at least one imperative programming language.Basically, the core procedure wins performs a general backtracking search. Of course, all selectedpositions have to be legal.When a player in a certain situation finds a winning move position P as an answer to his opponent’smove to a position P , it is clear that P would be a winning answer to P if the situation beforeboth moves did not change. Therefore such a winning move position P is registered in the internalvariable forbidden and skipped in further checks. That would give no advantage if P has beenchecked before P . Therefore, excluding (“for historical reasons”) the second move of a game, thesearch of an answering move starts in a row that follows the row of the move of the previous movewith respect to the search order for the previous move.The newly added Boolean routine parameter rotsym indicates, whether all currently involved movesof player 2 had re-established the initial (half-turn) rotation symmetry by choosing the uniqueposition just a half-turn away from the position of the previous move of player 1. If the value of rotsym is True when player 1 has to make a move, the symmetry is used by just considering therows up to the center of the board. (Source code fragment: “ (not rotsym or (r<=nm1div2)) ”).In addition, there are these further conditions (in order to reduce the set of positions to be checked):For the first move, the (then complete) symmetry is used.(Source code fragment: “ (c<=nm1div2) and (r<=c) ”).If n is odd, the above described strategy for the first player is used.If n is even and not greater than 8, the first player is forced to start at a certain of the four innerpositions. (Source code fragment: “(r=nm1div2) and (c=nm1div2)”).2ests have shown, that this restriction does not prevent the first player from winning (in that case)while significantly reducing the sum of moves performed during the search in the case n = 8.Finally, there is a special treatment of the case n = 16 by player 2, described in the followingsubsection. (Source code fragment:“ ((n<>16) or(n_moves>3) or(n_moves=3) and n16_player_2_round_2(prev_r,prev_c,r,c) or(n_moves=1) and n16_player_2_round_1(prev_r,prev_c,r,c)) ”). n = 16 In order to reduce the otherwise very long execution time, QPGAME3.PAS uses previously found andchecked moves of player 2 to treat the case n = 16 in a special way: Two routines ( n16 player 2 round 1 and n16 player 2 round 2 ) select unique moves of player 2 when he has to answer to the moves ofplayer 1 in the first two rounds.In n16 player 2 round 1 , the table T contains for each possible first move of player 1 a hexadecimallynoted byte value, where $FF indicates an invalid (unexpected) move of player 1 and each other valueencodes an answering move of player 2: The upper/lower half-byte (first/second hexadecimal) givesthe row/column number.Because the first move of player 1 is restricted to positions satisfying “ (c<=nm1div2) and (r<=c) ”,here “ (c<=7) and (r<=c) ”, just 36 move positions have to bo considered. In order to be able to usethe rotation symmetry, it has been tried to use (15-r0,15-c0) as often as possible. Clearly, this wasimpossible for the 8 positions on the diagonal (r0=c0). In these cases a position (r1,c1) is given suchthat “ (c1<=7) and (r1<=c1) ” and therefore two positions can and will be checked by one actualcheck. So, 28 actual checks have to be executed.By the way, for some non-diagonal positions (r0,c0) turned out that player 2 could not win bychoosing (15-r0,15-c0), for instance (1,2) and (6,7).In n16 player 2 round 2 , the situation is more complicated, because the answering second move ofplayer 2 depends not just on the second move of player 1 (third move of the game), but also on thefirst move of player 1 (first move of the game). Therefore, that first move has been stored beforein the global variables r0 and c0 and is used to address an index value in the table A . Here, thevalue 0 means, that the addressing position is invalid (by design); its use would cause a range checkerror and abort the execution. Otherwise, that value is the first index value to address an entry inthe table B . As the other two index values, the row and the column number of the second move ofplayer 1 (routine parameters ( r2 and c2 ) are used. The type, notation, meaning, and usage of theentries of B are as described above for the table T .Some of the (sub-)tables contain unused space or redundant information. In addition, the usage ofthe tables in a predicative (checking) instead of a functional (constructive) way is not optimal forminimal runtime. But the latter can be ignored because the selection of a second or fourth move ofa game is an extremely seldom event under the move selections performed during a whole case check(search) if n is not very little. And because the program is still small, also the waste of space is noreal problem here. The program does not use the heap or any pointer operation at all. If you don’t change the respectivecompiler directives, range checks and stack overflow checks will be generated. So the resultingexecutable will be extremely safe. It is also small and needs only a few kilobytes for the stack.3he program ignores any command line parameters or inputs other than pressing Ctrl-C to cancelthe execution - where the speed of response depends on the used compiler (slow in the case ofTurbo Pascal). It writes only to the standard output device and into the automatically created orrewritten, resp., file QPGAME.LST within the working directory. In the default case, the standardoutput device will be the monitor screen. But one could redirect that output (e.g. to a file). Inmost cases that will be unnecessary because the listing in QPGAME.LST is an essence of the datawritten to the standard output device. Just some execution state indicators are omitted. Even afterthe execution has been stopped by cancelling, the listing file should be readable and contain thedata written before the end of the execution. During the execution, its content is not accessible (byother processes).
As for the older program versions, a + will be emitted (written to the standard output device) aftereach 1000000 moves. Those characters (and corresponding new-lines) will not be written into thelisting file.When the check of a certain case (value of n ) begins or ends, an information line will be written (tothe standard output decice and into the listing file). QPGAME1, the program provided with the first version of this article, used a simple longint (signed32 bit integer) variable to count the calls (of the core routine / of a player to try to find a winningmove), then displayed as
Sum of moves , which is actually smaller (by one). Because the upper limitof that counter was 2 − ) could occur in realisticcases. Those overflows can not influence the search but mislead the user. So, the counter has beenredesigned in QPGAME2.PAS. Since then, it will not overflow below (2 − × > . × . In order to allow to give more detailed information about the state and subresults of the execution,especially in very extensive checks, now the generation of additional output can be advised:If the Boolean variable first move checking statistics does have the value
True , a subresult summaryline (containing the position of the first move (by player 1), the resulting outcome, and the sum ofcalls during the check of that first move) will be written to the standard output decice and into thelisting file.If the Boolean variable indicate third moves checking does have the value
True , the start and theend of the check of the situation established by a third move will be indicated (but not written tothe listing file). To be more concrete: On the start of that check, the positions of the first and thirdmove of the game (both by player 1) will be written to the standard output device. On the end ofthat check, the result of the routine wins (the sub-case success of player 2) will be indicated by adigit (0 means loss, 1 means win). Typically, there are some + symbols between these two outputs.An example (from the check of the case n = 16): [1:(0,0)] 3:(2,3)++++++++++++++++++++++++++++++++++++ -> 1 In the unchanged QPGAME3.PAS, “ n>=16 ” is the evaluating expression for both mentioned control variables.
In principle, the program is able to check the cases of n from 1 to 16 or 32 (depending on the compiler symbol BIGN , see below) without large changes. Because some mentioned cases were already solved mathematically nd checking the case n = 14 will take some time, in the published version only checks for even values of n from 6 to 12 are called from the main loop.In order to avoid a compilation result depending on the settings you could use the command line versions ofthe compilers (TPC for Turbo Pascal, BPC for Borland Pascal 7, DCC32 for Borland Delphi (32 bit versions;do not miss to use the -CC option in order to generate a console executable), VPC for Virtual Pascal, FPCfor Free Pascal) instead of the compilers integrated in the IDEs.In order to make the execution faster than that of QPGAME1 (provided with the first version of this article),alternative structure and usage of the variables containing the information on already used rows, columns,and diagonals as described in [4] have been implemented with QPGAME2. The program became indeedfaster, but because the new code will not work if n is greater than 16 and its function is not just thatobvious as that of the old code, the old code is still in the source file and can be used instead of the newcode by compiling with the symbol BIGN defined. For this purpose, the appropriate command line optionwould be -DBIGN for compilers from Borland (Turbo Pascal, Delphi), and -dBIGN for Free Pascal.The program has been successfully compiled and executed on a 1 GHz Intel PIII PC running MS Windows 98SE. These are the used compilers and the respective two execution times from compilations with defined/undefinedcompiler symbol BIGN:Turbo Pascal 5.5 : 1:44 min / 1:16 minTurbo Pascal 7.01 : 1:39 min / 0:41 minBorland Delphi 4.0 build 5.37 : 0:13 min / 0:11 minVirtual Pascal 2.1 build 279 : 0:18 min / 0:14 minFree Pascal 2.4.4 i386-Win32 : 0:15 min / 0:11 minHere is the content of QPGAME.LST after compiling the (unchanged) QPGAME3.PAS and running thegenerated QPGAME3.EXE: === Checking solutions for the queens placing game problem ====== Version 3 Copyright (c) 2014-04-17 Thomas Jenrich ===Hints:Output listing into file QPGAME.LST within the working directory.After each 1000000 moves a + will be emitted.To cancel the execution press Ctrl-C.Starting search with n = 6Search completed. Result of player 1: win. Sum of calls: 54Starting search with n = 8Search completed. Result of player 1: win. Sum of calls: 2266Starting search with n = 10Search completed. Result of player 1: loss. Sum of calls: 653007Starting search with n = 12Search completed. Result of player 1: loss. Sum of calls: 11334613== Regular program stop == After changing the upper limit of the main loop from 13 to 14, Free Pascal has compiled QPGAME3.PASagain. The execution of the resulting QPGAME3.EXE took 18:51 min. This is the (relevant part of the)additional output:
Starting search with n = 14Search completed. Result of player 1: loss. Sum of calls: 1161385667
After changing the lower limit and the upper limit of the main loop into 16, Free Pascal has compiledQPGAME3.PAS again. The execution of the resulting QPGAME3.EXE took 22:55:30 h. Parallel activities n that computer may have caused some delay, but probably not more than one hour.Here is the content of the generated QPGAME.LST: === Checking solutions for the queens placing game problem ====== Version 3 Copyright (c) 2014-04-17 Thomas Jenrich ===Hints:Output listing into file QPGAME.LST within the working directory.After each 1000000 moves a + will be emitted.To cancel the execution press Ctrl-C.Starting search with n = 16pl. 1: (0,0) -> pl. 2: win. Sum of calls: 4470810024pl. 1: (0,1) -> pl. 2: win. Sum of calls: 3905839444pl. 1: (0,2) -> pl. 2: win. Sum of calls: 3734401972pl. 1: (0,3) -> pl. 2: win. Sum of calls: 3184149898pl. 1: (0,4) -> pl. 2: win. Sum of calls: 4328139348pl. 1: (0,5) -> pl. 2: win. Sum of calls: 4425220446pl. 1: (0,6) -> pl. 2: win. Sum of calls: 2278166076pl. 1: (0,7) -> pl. 2: win. Sum of calls: 2676717214pl. 1: (1,2) -> pl. 2: win. Sum of calls: 3876758648pl. 1: (1,3) -> pl. 2: win. Sum of calls: 2063718676pl. 1: (1,4) -> pl. 2: win. Sum of calls: 2120404822pl. 1: (1,5) -> pl. 2: win. Sum of calls: 2114679020pl. 1: (1,7) -> pl. 2: win. Sum of calls: 2290066796pl. 1: (2,2) -> pl. 2: win. Sum of calls: 3360372268pl. 1: (2,3) -> pl. 2: win. Sum of calls: 1952040408pl. 1: (2,4) -> pl. 2: win. Sum of calls: 1993953094pl. 1: (2,5) -> pl. 2: win. Sum of calls: 1965208480pl. 1: (2,6) -> pl. 2: win. Sum of calls: 2088818438pl. 1: (2,7) -> pl. 2: win. Sum of calls: 2192529724pl. 1: (3,4) -> pl. 2: win. Sum of calls: 1719300574pl. 1: (3,5) -> pl. 2: win. Sum of calls: 1834911490pl. 1: (3,6) -> pl. 2: win. Sum of calls: 1981765458pl. 1: (3,7) -> pl. 2: win. Sum of calls: 1964816772pl. 1: (4,5) -> pl. 2: win. Sum of calls: 1671384528pl. 1: (4,6) -> pl. 2: win. Sum of calls: 1857679532pl. 1: (4,7) -> pl. 2: win. Sum of calls: 2007652010pl. 1: (5,6) -> pl. 2: win. Sum of calls: 1690434580pl. 1: (5,7) -> pl. 2: win. Sum of calls: 1712035496Search completed. Result of player 1: loss. Sum of calls: 71461975237== Regular program stop == References [1] Martin Erickson,
Some Two-Player Achievement Games ,http://mathacadabra.com/Items2013/TwoPlayerAchievementGames.aspx,retrieved 2013-11-27[2] Hassan A Noon,
SURREAL NUMBERS AND THE N-QUEENS GAME
3] Hassan Noon, Glen Van Brummelen,
The non-attacking queens game
Backtracking Algorithms in MCPL using Bit Patterns and Recursion
Turbo Pascal versions 1.0, 3.02, and 5.5 (binaries only)http://edn.embarcadero.com/museum/antiquesoftware
Virtual Pascal (Closed Source freeware)One ZIP-file including binaries and documentation for Win32, OS/2, and LinuxOfficial forum:http://vpascal.ning.com/Forum entry
Where can I download VP? :http://vpascal.ning.com/forum/topic/show?id=854411 %3ATopic%3A9 [7]
Free Pascal