home *** CD-ROM | disk | FTP | other *** search
/ Big Blue Disk 22 / bbd22.zip / QUEENS-D.CHN (.txt) < prev    next >
Turbo Pascal Chain module  |  1988-05-10  |  12KB  |  90 lines

  1. Bad variable number.
  2. C               
  3.          
  4. C               
  5.          
  6.      
  7.      
  8. C               
  9. C               
  10.          
  11.      
  12.      
  13.      
  14. C               
  15.          
  16. Can you place eight  
  17. queens on a chess    
  18. board so that no two 
  19. attack each other?   
  20. Almost!
  21.  Press the spacebar to continue! 
  22. Positions Tested: 
  23.       Queens Set: 
  24.  END OF DEMO. ! 
  25. return.chn
  26. DEMO of QUEENS.PAS
  27. )Enter a speed from 1 to 9 (1=slowest),   
  28. !or 0 to single step thru program 
  29. *Choose a display mode: Full or Overview.  
  30. Enter F or O 
  31.  Press spacebar to continue... 
  32. 0SPACE=continue, ESC=quit, 0-9 to change speed,  
  33. + to change mode,  
  34.  to scroll
  35. InRow
  36. InDiag1
  37. InDiag2
  38. QueenPos
  39. Success
  40. "{QUEENS.PAS = program to solve the
  41. + 8-queens problem  by John Sigle  1/21/88 }
  42. %PROGRAM EightQueens( Input, Output );
  43. VAR K: INTEGER;
  44.     Success: Boolean;
  45. "    InRow: ARRAY[1..8] OF BOOLEAN;
  46. *    InDiag1: ARRAY[2..16] OF BOOLEAN;  {/}
  47. *    InDiag2: ARRAY[-7..7] OF BOOLEAN;  {\}
  48. %    QueenPos: ARRAY[1..8] OF INTEGER;
  49. PROCEDURE Try( Col: INTEGER );
  50. VAR Row: INTEGER;
  51. ,BEGIN { Try to place a queen in column Col }
  52.   Row := 0;
  53.   REPEAT   { Try next row }
  54.     Row := Row+1;
  55.     Success := FALSE;
  56.     { Test position }
  57. )    IF NOT(InRow[Row] OR InDiag1[Col+Row]
  58. #        OR InDiag2[Col-Row])   THEN
  59.       BEGIN
  60.         { Set the queen }
  61.         QueenPos[Col] := Row;
  62.         InRow[Row] := TRUE;
  63. !        InDiag1[Col+Row] := TRUE;
  64. !        InDiag2[Col-Row] := TRUE;
  65.         IF Col<8 THEN
  66. (          BEGIN { Try to place another }
  67.             Try( Col+1 );
  68.             IF NOT Success THEN
  69. *              BEGIN  { Remove this queen }
  70. #                QueenPos[Col] := 0;
  71. $                InRow[Row] := FALSE;
  72. *                InDiag1[Col+Row] := FALSE;
  73. )                InDiag2[Col-Row] := FALSE
  74.               END
  75.           END
  76.         ELSE Success := TRUE
  77.           END
  78.   UNTIL Success OR (Row=8)
  79. END;   { of Try }
  80. BEGIN    { Main program }
  81.   { Initialize indicators }
  82. )  FOR K := 1 TO 8 DO   InRow[K] := FALSE;
  83. +  FOR K := 2 TO 16 DO  InDiag1[K] := FALSE;
  84. +  FOR K := -7 TO 7 DO  InDiag2[K] := FALSE;
  85. (  FOR K := 1 TO 8 DO   QueenPos[K] := 0;
  86.   { Try to find a solution }
  87.   TRY( 1 );
  88. END.  { of EightQueens }
  89. Bad variable number.
  90.