home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l076 / 1.ddi / KNIGHT.TRU < prev    next >
Encoding:
Text File  |  1988-08-27  |  3.0 KB  |  97 lines

  1. !  Knight's tour.
  2. !
  3. SET window -3,13,0,11
  4. DIM move(8,2)                     ! Legal moves
  5. DIM board(-1 to 10,-1 to 10)      ! Board status
  6.  
  7. CALL initdraw                     ! Board & knight
  8. CALL initial                      ! Tables
  9. RANDOMIZE
  10.  
  11. LET x, y = 1                      ! Initial position
  12. FOR t = 2 to 64
  13.     CALL nextmove(x,y,newx,newy)  ! Find new position
  14.     BOX SHOW k0$ at x+.1,y+.1     ! Shadow knight to old
  15.     BOX SHOW k$ at newx+.1,newy+.1     ! Knight to new
  16.     PAUSE .2                      ! Otherwise too fast
  17.     LET x = newx                  ! Now the position
  18.     LET y = newy
  19. NEXT t
  20.  
  21. SUB initial         ! Set up tables
  22.     MAT READ move                 ! Legal (x,y) changes
  23.     DATA 2,1,2,-1,1,2,1,-2,-1,2,-1,-2,-2,1,-2,-1
  24.  
  25.     FOR x = -1 to 10              ! Mark positions off board
  26.         FOR y = -1 to 0
  27.             LET board(x,y), board(y,x) = -1
  28.         NEXT y
  29.         FOR y = 9 to 10
  30.             LET board(x,y), board(y,x) = -1
  31.         NEXT y
  32.     NEXT x
  33.  
  34.     FOR x = 1 to 8                ! Moves possible from here
  35.         FOR y = 1 to 8
  36.             LET count = 0
  37.             FOR m = 1 to 8        ! Try 8 legal possibilities
  38.                 LET x1 = x+move(m,1)
  39.                 LET y1 = y+move(m,2)
  40.                 IF board(x1,y1)>=0 then LET count = count + 1   ! On board
  41.             NEXT m
  42.             LET board(x,y) = count     ! Possible moves
  43.         NEXT y
  44.     NEXT x
  45. END SUB
  46.  
  47. SUB nextmove(x,y,x2,y2)           ! Find hardest to get to
  48.     DIM opt(8,2)                ! Optimal moves
  49.     LET board(x,y) = -1           ! Can't move there anymore
  50.     LET bmin = 9                  ! For minimum calculation
  51.     FOR m = 1 to 8                ! 8 possibles
  52.         LET x1 = x+move(m,1)
  53.         LET y1 = y+move(m,2)
  54.         LET b = board(x1,y1) - 1  ! Now one less possible move
  55.         LET board(x1,y1) = b
  56.         IF b >= 0 and b<bmin then ! Possible move, new low
  57.            LET bmin = b
  58.            LET new = 1
  59.            LET opt(new,1) = x1
  60.            LET opt(new,2) = y1
  61.         ELSEIF b>=0 and b = bmin then  ! Tie
  62.            LET new = new + 1
  63.            LET opt(new,1) = x1
  64.            LET opt(new,2) = y1
  65.         END IF
  66.     NEXT m
  67.     LET nm = int(new*rnd+1)      ! Pick at random
  68.     LET x2 = opt(nm,1)
  69.     LET y2 = opt(nm,2)
  70. END SUB
  71.  
  72. SUB initdraw   ! Draw board and knight
  73.     SET TEXT JUSTIFY "center","base"
  74.     PLOT TEXT, at 5,9.5: "Knight's Tour"
  75.  
  76.     FOR x = 1 to 9                ! Board
  77.         PLOT x,1;x,9
  78.         PLOT 1,x;9,x
  79.     NEXT x
  80.  
  81.     SET color "yellow"             ! Outline of knight
  82.     DRAW knight with shift(1,1)
  83.     BOX KEEP 1.1,1.9,1.1,1.9 in k0$    ! Save for show
  84.     SET color "red"               ! Red knight
  85.     FLOOD 1.5,1.5
  86.     BOX KEEP 1.1,1.9,1.1,1.9 in k$     ! Save it also
  87. END SUB
  88.  
  89. PICTURE knight    ! Picture of knight
  90.     PLOT .2,.1;.8,.1;.8,.2;
  91.     PLOT .7,.25;.7,.3;.8,.4;.65,.7;.6,.9;.55,.9;
  92.     PLOT .5,.82;.2,.75;.2,.6;.3,.6;.4,.55;
  93.     PLOT .25,.45;.2,.37;.3,.3;.3,.25;.2,.2;.2,.1
  94. END PICTURE
  95.  
  96. END
  97.