mirror of
https://github.com/rfivet/BCG101.git
synced 2024-12-18 08:06:27 -05:00
81 lines
4.1 KiB
QBasic
81 lines
4.1 KiB
QBasic
1 ! BY JEFF DALTON, CLASS OF '74, NORTHFIELD MOUNT HERMON SCHOOL
|
|
2 ! THIS PROGRAM PLAYS THE GAME 'HEXAPAWN' BY A METHOD OUTLINED IN
|
|
'MATHEMATICAL GAMES' IN MARCH 1962 SCIENTIFIC AMERICAN.
|
|
3 ! THE PROGRAM LEARNS BY ELIMINATION OF BAD MOVES. ALL POSITIONS
|
|
ENCOUNTERED BY THE PROGRAM AND THE ACCEPTABLE MOVES FROM THEM
|
|
ARE STORED IN P$(I%)
|
|
4 ! WHEN THE PROGRAM ENCOUNTERS AN UNFAMILIAR POSITION, THE POSITION
|
|
AND ALL LEGAL MOVES FROM IT ARE ADDED TO THE LIST.
|
|
5 ! IF THE PROGRAM LOSES A GAME, IT ERASES THE MOVE THAT LED TO DEFEAT.
|
|
IF IT HITS A POSITION FROM WHICH ALL MOVES HAVE BEEN DELETED
|
|
( THEY ALL LED TO DEFEAT ), IT ERASES THE MOVE THAT GOT
|
|
IT HERE AND RESIGNS.
|
|
10 INPUT "INSTRUCTIONS"; C$: IF LEFT(C$,1%)<>"Y" THEN 110
|
|
20 &: &,"THIS PROGRAM PLAYS THE GAME OF HEXAPAWN.":&
|
|
"HEXAPAWN IS PLAYED WITH CHESS PAWNS ON A 3 BY 3 BOARD. THE PAWNS ARE":&
|
|
"MOVED AS IN CHESS - ONE SPACE FORWARD TO AN EMPTY SPACE OR ONE SPACE"
|
|
30 &"FORWARD AND DIAGONALLY TO CAPTURE AN OPPOSING MAN.":&:&CHR$(9%);
|
|
"O THE BOARD, YOUR PAWNS ARE 'O', THE COMPUTER'S PAWNS ARE '*'":&
|
|
"AND EMPTY SQUARES ARE '-'. TO ENTER A MOVE, TYPE THE NUMBER OF THE"
|
|
40 &"SQUARE YOU WILL MOVE FROM FOLLOWED BY THE NUMBER OF THE SQUARE":&
|
|
"YOU WILL MOVE TO. (THE NUMBERS ARE SEPARATED BY A COMMA.)":&
|
|
50 &" THE PROGRAM STARTS A SERIS OF GAMES KNOWING ONLY WHEN THE":&
|
|
"GAME IS WON (A DRAW IS IMPOSSIBLE) AND HOW TO MOVE. IT HAS NO":&
|
|
"STRATEGY AT FIRST AND JUST MOVES RANDOMLY. HOWEVER, IT LEARNS"
|
|
60 &"FROM EACH GAME. THUS, DEFEATING IT BECOMES MORE AND MORE":&
|
|
"DIFFICULT. ALSO, TO HELP OFFSET YOUR INITIAL ADVANTAGE, YOU WILL":&
|
|
"NOT BE TOLD HOW TO WIN THE GAME BUT MUST LEARN THIS BY PLAYING":&
|
|
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
|
|
110 DIM P$(50%): RANDOMIZE: Q%=0%:
|
|
PRINT "SINCE I'M A GOOD SPORT, YOU'LL ALWAYS GO FIRST
|
|
120 P%=0%: P$="***---OOO": &: &"NUMBERING:":
|
|
&"123": &"456": &"789": &
|
|
190 D%=-1%: Q$="O": GOSUB 2000: IF M$="" THEN
|
|
PRINT "YOU CAN'T MOVE. I WIN.": GOTO 510
|
|
200 &: &"BOARD:": &LEFT(P$,3%): &MID(P$,4%,3%): &RIGHT(P$,7%): &
|
|
210 INPUT "WHAT IS YOUR MOVE"; A%,B%:
|
|
IF INSTR(1%,M$,FNN$(A%)+FNN$(B%))=0% THEN
|
|
PRINT "ILLEGAL MOVE.": GOTO 210
|
|
230 P$=FNC$(FNC$(P$,A%,"-"),B%,"O"):
|
|
IF INSTR(1%,P$,"*")=0% OR INSTR(1%,P$,"O")<4% THEN
|
|
PRINT "YOU WIN.": GOTO 500
|
|
300 P%=P%+2% ! COMPUTER'S MOVE
|
|
310 FOR C%=1% TO Q%: C$=P$(C%): IF VAL(LEFT(C$,1%))=P% AND
|
|
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>.33333333 AND M$<>"" THEN 410
|
|
420 K%=C%: A%=VAL(LEFT(K$,1%)): B%=VAL(RIGHT(K$,2%)):
|
|
P$=FNC$(FNC$(P$,A%,"-"),B%,"*")
|
|
430 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
|
|
510 W1%=W1%+1%
|
|
520 &: &"BOARD:": &LEFT(P$,3%): &MID(P$,4%,3%): &RIGHT(P$,7%): &:
|
|
& "I HAVE WON" W1% "AND YOU HAVE WON" W2% "OF" W1%+W2% "GAMES"
|
|
530 INPUT "ANOTHER GAME"; C$: IF C$="NO" THEN 9999 ELSE 120
|
|
900 ! DATA**= <- OF MOVE IN GAME><POSITION><LIST OF MOVES>
|
|
IN <POSITION>, -=BLANK, *=COMPUTER'S PAWN, O=PLAYERS'S PAWN
|
|
<LIST OF MOVES> IS <- MOVE FROM><- MOVE TO><LIST OF MOVES>
|
|
2000 M$=""
|
|
2010 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
|
|
(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
|
|
2040 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$
|
|
2060 RETURN
|
|
9999 END
|