'===========================================================================
' Subject: SOLVING THE KNIGHT PROBLEM         Date: 10-18-97 (23:40)       
'  Author: Timothy J. Mayo                    Code: QB, QBasic, PDS        
'  Origin: tjmayo@spnt.com                  Packet: ALGOR.ABC
'===========================================================================
DECLARE SUB newbreed ()
DECLARE SUB displaybest ()
''--------PROGRAM QBASIC--------''
''this product was made designed
''and programed by A.D. Ice
''14 years of age if you want
''to use my code please give
''me some credit for my code
''if you want to compile and sell
''my program (edited of course)
''"""""GO FOR IT""""""
''just give me SOME credit
''E-mail <tjmayo@spnt.com>

''This program is desined as a way to solve the famous knight problem
''the knight problem is were the knight is placed on the chess board
''and the object of the rook is to move through each square on the board
'':------:------:------:------:------:------:------:------:
'':xxxxxx:      :      :      :      :      :      :      :
'':------:------:------:------:------:------:------:------:
'':      :      :      :      :      :      :      :      :
'':------:------:------:------:------:------:------:------:
'':      :      :      :      :      :      :      :      :
'':------:------:------:------:------:------:------:------:
'':      :      :      :      :      :      :      :      :
'':------:------:------:------:------:------:------:------:
'':      :      :      :      :      :      :      :      :
'':------:------:------:------:------:------:------:------:
'':      :      :      :      :      :      :      :      :
'':------:------:------:------:------:------:------:------:
'':      :      :      :      :      :      :      :      :
'':------:------:------:------:------:------:------:------:
'':      :      :      :      :      :      :      :      :
'':------:------:------:------:------:------:------:------:
''useing the genetic alogarithem approach the knight LEARNS to go in the right way
''in practice useing this technique it could be possible to evolve a computer program
''the list of letters at the end of this program is the ANSWER to the program this is
''how you can decipher this program is as follows
''
''a: move up 2 left 1
''b: move up 2 right 1
''c: move up 1 left 2
''d: move up 1 right 2
''e: move down 2 right 1
''f: move down 2 left 1
''g: move down 1 right 2
''h: move down 1 left 2
''if you have question or just want to say hi you can e-mail me at
'' tjmayo <tjmayo@spnt.com> subject PROGRAMING A.D.
DECLARE SUB setup ()
DECLARE SUB move ()
SCREEN 12
RANDOMIZE TIMER
DIM SHARED board(8, 8, 201)
DIM SHARED x(201)
DIM SHARED y(201)
DIM SHARED genes$(201)
DIM SHARED value(201)
DIM SHARED count2
CALL setup
1 FOR count = 0 TO 201
FOR count2 = 1 TO 64
CALL move
num = num + 1
value(count) = num
IF ended = 1 THEN GOTO 11
IF ended = 0 THEN GOTO 30
NEXT
11 ended = 0
num = 0
NEXT
CALL newbreed
GOTO 1
30 CALL displaybest

SUB displaybest
PRINT genes$(count)
x(count) = 0
y(count) = 0
FOR count = 0 TO 64

IF MID$(genes$(count), count2, 1) = "A" THEN
 x(count) = x(count) - 1
 y(count) = y(count) - 2
END IF
IF MID$(genes$(count), count2, 1) = "B" THEN
 x(count) = x(count) + 1
 y(count) = y(count) - 2
END IF

IF MID$(genes$(count), count2, 1) = "C" THEN
 x(count) = x(count) - 2
 y(count) = y(count) - 1
END IF

IF MID$(genes$(count), count2, 1) = "D" THEN
 x(count) = x(count) + 2
 y(count) = y(count) - 1
END IF

IF MID$(genes$(count), count2, 1) = "E" THEN
 x(count) = x(count) + 1
 y(count) = y(count) + 2
END IF

IF MID$(genes$(count), count2, 1) = "F" THEN
 x(count) = x(count) - 1
 y(count) = y(count) + 2
END IF

IF MID$(genes$(count), count2, 1) = "G" THEN
 x(count) = x(count) + 2
 y(count) = y(count) + 1
END IF

IF MID$(genes$(count), count2, 1) = "H" THEN
 x(count) = x(count) - 2
 y(count) = y(count) + 1
END IF
NEXT
END SUB

SUB move
IF MID$(genes$(count), count2, 1) = "A" THEN
 x(count) = x(count) - 1
 y(count) = y(count) - 2
  IF x(count) < 0 OR y(count) < 0 OR x(count) > 64 OR y(count) > 64 THEN
  ended = 1
  GOTO 10
  END IF
  board(x(count), y(count), count) = board(x(count), y(count), count) + 1
   IF board(x(count), y(count), count) >= 2 THEN
   ended = 1
   GOTO 10
   END IF
END IF

IF MID$(genes$(count), count2, 1) = "B" THEN
 x(count) = x(count) + 1
 y(count) = y(count) - 2
  IF x(count) < 0 OR y(count) < 0 OR x(count) > 64 OR y(count) > 64 THEN
  ended = 1
  GOTO 10
  END IF
  board(x(count), y(count), count) = board(x(count), y(count), count) + 1
   IF board(x(count), y(count), count) >= 2 THEN
   ended = 1
   GOTO 10
   END IF
END IF

IF MID$(genes$(count), count2, 1) = "C" THEN
 x(count) = x(count) - 2
 y(count) = y(count) - 1
  IF x(count) < 0 OR y(count) < 0 OR x(count) > 64 OR y(count) > 64 THEN
  ended = 1
  GOTO 10
  END IF
  board(x(count), y(count), count) = board(x(count), y(count), count) + 1
   IF board(x(count), y(count), count) >= 2 THEN
   ended = 1
   GOTO 10
   END IF
END IF

IF MID$(genes$(count), count2, 1) = "D" THEN
 x(count) = x(count) + 2
 y(count) = y(count) - 1
  IF x(count) < 0 OR y(count) < 0 OR x(count) > 64 OR y(count) > 64 THEN
  ended = 1
  GOTO 10
  END IF
  board(x(count), y(count), count) = board(x(count), y(count), count) + 1
   IF board(x(count), y(count), count) >= 2 THEN
   ended = 1
   GOTO 10
   END IF
END IF

IF MID$(genes$(count), count2, 1) = "E" THEN
 x(count) = x(count) + 1
 y(count) = y(count) + 2
  IF x(count) < 0 OR y(count) < 0 OR x(count) > 64 OR y(count) > 64 THEN
  ended = 1
  GOTO 10
  END IF
  board(x(count), y(count), count) = board(x(count), y(count), count) + 1
   IF board(x(count), y(count), count) >= 2 THEN
   ended = 1
   GOTO 10
   END IF
END IF

IF MID$(genes$(count), count2, 1) = "F" THEN
 x(count) = x(count) - 1
 y(count) = y(count) + 2
  IF x(count) < 0 OR y(count) < 0 OR x(count) > 64 OR y(count) > 64 THEN
  ended = 1
  GOTO 10
  END IF
  board(x(count), y(count), count) = board(x(count), y(count), count) + 1
   IF board(x(count), y(count), count) >= 2 THEN
   ended = 1
   GOTO 10
   END IF
END IF

IF MID$(genes$(count), count2, 1) = "G" THEN
 x(count) = x(count) + 2
 y(count) = y(count) + 1
 IF x(count) < 0 OR y(count) < 0 OR x(count) > 64 OR y(count) > 64 THEN
  ended = 1
  GOTO 10
  END IF
  board(x(count), y(count), count) = board(x(count), y(count), count) + 1
   IF board(x(count), y(count), count) >= 2 THEN
   ended = 1
   GOTO 10
   END IF
END IF

IF MID$(genes$(count), count2, 1) = "H" THEN
 x(count) = x(count) - 2
 y(count) = y(count) + 1
 IF x(count) < 0 OR y(count) < 0 OR x(count) > 64 OR y(count) > 64 THEN
  ended = 1
  GOTO 10
  END IF
  board(x(count), y(count), count) = board(x(count), y(count), count) + 1
   IF board(x(count), y(count), count) >= 2 THEN
   ended = 1
   GOTO 10
   END IF
END IF

10 END SUB

SUB newbreed
best1 = 0
bestnum1 = 0
best2 = 0
bestnum2 = 0
FOR count3 = 0 TO 201
IF value(count3) > best1 THEN bestnum1 = count3
IF bestnum1 <> count3 AND value(count3) > best2 THEN bestnum1 = count3
NEXT count3
FOR count3 = 0 TO 201
IF bestnum1 <> count3 OR bestnum2 <> count3 THEN
value(count3) = 0
genes$(count3) = ""
x(count3) = 0
y(count3) = 0
END IF
NEXT count3
FOR count3 = 0 TO 8
FOR count4 = 0 TO 8
FOR count5 = 0 TO 201
board(count3, count4, count5) = 0
NEXT count5, count4, count3
FOR count3 = 0 TO 201
rand = INT(RND * 63) + 1
IF genes$(count3) = "" THEN genes$(count3) = LEFT$(genes$(bestnum1), rand) + RIGHT$(genes$(bestnum2), rand)
NEXT count3
END SUB

SUB setup
PRINT
PRINT "PRESS ESC TO CONTINUE"
DO UNTIL INKEY$ = CHR$(27)
LOOP
CLS
FOR count1 = 0 TO 201
FOR count = 0 TO 64
genes$(count1) = genes$(count1) + CHR$(INT(RND * 7) + 65)
NEXT
board(0, 0, count1) = 1
NEXT
END SUB
