DECLARE FUNCTION AlphaBeta%
(player%
, board%
(), achievable%
, cutoff%
, ply%
) DECLARE SUB MakeFlips
(move%
, player%
, board%
(), dir%
) DATA -11,-10,-9,-1,1,9,10,11
DATA 0,120,-20,20,5,5,20,-20,120,0 DATA 0,-20,-0,-5,-5,-5,-5,-40,-20,0 DATA 0,20,-5,15,3,3,15,-5,20,0 DATA 0,5,-5,3,3,3,3,-5,5,0 DATA 0,5,-5,3,3,3,3,-5,5,0 DATA 0,20,-5,15,3,3,15,-5,20,0 DATA 0,-20,-0,-5,-5,-5,-5,-40,-20,0 DATA 0,120,-20,20,5,5,20,-20,120,0
CONST SquareWidth
= ScreenHeight
/ 8 CONST tlx
= (ScreenWidth
- ScreenHeight
) / 2 DIM SHARED brx: brx
= ScreenWidth
- (ScreenWidth
- ScreenHeight
) / 2 'here CONST doesn't like to compiler CONST PieceRadius
= SquareWidth
/ 2 - 5 CONST WinningValue
= 32767 CONST LosingValue
= -32767
ax = 0
bx = 0
cx = 0
dx = 0
player = BLACK
human = BLACK
computer = Opponent(human)
n = GetMove
IF LegalP
(n
, player
, board
()) THEN CALL MakeMove
(n
, player
, board
()) player = NextToPlay(board(), player)
n = AlphaBeta(player, board(), LosingValue, WinningValue, nply)
move = bestm(nply)
CALL MakeMove
(move
, player
, board
()) player = NextToPlay(board(), player)
FUNCTION AlphaBeta
(player
, board
(), achievable
, cutoff
, ply
) board2(i) = board(i)
AlphaBeta = WeightedSquares(player, board())
nlegal = 0
IF LegalP
(move
, player
, board
()) THEN nlegal = nlegal + 1
CALL MakeMove
(move
, player
, board2
()) value = -AlphaBeta(Opponent(player), board2(), -cutoff, -achievable, ply - 1)
achievable = value
bestmove = move
IF AnyLegalMove
(Opponent
(player
), board
()) THEN AlphaBeta = -AlphaBeta(Opponent(player), board(), -cutoff, -achievable, ply - 1)
AlphaBeta = FinalValue(player, board())
bestm(ply) = bestmove
Colour = 0
Colour = 15
FUNCTION CountDifference
(player
, board
()) c = 0
IF board
(10 * y
+ x
) = player
THEN c
= c
+ 1 IF board
(10 * y
+ x
) = Opponent
(player
) THEN c
= c
- 1 CountDifference = c
FinalValue = LosingValue
FinalValue = 0
FinalValue = WinningValue
FUNCTION FindBracketingPiece
(square
, player
, board
(), dir
) IF board
(square
) = player
THEN FindBracketingPiece = square
FindBracketingPiece = FindBracketingPiece(square + dir, player, board(), dir)
y = (dx - tly) \ SquareWidth + 1
x = (cx - tlx) \ SquareWidth + 1
GetMove = 10 * y + x
board(i) = OUTER
board(90 + i) = OUTER
board(i * 10) = OUTER
board(i * 10 + 9) = OUTER
board(44) = 1
board(45) = 2
board(54) = 2
board(55) = 1
x = WouldFlip(move, player, board(), AllDirections(i))
SUB MakeFlips
(move
, player
, board
(), dir
) bracketer = WouldFlip(move, player, board(), dir)
board(c) = player
SUB MakeMove
(move
, player
, board
()) board(move) = player
CALL MakeFlips
(move
, player
, board
(), AllDirections
(i
))
FUNCTION MaximizeDifference
(player
, board
()) best = -9999
move = 10 * y + x
IF LegalP
(move
, player
, board
()) THEN board2(i) = board(i)
CALL MakeMove
(move
, player
, board2
()) score = WeightedSquares(player, board2())
IF score
> best
THEN best
= score: bestmove
= move
MaximizeDifference = bestmove
ml$ = "" ' -=<( Mouse Code )>=-
ml$
= ml$
+ CHR$(&H55) ' push bp ; preserve BP register ml$
= ml$
+ CHR$(&H89) + CHR$(&HE5) ' mov bp, sp ; copy SP to BP ml$
= ml$
+ CHR$(&HB8) + CHR$(ax
) + CHR$(&H0) ' mov ax, # ; copy SUBFUNCTION to AX ml$
= ml$
+ CHR$(&HCD) + CHR$(&H33) ' int 33 ; call mouse interrupt ml$
= ml$
+ CHR$(&H53) ' push bx ; preserve BX (again) ml$
= ml$
+ CHR$(&H8B) + CHR$(&H5E) + CHR$(&H6) ' mov bx, [bp+6] ; copy location of dx (last variable) to BX ml$
= ml$
+ CHR$(&H89) + CHR$(&H17) ' mov [bx], dx ; copy DX to dx location in BX ml$
= ml$
+ CHR$(&H8B) + CHR$(&H5E) + CHR$(&H8) ' mov bx, [bp+8] ; copy location of cx to BX ml$
= ml$
+ CHR$(&H89) + CHR$(&HF) ' mov [bx], cx ; copy CX to cx location in BX ml$
= ml$
+ CHR$(&H8B) + CHR$(&H5E) + CHR$(&HC) ' mov bx, [bp+C] ; copy location of ax to BX ml$
= ml$
+ CHR$(&H89) + CHR$(&HF7) ' mov [bx], ax ; copy AX to ax location in BX ml$
= ml$
+ CHR$(&H8B) + CHR$(&H5E) + CHR$(&HA) ' mov bx, [bp+A] ; copy location of bx to BX ml$
= ml$
+ CHR$(&H58) ' pop ax ; restore int 33's BX value to AX ml$
= ml$
+ CHR$(&H89) + CHR$(&H7) ' mov [bx], ax ; copy AX to bx location in BX ml$
= ml$
+ CHR$(&H5D) ' pop bp ; restore BP ml$
= ml$
+ CHR$(&HCA) + CHR$(&H8) + CHR$(&H0) ' retf 8 ; Return Far and skip 8 bytes of variables DEF SEG = VARSEG(ml$
) ' Set current segment this machine code segment 'here is SSEG offset%
= SADD(ml$
) ' Set offset to this machine code location CALL ABSOLUTE(ax
, bx
, cx
, dx
, offset%
) ' The actual call to this machine code DEF SEG ' Restore the default segment
FUNCTION NextToPlay
(board
(), PreviousPlayer
) opp = Opponent(PreviousPlayer)
IF AnyLegalMove
(PreviousPlayer
, board
()) THEN NextToPlay
= PreviousPlayer
IF player
= WHITE
THEN Opponent
= BLACK
IF player
= BLACK
THEN Opponent
= WHITE
LINE (tlx
, tly
)-(brx
, bry
), 8, BF
LINE (tlx
+ SquareWidth
* i
, tly
)-(tlx
+ SquareWidth
* i
, bry
) LINE (tlx
, tly
+ SquareWidth
* i
)-(brx
, tly
+ SquareWidth
* i
) IF board
(10 * y
+ x
) <> EMPTY
THEN CIRCLE (tlx
+ SquareWidth
* x
- SquareWidth
/ 2, tly
+ SquareWidth
* y
- SquareWidth
/ 2), PieceRadius
, Colour
(board
(10 * y
+ x
)) PAINT (tlx
+ SquareWidth
* x
- SquareWidth
/ 2, tly
+ SquareWidth
* y
- SquareWidth
/ 2), Colour
(board
(10 * y
+ x
))
FUNCTION WeightedSquares
(player
, board
()) opp = Opponent(player)
sum = 0
IF board
(i
) = player
THEN sum
= sum
+ weights
(i
) IF board
(i
) = opp
THEN sum
= sum
- weights
(i
) WeightedSquares = sum
FUNCTION WouldFlip
(move
, player
, board
(), dir
) c = move + dir
WouldFlip = FindBracketingPiece(c + dir, player, board(), dir)