home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l074 / 1.ddi / KNIGHT.TRU < prev    next >
Encoding:
Text File  |  1984-12-19  |  3.5 KB  |  111 lines

  1. ! Knight's tour
  2. !
  3. ! a True BASIC(tm), Inc. product
  4. !
  5. ! ABSTRACT
  6. !    Conducts a "Knight's Tour" of a chess board.
  7. !    A Knight starts at the lower left of the
  8. !    screen and selects legal moves, each move
  9. !    to an unused space, until the chessboard
  10. !    has no spaces available into which to move.
  11. !
  12. ! Copyright (c) 1985 by True BASIC, Inc.
  13.  
  14. SET mode "graphics"
  15. SET window -3,13,1,11
  16. DIM move(8,2)                     ! Legal moves
  17. DIM board(-1 to 10,-1 to 10)      ! Status of board
  18.  
  19. CALL initdraw                     ! Board and knight
  20. CALL initial                      ! Tables
  21. RANDOMIZE
  22.  
  23. LET x, y = 1                      ! Initial position
  24. FOR t = 2 to 64
  25.     CALL nextmove(x,y,newx,newy)  ! Find new position
  26.     BOX SHOW k0$ at x+.1,y+.1     ! Shadow knight to old
  27.     BOX SHOW k$ at newx+.1,newy+.1     ! Knight to new
  28.     PAUSE .2                      ! Otherwise too fast
  29.     LET x = newx                  ! Now the position
  30.     LET y = newy
  31. NEXT t
  32.  
  33. SUB initial                       ! Set up tables
  34.     MAT READ move                 ! Legal (x,y) changes
  35.     DATA 2,1,2,-1,1,2,1,-2,-1,2,-1,-2,-2,1,-2,-1
  36.  
  37.     FOR x = -1 to 10              ! Mark positions off board
  38.         FOR y = -1 to 0
  39.             LET board(x,y) = -1
  40.             LET board(y,x) = -1
  41.         NEXT y
  42.         FOR y = 9 to 10
  43.             LET board(x,y) = -1
  44.             LET board(y,x) = -1
  45.         NEXT y
  46.     NEXT x
  47.  
  48.     FOR x = 1 to 8                ! Moves possible from here
  49.         FOR y = 1 to 8
  50.             LET count = 0
  51.             FOR m = 1 to 8        ! Try 8 legal possibilities
  52.                 LET x1 = x+move(m,1)
  53.                 LET y1 = y+move(m,2)
  54.                 IF board(x1,y1)>=0 then LET count = count + 1   ! On board
  55.             NEXT m
  56.             LET board(x,y) = count     ! Possible moves
  57.         NEXT y
  58.     NEXT x
  59. END SUB
  60.  
  61. SUB nextmove(x,y,x2,y2)           ! Find hardest to get to
  62.     DIM moves(8,2)                ! Optimal moves
  63.     LET board(x,y) = -1           ! Can't move there anymore
  64.     LET bmin = 9                  ! For minimum calculation
  65.     FOR m = 1 to 8                ! 8 possibles
  66.         LET x1 = x+move(m,1)
  67.         LET y1 = y+move(m,2)
  68.         LET b = board(x1,y1) - 1  ! Now one less possible move
  69.         LET board(x1,y1) = b
  70.         IF b >= 0 and b<bmin then      ! Possible move, new low
  71.            LET bmin = b
  72.            LET nmove = 1
  73.            LET moves(nmove,1) = x1
  74.            LET moves(nmove,2) = y1
  75.         ELSEIF b>=0 and b = bmin then     ! Tie
  76.            LET nmove = nmove + 1
  77.            LET moves(nmove,1) = x1
  78.            LET moves(nmove,2) = y1
  79.         END IF
  80.     NEXT m
  81.     LET nm = int(nmove*rnd+1)             ! Pick at random
  82.     LET x2 = moves(nm,1)
  83.     LET y2 = moves(nm,2)
  84. END SUB
  85.  
  86. SUB initdraw                      ! Draw board and knight
  87.     SET cursor 1,15               ! Title
  88.     PRINT "Knight's Tour"
  89.  
  90.     FOR x = 1 to 9                ! Board
  91.         PLOT x,1;x,9
  92.         PLOT 1,x;9,x
  93.     NEXT x
  94.  
  95.     SET color "green"             ! Outline of knight
  96.     DRAW knight with shift(1,1)
  97.     BOX KEEP 1.1,1.9,1.1,1.9 in k0$    ! Save for show
  98.     SET color "red"               ! Red knight
  99.     FLOOD 1.5,1.5
  100.     BOX KEEP 1.1,1.9,1.1,1.9 in k$     ! Save it also
  101. END SUB
  102.  
  103. PICTURE knight                    ! Picture of knight
  104.     PLOT .2,.1;.8,.1;.8,.2;
  105.     PLOT .7,.25;.7,.3;.8,.4;.65,.7;.6,.9;.55,.9;
  106.     PLOT .5,.82;.2,.75;.2,.6;.3,.6;.4,.55;
  107.     PLOT .25,.45;.2,.37;.3,.3;.3,.25;.2,.2;.2,.1
  108. END PICTURE
  109.  
  110. END
  111.