Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: 8 Queens Solution

Author: David Mitchell

Date: 13:25:39 07/02/04

Go up one level in this thread


On July 02, 2004 at 15:58:03, Matt Liegel wrote:

>Anyone remember the long if() statement that solved the 8 Queens problem?
>
>Thanks
>Matt

A more "integrated" approach, I think, from Weiner's Quick/Q BASIC book, iirc:


DECLARE SUB Fillboard (X%, Y%)
DECLARE FUNCTION Randomint% ()

COLOR 14, 1
CLS
DEFINT A-Z
DIM Chessboard(1 TO 8, 1 TO 8)

CONST Queen = 81
CONST Star = 42
D! = 1

RANDOMIZE TIMER
DO
  ERASE Chessboard
  Sqcnt = 0
  Qncnt! = 0

  DO WHILE Sqcnt < 64
    Row = Randomint: Cl = Randomint
    IF Chessboard(Row, Cl) = 0 THEN CALL Fillboard(Row, Cl)
  LOOP

  LOCATE 25, 1: PRINT "Finished Try#"; D!;
  D! = D! + 1

LOOP UNTIL Qncnt! = 8

'print the chessboard

LOCATE 25, 1
BEEP
PRINT SPACE$(30); "Success on Try#"; D! - 1

FOR I = 1 TO 8
  LOCATE (I + 7), 20
  FOR J = 1 TO 8
    PRINT CHR$(Chessboard(I, J)); SPC(4);
  NEXT J
  PRINT
NEXT I
END

SUB Fillboard (X, Y)
SHARED Chessboard(), Sqcnt, Qncnt!

'local variables are:Rw,Cl,I,J

FOR I = -1 TO 1
  FOR J = -1 TO 1

  Rw = X
  Cl = Y
  IF I <> 0 OR J <> 0 THEN
    DO WHILE Rw > 0 AND Rw < 9 AND Cl > 0 AND Cl < 9
      IF Chessboard(Rw, Cl) = 0 THEN
        Chessboard(Rw, Cl) = Star
        Sqcnt = Sqcnt + 1
      END IF
      Rw = Rw + I
      Cl = Cl + J
    LOOP
  END IF
  NEXT J
NEXT I

Chessboard(X, Y) = Queen
Qncnt! = Qncnt! + 1

END SUB

FUNCTION Randomint
  Randomint = INT(8 * RND(1)) + 1
END FUNCTION

Dave



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.