d2 ! THIS PROGRAM PLAYS THE GAME 'HEXAPAWN' BY A METHOD OUTLINED IS 3 ! 'MATHEMATICAL GAMES' IS MARCH 1962 SCIENTIFIC AMERICAN. ,4 ! THE PROGRAM LEARNS BY ELIMINATION OF BAD MOVES. ALL POSITIONS 5 ! ENCOUNTERED BY THE PROGRAM AND THE ACCEPTABLE MOVES FROM THEM 6 ! ARE STORED IN P$(I%) X7 ! WHEN THE PROGRAM ENCOUNTERS AN UNFAMILIAR POSITION, THE POSITION 8 ! AND ALL THE LEGAL MOVES ARE ADDED TO THE LIST. 9 ! IF THE PROGRAM LOSES A GAME, IT ERASES THE MOVE THAT LED TO DEFEAT. 10 ! IF IT HITS A POSITION FROM WHICH ALL MOVES HAVE BEEN DELETED 11 ! (THEY ALL LED TO DEFEAT), IT ERASES THE MOVE THAT GOT L12 ! IT HERE AND RESIGNS. 19 INPUT "INSTRUCTIONS"; C$ \ IF LEFT(C$,1%)<>"Y" THEN 110 20 PRINT \PRINT ,"THIS PROGRAM PLAYS THE GAME OF HEXAPAWN." x21 PRINT "HEXAPAWN IS PLAYED WITH CHESS PAWNS ON A 3 BY 3 BOARD. THE PAWNS ARE" 22 PRINT "MOVED AS IN CHESS - ONE SPACE FORWARD TO AN EMPTY SPACE OR ONE SPACE" @30 PRINT "FORWARD AND DIAGONALLY TO CAPTURE AN OPPOSING MAN."\PRINT \PRINT CHR$(9%); 31 PRINT "ON THE BOARD, YOUR PAWNS ARE 'O', THE COMPUTER'S PAWNS ARE '*'" 32 PRINT "AND EMPTY SQUARES ARE '-'. TO ENTER A MOVE, TYPE THE NUMBER OF THE " l40 PRINT "SQUARE YOU WILL MOVE FROM FOLLOWED BY THE NUMBER OF THE SQUARE" 41 PRINT "YOU WILL MOVE TO. (THE NUMBERS ARE SEPARATED BY A COMMA.)" 450 PRINT \ PRINT " THE PROGRAM STARTS A SERIES OF GAMES KNOWING ONLY WHEN THE" 51 PRINT "GAME IS WON (A DRAW IS IMPOSSIBLE) AND HOW TO MOVE. IT HAS NO" 52 PRINT "STRATEGY AT FIRST AND JUST MOVES RANDOMLY. HOWEVER, IT LEARNS" ` 60 PRINT "FROM EACH GAME. THUS, DEFEATING IT BECOMES MORE AND MORE" 61 PRINT "DIFFICULT. ALSO, TO HELP OFFSET YOUR INITIAL ADVANTAGE, YOU WILL" ( 62 PRINT "NOT BE TOLD HOW TO WIN THE GAME BUT MUST LEARN THIS BY PLAYING" Z 63 INPUT "TYPE 'GO' WHEN READY";C$ 100 DEF FNC$(X$,X%,Y$)=LEFT(X$,X%-1%)+Y$+RIGHT(X$,X%+LEN(Y$)) 105 DEF FNN$(X%)\ X$=NUM$(X%)\ FNN$=MID(X$,2%,LEN(X$)-2%)\ FNEND T 110 DIM P$(50%) \ RANDOMIZE \ Q%=0% 111 PRINT "SINCE I'M A GOOD SPORT, YOU'LL ALWAYS GO FIRST"  120 P%=0% \ P$="***---OOO" \ PRINT \ PRINT "NUMBERING:"\ 121 PRINT "123" \ PRINT "456" \ PRINT "789" \ PRINT 190 D%=-1% \ Q$="O" \ GOSUB 2000 H 191 IF M$="" THEN PRINT "YOU CAN'T MOVE. I WIN." \ GOTO 510 200 PRINT \ PRINT "BOARD:" \ PRINT LEFT(P$,3%) \ PRINT MID(P$,4%,3%) \ PRINT RIGHT(P$,7%) \ PRINT 210 INPUT "WHAT IS YOUR MOVE"; A%,B% t220 IF INSTR(1%,M$,FNN$(A%)+FNN$(B%))=0% THEN PRINT "ILLEGAL MOVE." \ GOTO 210 <230 P$=FNC$(FNC$(P$,A%,"-"),B%,"O") 231 IF INSTR(1%,P$,"*")=0% OR INSTR(1%,P$,"O")<4% THEN PRINT "YOU WIN." \ GOTO 500 h300 P%=P%+1% ! COMPUTER'S MOVE 310 FOR C%=1% TO Q% \ C$=P$(C%) \ IF VAL(LEFT(C$,1%))=P% AND & 0 MID(C$,2%,9%)=P$ THEN M$=RIGHT(C$,11%) \ GOTO 400 320 NEXT C% \ Q$="*" \ D%=1% \ GOSUB 2000 \ C%=Q% \ &  IF M$="" THEN PRINT"I CAN'T MOVE. YOU WIN." \ GOTO 500 \400 IF M$="" THEN PRINT "I RESIGN." \ GOTO 500 410 K$=LEFT(M$,2%) \ M$=RIGHT(M$,3%) \ & $ IF RND>.33333 AND M$<>"" THEN 410 420 K%=C% \ A%=VAL(LEFT(K$,1%)) \ B%=VAL(RIGHT(K$,2%)) \ &  P$=FNC$(FNC$(P$,A%,"-"),B%,"*") P430 IF INSTR(1%,P$,"O")=0% OR INSTR(7%,P$,"*") THEN &  PRINT "I WIN." \ GOTO 510 440 PRINT "I MOVE FROM";A%;" TO";B% \ GOTO 190 |500 W2%=W2%+1% \ I%=INSTR(11%,P$(K%),K$) \ &  P$(K%)=LEFT(P$(K%),I%-1%)+RIGHT(P$(K%),I%+2%) IF I% \ GOTO 520 D510 W1%=W1%+1% 520 PRINT \ PRINT "BOARD:" \ PRINT LEFT(P$,3%) \ PRINT MID(P$,4%,3%) \ PRINT RIGHT(P$,7%) \ PRINT \ &  PRINT "I HAVE WON";W1%;" AND YOU HAVE WON";W2%;" OF";W1%+W2%;" GAMES" p530 INPUT "ANOTHER GAME"; C$ \ IF C$="NO" THEN 9999 ELSE 120 900 ! DATA**= <- OF MOVE IN GAME> 8901 ! IN , -=BLANK, *=COMPUTER'S PAWN, O=PLAYER'S PAWN 902 ! IS <- MOVE FROM><- MOVE TO> 2000 M$="" d2010 FOR J%=1% TO 9% \ IF MID(P$,J%,1%)<>Q$ THEN 2050 2015 T%=J%+D%*3% \ IF T%<1% OR T%>9% THEN 2025 ,2020 IF MID(P$,T%,1%)="-" THEN &  M$=M$+FNN$(J%)+FNN$(T%) 2025 T%=J%+D%*2% \ IF T%<1% OR T%>9% OR & X (D%=1% AND (J%=1% OR J%=4% OR J%=7%)) OR &  (D%=-1% AND (J%=3% OR J%=6% OR J%=9%)) THEN 2035 2030 IF INSTR(1%,Q$+"-",MID(P$,T%,1%))=0% THEN &  M$=M$+FNN$(J%)+FNN$(T%) 2035 T%=J%+D%*4% \ IF T%<1% OR T%>9% OR J%=3% OR J%=7% THEN 2050 L2040 IF INSTR(1%,Q$+"-",MID(P$,T%,1%))=0% THEN &  M$=M$+FNN$(J%)+FNN$(T%) 2050 NEXT J% \ IF D%=1% AND M$<>"" THEN Q%=Q%+1% \ P$(Q%)=FNN$(P%)+P$+M$ x2060 RETURN 9999 END