zxGammon

play.asm

The title and menu, games and matches, the player's and Gnubby's turns, the doubling cube, the scores. Download · All the source

; ----------------------------------------------------------------- play; The games: the title and menu, a match or money session, and each; game's turns, the cube, and the scores.start:  di        ld sp,STACK        xor a        out ($fe),a        ld hl,G                 ; the game's RAM (where BASIC was)        ld de,G+1        ld bc,$2ff        ld (hl),a        ldir        call init        ld hl,1        ld (SEED),hl        ; the loading screen a while longer, or until a key        xor a                   ; K_ANY        ld (KSET),a        inc a        ld (KWAIT),a        ld b,200.hold:  push bc        call kscan        pop bc        or a        jr nz,title        push bc        ld b,1        call pause        pop bc        djnz .hold; The title, and a choice of game.title:  ld sp,STACK        xor a        ld (KWAIT),a        ld (SAID),a        ld (UP),a               ; the board to be set up again        call cls        ld a,$42        ld (TATTR),a        ld hl,S_TITLE        ld bc,2*256+8        call bigtext        ld a,1        ld (MOOD),a        ld bc,6*256+14        call mascot        ld a,$45        ld (TATTR),a        ld hl,S_WITH        ld bc,11*256+9        call print        ld a,$46        ld (TATTR),a        ld hl,S_MENU        ld bc,13*256+8        call print        ld a,$05        ld (TATTR),a        ld hl,S_FOOT        ld bc,23*256+4        call print.k:     ld a,K_TITLE        ; M for money, else the match's length        call getk        ld b,0        cp 'M'        jr z,.len        sub '0'        ld b,a        cp 8        jr nc,.k        and 1                   ; 1, 3, 5 or 7        jr z,.k.len:   ld a,b        ld (MLEN),a        ld hl,0        ld (SCH),hl        ld (SCC),hl        xor a        ld (CRAW),a; A game.newgame:        ld sp,STACK        call lgame        ld hl,START        ld de,GB        call copy64        ld a,1        ld (CUBE),a        xor a        ld (OWNER),a        ld (SHOWN),a        ld (MOOD),a        ld (PLAYED),a        ld (U1),a        ld (U2),a        ld (NDONE),a        ld a,25        ld (CURS),a        ld hl,S_HELLO        call say        call drawall        call getkey        ; the opening roll: one die each, the higher starts with both.open:  ld a,3        ld (SHOWN),a        call roll        call tsay        ld hl,S_YOUROLL        call tstr        ld a,(DIE1)        call tdig        ld hl,S_IROLL        call tstr        ld a,(DIE2)        call tdig        ld a,(DIE1)        ld b,a        ld a,(DIE2)        cp b        jr nz,.go        ld hl,S_TIE        call tstr        call tdone        call getkey        jr .open.go:    ld hl,S_YOUSTART        jr c,.h        ld hl,S_ISTART.h:     push af        call tstr        call tdone        call getkey        pop af        ld a,1        jr c,.hum        inc a.hum:   ld (SHOWN),a        dec a        ld (TURN),a        jp nz,cturn1        jp hturn1; The player's turn: first the cube, then the dice.hturn:  xor a        ld (SHOWN),a        ld (U1),a        ld (U2),a        ld (NDONE),a        ld a,25        ld (CURS),a        xor a        ld (TURN),a        call candbl        ld hl,S_ROLL        jr nc,.nd        ld hl,S_ROLLDBL.nd:    call sayplay        ld a,0        ld (MOOD),a        call drawall.k:     call candbl         ; D only if the player may double        ld a,K_ROLL        jr nc,.kr        ld a,K_ROLLD.kr:    call getk        cp 'R'        jr z,.roll        cp ' '        jr z,.roll        cp 13        jr z,.roll        cp 'D'        jr nz,.k        call candbl        jr nc,.k        call hdouble        ret c                   ; Gnubby passed: the game is over.roll:  ld a,1        ld (SHOWN),a        call rollhturn1: call hmove        ld hl,GB        call total        or a        jr nz,cturn        ; the player bore off the last checker        ld a,1        jp won; Gnubby's turn.cturn:  xor a        ld (U1),a        ld (U2),a        ld (NDONE),a        ld (SHOWN),a        ld a,25        ld (CURS),a        ld a,1        ld (TURN),a        call drawall            ; the player's cursor gone        call cdouble        ret c                   ; the player passed        ld a,2        ld (SHOWN),a        call rollcturn1: ld a,25        ld (CURS),a        call cmove        ld hl,GB+32        call total        or a        jp nz,hturn        ld a,2        jp won; won: A = 1 if the player won, 2 Gnubby: its points, and the next game.won:    ld (WINNER),a        ; 1 point, 2 for a gammon (the loser has none off), 3 for a        ; backgammon (and a checker on the bar or in the winner's home)        ld hl,GB+32        cp 1        jr z,.l        ld hl,GB.l:     push hl        call total        pop hl        ld c,1        cp 15        jr nz,.pts        inc c        ld de,18        add hl,de        ld b,7        xor a.bg:    or (hl)        inc hl        djnz .bg        jr z,.pts        inc c.pts:   ld a,c        ld (GAMMON),a        ld a,(CUBE)        ld b,a        xor a.m:     add a,c        djnz .m        jp score; score: A points to WINNER; the score, and a new game, or the match's end.score:  ld (POINTS),a        call lwin        ld a,(POINTS)        ld e,a        ld d,0        ld a,(WINNER)        dec a        ld hl,SCH        jr z,.h        ld hl,SCC.h:     ld a,(hl)        add a,e        ld (hl),a        inc hl        ld a,(hl)        adc a,d        ld (hl),a    ; the Crawford game is the one after a player first needs one point        ld a,(MLEN)        or a        jr z,.said        ld a,(CRAW)        cp 1        jr nz,.nc        inc a        ld (CRAW),a        jr .said.nc:    or a        jr nz,.said        ld a,(MLEN)        dec a        ld hl,SCH        cp (hl)        jr z,.cr        ld hl,SCC        cp (hl)        jr nz,.said.cr:    ld a,1        ld (CRAW),a.said:  call tsay        ld a,(WINNER)        dec a        ld hl,S_YOUWIN        jr z,.w        ld hl,S_IWIN.w:     call tstr        ld a,(POINTS)        call tnum        ld hl,S_POINT        call tstr        ld a,(POINTS)        dec a        jr z,.one        ld a,'S'        call tchr.one:   ld a,(GAMMON)        dec a        jr z,.plain        ld hl,S_GAMMON        dec a        jr z,.g        ld hl,S_BACKG.g:     call tstr.plain: ld a,(WINNER)        dec a        ld hl,S_YAY        ld a,2                  ; Gnubby sad, but a good sport        jr z,.m        ld hl,S_HEHE        ld a,1.m:     ld (MOOD),a        push hl                 ; the second line, unless the match is over        ; the match over?        ld a,(MLEN)        or a        jr z,.money        ld e,a        ld d,0        ld hl,(SCH)        or a        sbc hl,de        ld hl,S_MATCHYOU        jr nc,.over        ld hl,(SCC)        or a        sbc hl,de        ld hl,S_MATCHME        jr nc,.over        pop hl        call tstr        call tdone        call drawall        call cheer        call getkey        jp newgame.over:  pop de        push hl        ld hl,S_NL        call tstr        pop hl        call tstr        call tdone        call drawall        call cheer        call getkey        jp title.money: pop hl        call tstr        ld hl,S_AGAIN        call tstr        call tdone        call drawall        call cheer        ld a,K_AGAIN        call yesno        jp c,newgame        jp title; cheer: a tune for the winner.cheer:  ld a,(WINNER)        dec a        ld hl,T_WIN        jr z,.t        ld hl,T_LOSE.t:     jp tune; ----------------------------------------------------------------- cube; candbl: carry if the side on roll (TURN) may double: the cube theirs or; in the middle, not the Crawford game, and more points in it than they; need, in a match.candbl: ld a,(OWNER)        ld hl,TURN        or a        jr z,.mine        dec a        cp (hl)        jr nz,.no.mine:  ld a,(MLEN)        or a        jr z,.yes        ld a,(CRAW)        dec a        jr z,.no        call need        ld hl,CUBE        cp (hl)        jr c,.no        jr z,.no.yes:   scf        ret.no:    or a        ret; need: A = the points the side on roll needs to win the match.need:   ld a,(TURN)        or a        ld hl,(SCH)        jr z,.h        ld hl,(SCC).h:     ld a,(MLEN)        sub l        ret; hdouble: the player doubles: Gnubby takes (carry clear) or passes (the; game over, carry set).hdouble:        xor a                   ; the player        call ldbl        ld hl,S_DBLYOU        call say        ld a,3        ld (MOOD),a        call drawall        ; take if passing loses the match        ld a,(MLEN)        or a        jr z,.eq        ld hl,CUBE        ld a,(SCH)        add a,(hl)        ld hl,MLEN        cp (hl)        jr nc,.take        ; else if the player's equity is under the take threshold.eq:    ld hl,GB        ld de,ROOT        call copy64        ld a,1        ld (TAKEQ),a        xor a        ld (OWNED),a        call cubeq        jr c,.take.pass:  ld a,$41                ; Gnubby passes        call lone        ld hl,S_IPASS        call say        ld a,2        ld (MOOD),a        call drawall        call getkey        ld a,1        ld (WINNER),a        ld (GAMMON),a        ld a,(CUBE)        call score        scf        ret.take:  ld a,$31                ; Gnubby takes        call lone        ld hl,CUBE        sla (hl)        ld a,2        ld (OWNER),a        ld hl,S_ITAKE        call say        xor a        ld (MOOD),a        call drawall        ld hl,T_DBL        call tune        call getkey        or a        ret; cdouble: Gnubby's turn: it may double; the player takes (carry clear) or; passes (the game over, carry set).cdouble:        call candbl        ret nc        ; after the Crawford game, double at once when the player needs        ; a point and Gnubby more        ld a,(CRAW)        cp 2        jr nz,.eq        ld a,(MLEN)        dec a        ld hl,SCH        cp (hl)        jr z,.dbl.eq:    ld hl,GB        ld de,ROOT        call turn        xor a        ld (TAKEQ),a        ld a,(OWNER)        cp 2        ld a,0        jr nz,.o        inc a.o:     ld (OWNED),a        ld a,1        call cubeq        jr c,.dbl        or a        ret.dbl:   ld a,1                  ; Gnubby doubles        call ldbl        ld hl,T_DBL        call tune        call tsay        ld hl,S_IDOUBLE        call tstr        ld a,(CUBE)        add a,a        call tnum        ld hl,S_TAKEQ        call tstr        call tdone        ld a,1        ld (MOOD),a        call drawall        ld a,K_TAKE        call yesno        jr nc,.pass        ld a,$30                ; the player takes        call lone        ld hl,CUBE        sla (hl)        ld a,1        ld (OWNER),a        ld hl,S_YOUTAKE        call say        xor a        ld (MOOD),a        call drawall        ld b,40        call pause        or a        ret.pass:  ld a,$40                ; the player passes        call lone        ld a,2        ld (WINNER),a        ld a,1        ld (GAMMON),a        ld a,(CUBE)        call score        scf        ret; ------------------------------------------------------- the cube's rules; Gnubby judges the cube as gnubg does, by Janowski's cubeful equities,; from the engine's own chances for the doubler: of winning, and the extra; points of its gammons won and lost (eq0's; within an eighth of a single; game of changing his mind, ply1's, a ply deeper). Money: the dead cube's; equity and the live cube's, between the take and cash points that the; average win and loss give. A match: the same in match winning chances,; from gnubg's match equity table (MET16) at the score, the cash points; worked out down the cube's levels as gnubg does. For no double (the; cube as it is) and for double and take (twice the cube, the other's); a; pass is a single win at the cube. The model is src/zcube.ax. Chances; 8192 for 1; money 2048 a point at the cube; winning chances 16384 for 1.JBASE   equ TOPC+2              ; the rule's variables, in decide's TOPC                                ; (past its count and capacity; ply1's                                ; P1LIST from TOPC+40 is idle once the                                ; chances are in)JP      equ JBASE+0             ; the chances: of winning (1 to 8191),JG      equ JBASE+2             ; the gammons' extra points won (to JP)JL      equ JBASE+4             ; and lost (to 1 - JP)JG0     equ JBASE+6             ; the gammon ratios of the doubler's winsJG1     equ JBASE+8             ; and of the other'sJX      equ JBASE+10            ; the cube's efficiency (256 for 1)JA      equ JBASE+11            ; the doubler's away, then the other'sJB      equ JBASE+12JPOST   equ JBASE+13            ; either is 1-awayJC      equ JBASE+14            ; the cube (1 for money)JV      equ JBASE+15            ; the cube of the state being valuedJOWN    equ JBASE+16            ; its owner: 0 the middle, 1 the doubler, 2 the otherRW      equ JBASE+17            ; the doubler's results at JV: a single win,RWG     equ JBASE+19            ; a gammon won, a single loss, a gammon lostRL      equ JBASE+21RLG     equ JBASE+23JDEAD   equ JBASE+25            ; the dead cube's equityJTG     equ JBASE+27            ; the doubler's cash pointJOTG    equ JBASE+29            ; 1 less the other'sJWIN    equ JBASE+31            ; the average win and lossJLOSE   equ JBASE+33JS      equ JBASE+35            ; the decision (see jdec); double and take,                                ; the passJDT     equ JBASE+37JDP     equ JBASE+39JWID    equ JBASE+41            ; close within thisLPB     equ JBASE+43            ; lerp's far endJCP     equ JBASE+45            ; cash points by side and level (2 x 4 x 2)JN      equ JBASE+61            ; the levelJK      equ JBASE+62            ; the sideAK      equ JBASE+63            ; its away, the other'sBK      equ JBASE+64CV      equ JBASE+65            ; the level's cubeJQ      equ JBASE+66            ; twice the cube a redouble is valued atJTOP    equ JBASE+67            ; the top levelJTX     equ JBASE+68            ; the redouble's take (2)JDTW    equ JBASE+70            ; its win (2)JNEXT   equ JBASE+72            ; the other's cash point a level up (2)JDP2    equ JBASE+74            ; a win at the level's cube (2), to +76; cubeq: ROOT = a board, the doubler (A: 0 the player, 1 Gnubby) on roll,; OWNED whether they own the cube, TAKEQ whether it is a take -> carry if; the doubler should double (a double), or the other take (a take).cubeq:  call jscore        call vwmoney        ld hl,ROOT        call eq0        ; the cube's efficiency: 0.68 in contact; in a race 0.55 + 0.00125        ; a pip of the doubler's, from 0.6 to 0.7 (256 for 1)        call contact        ld a,174        jr c,.x        ld hl,ROOT2        call pips        ex de,hl        ld a,82        call mul        ld a,h        cp 38        jr c,.r        ld a,38.r:     add a,141        cp 154        jr nc,.x        ld a,154.x:     ld (JX),a        call jdec        jr nc,jacts        ld a,3                  ; thinking        ld (MOOD),a        call drawmasc        ld a,(TAKEQ)            ; his own double: he says he is thinking        or a                    ; (a take follows "YOU DOUBLE? HMM")        ld hl,S_CUBEQ        call z,say        call ply1        call jdec; jacts: carry if the rule doubles (double and take, or the pass if less,; better than no double: JS < 0), or takes (double and take no better than; the pass, for the doubler: JS <= 0).jacts:  ld hl,(JS)        ld a,(TAKEQ)        or a        jr z,.n        dec hl.n:     ld a,h        rla        ret; jdec: from the chances in SPW, JS: for a take double and take less the; pass, for a double no double less the smaller of them -> carry if it is; within JWID (an eighth of a single game) of 0.jdec:   ld hl,(SPW)             ; P from 1 to 8191, G to P, L to 1 - P        ld a,h        or l        jr nz,.p        inc hl.p:     ld de,8191        call umin        ld (JP),hl        ex de,hl        ld hl,(SPW+2)        call umin        ld (JG),hl        ld hl,8192        or a        sbc hl,de        ex de,hl        ld hl,(SPW+4)        call umin        ld (JL),hl        ld bc,8192              ; the gammon ratios: L / (1 - P), G / P        call mdu        ld (JG1),hl        ld hl,(JG)        ld de,(JP)        ld bc,8192        call mdu        ld (JG0),hl        ld a,(JC)               ; the pass, and the width (an eighth of it        ld (JV),a               ; less a single loss)        call results        ld hl,(RW)        ld (JDP),hl        ld de,(RL)        or a        sbc hl,de        ld b,3.w:     srl h        rr l        djnz .w        ld (JWID),hl        ld a,(JC)               ; double and take: twice the cube, the other's        add a,a        ld (JV),a        ld a,2        ld (JOWN),a        call cubeful        ld (JDT),hl        ld de,(JDP)        ld a,(TAKEQ)        or a        jr nz,.m        ld a,(JC)               ; no double: the cube as it is        ld (JV),a        ld a,(OWNED)        ld (JOWN),a        call cubeful        push hl        ld hl,(JDT)        ld de,(JDP)        call smin        ex de,hl        pop hl.m:     or a        sbc hl,de        ld (JS),hl        call abshl        ld de,(JWID)        jp scmp; jscore: A = the doubler (0 the player, 1 Gnubby) -> JC = the cube (1 for; money); in a match JA and JB, the doubler's and the other's aways, and; JPOST, either 1-away.jscore: ld b,a        ld a,1        ld (JC),a        ld a,(MLEN)        or a        ret z        ld c,a        ld a,(SCH)        ld d,a        ld a,(SCC)        ld e,a        ld a,b        or a        jr z,.p        ld a,d        ld d,e        ld e,a.p:     ld a,c        sub d        ld (JA),a        ld b,a        ld a,c        sub e        ld (JB),a        ld hl,JPOST        ld (hl),1        dec a        jr z,.c        dec b        jr z,.c        ld (hl),0.c:     ld a,(CUBE)        ld (JC),a        ret; results: RW, RWG, RL, RLG at the cube JV: money's 2048 and 4096 a cube,; a match's from MET16.results:        ld a,(MLEN)        or a        jr nz,.m        ld a,(JV)        ld h,a        ld l,0        add hl,hl        add hl,hl        add hl,hl        ld (RW),hl        add hl,hl        ld (RWG),hl        call neghl        ld (RLG),hl        sra h        rr l        ld (RL),hl        ret.m:     ld hl,(JA)        ld (AK),hl        ld a,(JV)        call mwa        ld (RW),hl        ld a,(JV)        add a,a        call mwa        ld (RWG),hl        ld a,(JV)        call mwb        ld (RL),hl        ld a,(JV)        add a,a        call mwb        ld (RLG),hl        ret; cubeful: HL = the doubler's cubeful equity at cube JV owned by JOWN,; from JP, JG, JL, JG0, JG1 and JX: the dead cube's, and the live cube's; through the cash points, mixed by the efficiency.cubeful:        call results        ld hl,(RW)              ; dead: RL + (RW - RL) P + (RWG - RW) G        ld de,(RL)              ; - (RL - RLG) L        ld bc,(JP)        call dq        push hl        ld hl,(RWG)        ld de,(RW)        ld bc,(JG)        call dq        pop de        add hl,de        push hl        ld hl,(RL)        ld de,(RLG)        ld bc,(JL)        call dq        ex de,hl        pop hl        or a        sbc hl,de        ld de,(RL)        add hl,de        ld (JDEAD),hl        ld a,(MLEN)        or a        jr z,.money        ld a,(JV)               ; dead in a match: both sides within the        ld c,a                  ; cube, or both 2-away        ld a,(JA)        ld b,a        ld a,(JB)        cp 2        jr nz,.n2        cp b        ret z.n2:    dec a        cp c        jr nc,.live        ld a,b        dec a        cp c        ret c.live:  call cpts        jr .wl.money: ld hl,(JG0)             ; the take and cash points:        ld de,(JG1)             ; (L - 1/2) / (W + L + 1/2)        add hl,de               ; and (L + 1) / (W + L + 1/2)        ld de,20480        add hl,de        ex de,hl        push de        ld hl,(JG1)        ld bc,4096        add hl,bc        ld bc,8192        call mdu        ld (JOTG),hl        pop de        ld hl,(JG1)        ld bc,16384        add hl,bc        ld bc,8192        call mdu        ld (JTG),hl.wl:    ld hl,(RW)              ; the average win and loss        ld de,(RWG)        ld bc,(JG0)        call mix        ld (JWIN),hl        ld hl,(RL)        ld de,(RLG)        ld bc,(JG1)        call mix        ld (JLOSE),hl        ld de,(JP)              ; the live cube's equity at P        ld a,(JOWN)        dec a        jr nz,.c0        ld hl,(JTG)             ; owned: the loss to the cash point, the        or a                    ; cash point to the win        sbc hl,de        jr nc,.ca        jr .cb.c0:    ld hl,(JOTG)            ; else the loss to the other's cash point        or a        sbc hl,de        jr nc,.cc        dec a        jr z,.cd                ; the other's: then to the win        ld hl,(JTG)             ; in the middle: cash point to cash point,        or a                    ; then to the win        sbc hl,de        jr c,.cb        jr z,.cb        ld hl,(JTG)        ld (LPB),hl        ld bc,(JOTG)        ld hl,(RL)        ld de,(RW)        jr .lerp.ca:    ld hl,(JTG)        ld (LPB),hl        ld bc,0        ld hl,(JLOSE)        ld de,(RW)        jr .lerp.cb:    ld hl,8192        ld (LPB),hl        ld bc,(JTG)        ld hl,(RW)        ld de,(JWIN)        jr .lerp.cc:    ld hl,(JOTG)        ld (LPB),hl        ld bc,0        ld hl,(JLOSE)        ld de,(RL)        jr .lerp.cd:    ld hl,8192        ld (LPB),hl        ld bc,(JOTG)        ld hl,(RL)        ld de,(JWIN).lerp:  call lerp        ld de,(JDEAD)           ; dead + (live - dead) x        or a        sbc hl,de        ld a,(JX)        ld c,a        ld b,0        ld de,256        call mdiv        ld de,(JDEAD)        add hl,de        ret; cpts: a match's cash points at cube JV, from the top level down (gnubg's; GetPoints): JTG, the doubler's, and JOTG, 1 less the other's, 0 to 1.cpts:   ld a,(JV)        ld c,a                  ; the dead cube        ld b,0                  ; levels below it.nm:    ld a,c        add a,a        ld d,a        ld a,(JA)        dec a        cp d        jr c,.top        ld a,(JB)        dec a        cp d        jr c,.top        inc b        ld c,d        jr .nm.top:   ld a,b        ld (JN),a.lev:   ld a,(JN)               ; CV = JV << JN        ld b,a        ld a,(JV)        inc b        jr .s1.s:     add a,a.s1:    djnz .s        ld (CV),a        xor a        call cpk        ld a,1        call cpk        ld hl,JN        dec (hl)        jp p,.lev        ld hl,(JCP)        call c01        ld (JTG),hl        ld hl,(JCP+8)        call c01        ex de,hl        ld hl,8192        or a        sbc hl,de        ld (JOTG),hl        ret; c01: HL (signed) from 0 to 8192.c01:    bit 7,h        jr z,.p        ld hl,0.p:     ld de,8192        jp umin; cpk: JCP's cash point of side A (0 the doubler, 1 the other) at level JN; and cube CV: where taking a redouble (a loss of 2q, gammons by the; other's ratio; below the top, the redouble's single loss with the; other's cash point a level up) and passing (a win at CV) are equal,; against a win of 2q (gammons by the side's ratio).cpk:    ld (JK),a        ld hl,(JA)        or a        jr z,.o        ld a,l        ld l,h        ld h,a.o:     ld (AK),hl        ld a,(CV)               ; the pass        call mwa        ld (JDP2),hl        ld a,(CV)               ; the top level: a redouble would leave a        ld b,a                  ; side within the cube        add a,a        ld c,a        ld a,(JA)        dec a        cp c        jr c,.t        ld a,(JB)        dec a        cp c.t:     sbc a,a        ld (JTOP),a        or a                    ; q: CV, or at the top 2 CV when this side        ld a,b                  ; is within it and the other is not        jr z,.q        ld a,(AK)        dec a        cp c        ld a,b        jr nc,.q        ld a,(BK)        dec a        cp c        ld a,b        jr c,.q        ld a,c.q:     add a,a        ld (JQ),a        call mwb                ; the take: a loss of 2q        ld (JTX),hl        ld a,(JQ)               ; a win of 2q, gammons by the side's ratio        call mwa        push hl        ld a,(JQ)        add a,a        call mwa        ex de,hl        pop hl        ld a,(JK)        call gk        call mix        ld (JDTW),hl        ld hl,8192        ld (JNEXT),hl        ld a,(JTOP)        or a        jr z,.al        ld a,(JQ)               ; the top: the take's gammons by the        add a,a                 ; other's ratio        call mwb        ex de,hl        ld hl,(JTX)        ld a,(JK)        xor 1        call gk        call mix        ld (JTX),hl        jr .c.al:    ld a,(JK)               ; below: the other's cash point a level up        xor 1        call cpat        inc hl        inc hl        ld a,(hl)        inc hl        ld h,(hl)        ld l,a        ld (JNEXT),hl.c:     ld hl,(JTX)             ; 1 - next (dp - dtw) / (take - dtw), or 1        ld de,(JDTW)        or a        sbc hl,de        jr nz,.dv        ld hl,8192.st:    push hl        ld a,(JK)        call cpat        pop de        ld (hl),e        inc hl        ld (hl),d        ret.dv:    push hl        ld hl,(JDP2)        or a        sbc hl,de        pop de        ld bc,8192        call mdiv        bit 7,h        jr z,.pt        ld hl,0.pt:    ld b,h        ld c,l        ld hl,(JNEXT)        call mulq        ex de,hl        ld hl,8192        or a        sbc hl,de        jr .st; cpat: HL -> JCP's entry for side A at level JN. Loses DE.cpat:   add a,a        add a,a        ld l,a        ld a,(JN)        add a,l        add a,a        ld l,a        ld h,0        ld de,JCP        add hl,de        ret; gk: BC = side A's gammon ratio (JG0, JG1). Keeps HL.gk:     push hl        ld hl,JG0        or a        jr z,.g        inc hl        inc hl.g:     ld c,(hl)        inc hl        ld b,(hl)        pop hl        ret; mwa: HL = mwc(AK - A, BK); mwb: HL = mwc(AK, BK - A).mwa:    ld b,a        ld a,(AK)        sub b        ld d,a        ld a,(BK)        ld e,a        jr mwcmwb:    ld b,a        ld a,(BK)        sub b        ld e,a        ld a,(AK)        ld d,a; mwc: HL = the winning chances (16384 for 1) of a side D away against E; away (signed; 0 or less: won, or lost), from MET16, after the Crawford; game if JPOST and one of them is 1-away.mwc:    ld hl,16384        ld a,d        dec a        ret m        ld c,a        ld hl,0        ld a,e        dec a        ret m        ld b,a        ld a,(JPOST)        or a        jr z,.pre        ld a,c        or a        jr z,.lead        ld a,b        or a        jr nz,.pre        ld a,c        jr .post.lead:  ld a,b        call .post        ex de,hl        ld hl,16384        or a        sbc hl,de        ret.pre:   ld a,c        add a,a        add a,a        add a,a        sub c        add a,b        jr .t.post:  add a,49.t:     ld l,a        ld h,0        add hl,hl        ld de,MET16        add hl,de        ld a,(hl)        inc hl        ld h,(hl)        ld l,a        ret; lerp: HL = the line from (BC, HL) to (LPB, DE) at JP.lerp:   push hl        ex de,hl        pop de        push de        or a        sbc hl,de        push hl        ld hl,(JP)        or a        sbc hl,bc        ex (sp),hl        push hl        ld hl,(LPB)        or a        sbc hl,bc        ex de,hl        pop hl        pop bc        call mdiv        pop de        add hl,de        ret; mix: HL = HL + (DE - HL) BC / 8192.mix:    push hl        ex de,hl        pop de        push de        call dq        pop de        add hl,de        ret; dq: HL = (HL - DE) BC / 8192; mulq: HL BC / 8192 (toward 0).dq:     or a        sbc hl,demulq:   ld de,8192; mdiv: HL = sign(HL / DE) floor(|HL| BC / |DE|), at most 32767 in size; (BC >= 0, DE not 0): zcube.ax's mdiv.mdiv:   ld a,h        xor d        push af        call abshl        ex de,hl        call abshl        ex de,hl        call mdu        pop af        ret p        jp neghl; abshl: HL = |HL|.abshl:  bit 7,h        ret z        jp neghl; mdu: HL = floor(HL BC / DE), unsigned, at most 32767.mdu:    push de        ex de,hl        ld hl,0                 ; DE:HL = DE x BC        ld a,16.m:     add hl,hl        rl e        rl d        jr nc,.mn        add hl,bc        jr nc,.mn        inc de.mn:    dec a        jr nz,.m        pop bc                  ; / BC        ex de,hl        or a        sbc hl,bc        jr nc,.sat        add hl,bc        ld a,16.l:     ex de,hl        add hl,hl        ex de,hl        adc hl,hl        jr c,.o        sbc hl,bc        jr nc,.one        add hl,bc        jr .n.o:     or a        sbc hl,bc.one:   inc e.n:     dec a        jr nz,.l        ex de,hl        bit 7,h        ret z.sat:   ld hl,32767        ret; umin: HL = the smaller of HL and DE (unsigned). Keeps DE.umin:   or a        sbc hl,de        add hl,de        ret c        ld h,d        ld l,e        ret; smin: HL = the smaller of HL and DE (signed).smin:   push hl        call scmp        pop hl        ret c        ex de,hl        ret; scmp: carry if HL < DE, signed. Loses HL.scmp:   ld a,h        xor d        jp m,.diff        or a        sbc hl,de        ret.diff:  ld a,h        rla        ret; vwmoney: the engine's outcomes' weights (VW) for money play.vwmoney:        ld hl,N2_VMONEY        ld de,VW        ld bc,6        ldir        ret; vwset: VW for the side on roll (TURN) at the score and cube: money's, or; in a match each side's gammon's and backgammon's worth in 1/32 point (a; single game 32): gnubg's gammon prices, from MET16, rounded, at most; 127 (zcube.ax's `prices`).vwset:  call vwmoney        ld a,(MLEN)        or a        ret z        ld a,(TURN)        call jscore        ld hl,(JA)        ld de,VW+1        call .ent        ld hl,(JA)        ld a,l        ld l,h        ld h,a        ld de,VW+4        call .ent        ld hl,VW+4        call .ng.ng:    xor a        sub (hl)        ld (hl),a        inc hl        ret; .ent: L, H = a side's away and the other's -> (DE), (DE+1) = its; gammon's and backgammon's worth..ent:   ld (AK),hl        push de        ld a,(JC)        call mwb                ; a single loss        push hl        ld a,(JC)        call mwa                ; a single win        pop de        push hl        add hl,de        ld (JDP2),hl            ; their sum        pop hl        or a        sbc hl,de        ld (JTX),hl             ; and difference        ld a,(JC)        add a,a        call .pr        pop de        ld (de),a        inc de        push de        ld a,(JC)        ld b,a        add a,a        add a,b        call .pr        pop de        ld (de),a        ret; .pr: A = 32 (2 mwc(AK - A, BK) - the sum) / the difference, rounded, at; most 127..pr:    call mwa        add hl,hl        ld de,(JDP2)        or a        sbc hl,de        ld bc,64        ld de,(JTX)        call mdu        inc hl        srl h        rr l        ld a,h        or a        ld a,l        jr nz,.big        cp 128        ret c.big:   ld a,127        ret; ----------------------------------------------------------- the player; hmove: the player plays DIE1, DIE2 (DHI, DLO), a checker at a time.hmove:  ld hl,GB        ld de,TB0        call copy64        xor a        ld (NDONE),a        ld (U1),a        ld (U2),a        ld hl,GB        call mx        ld (NMAX),a        or a        jr nz,.can        ld hl,S_NOMOVE        call say        ld a,2        ld (MOOD),a        call drawall        ld hl,0                 ; the player's turn, no play        xor a        call lmove        jp getkey.can:   call first.loop:  xor a        ld (MOOD),a        ld a,(NDONE)        ld hl,NMAX        cp (hl)        jr nz,.more        ld hl,S_DONE        call say        call drawall.dk:    ld a,K_DONE        call getk        cp 13        jr z,.fin        cp ' '        jr z,.fin        cp 'U'        jr nz,.dk        call undo        jr .loop.fin:   ld hl,TSTEPS            ; the player's turn, as played        xor a        jp lmove.more:  ld hl,S_MOVE        call say        call drawall.k:     ld a,K_MOVE        call getk        cp 'D'        jr z,.swap        cp 'Q'                  ; up, towards 24        jr z,.up        cp '7'                  ; (the cursor keys' up)        jr z,.up        cp 'A'                  ; down, towards 1        jr z,.down        cp '6'        jr z,.down        cp 'U'        jr z,.undo        cp 'H'        jr z,.hint        cp '1'        ld hl,DIE1        jr z,.die        cp '2'        ld hl,DIE2        jr z,.die        cp 13        jr z,.any        cp ' '        jr nz,.k.any:   ; the current die, the left unless it is played        ld a,(U1)        or a        jr nz,.rt        ld a,(DIE1)        call tryd        jp nc,.loop        ; it cannot be: the right, if it can, swapped to the left        ld a,(U2)        or a        jr nz,.no        call swapd        ld a,(DIE1)        call tryd        jp nc,.loop        call swapd        jr .no.rt:    ld a,(DIE2).try:   call tryd        jp nc,.loop.no:    ld hl,T_LOSE+4        call tune        jp .k.die:   ld a,(hl)        jr .try.swap:  call swapd        call drawall        jp .k.up:    ld e,1        jr .mv.down:  ld e,-1.mv:    call cursor        call drawall        jp .k.undo:  call undo        jp .loop.hint:  ld hl,S_HINT        call say        ld a,3        ld (MOOD),a        ld a,25                 ; no cursor while he thinks        ld (CURS),a        call drawall        ld hl,TB0        ld de,ROOT        call copy64        call think        ; its steps, for the log: the play generated again, as Gnubby's        ld hl,match        ld (USERFN),hl        ld hl,ROOT        call gen        ld hl,CSTEPS        ld de,TSTEPS        ld bc,8        ldir        ld hl,CRES        ld de,GB        call copy64        ld a,(NMAX)        ld (NDONE),a        ld a,1        ld (U1),a        ld (U2),a        ld a,25        ld (CURS),a        jp .loop; swapd: the dice (and whether each is played) the other way round.swapd:  ld hl,(DIE1)        ld a,l        ld l,h        ld h,a        ld (DIE1),hl        ld hl,(U1)        ld a,l        ld l,h        ld h,a        ld (U1),hl        ret; undo: the turn's start again.undo:   ld hl,TB0        ld de,GB        call copy64        xor a        ld (NDONE),a        ld (U1),a        ld (U2),a; first: the cursor on the bar, or the highest point with a checker.first:  ld a,$ff                ; no steps yet this turn        ld (TSTEPS),a        ld a,26        ld (CURS),a        ld e,0; cursor: to the next point in direction E (1 up, -1 down) with one of the; player's checkers, or (E = 0) kept if it has one; on the bar while; there are checkers there; none (25) when all are borne off.cursor: ld hl,GB        call total        or a        jr nz,.some        ld a,25        ld (CURS),a        ret.some:  ld a,(GB+24)        or a        ld a,24        jr nz,.set        ld a,e        or a        jr nz,.go        dec e        ld a,(CURS)        cp 24        jr c,.chk        ld a,23        jr .chk.go:    ld a,(CURS)        cp 24        jr c,.l        ld a,24.l:     add a,e        cp 24        jr c,.chk        ld a,e        or a        ld a,23        jp m,.chk        xor a.chk:   push af        ld hl,GB        call index        or a        pop bc        ld a,b        jr z,.l.set:   ld (CURS),a        ret; tryd: A = a die: its step from the cursor, if legal and the rest of the; roll can still be played as it must; carry if not.tryd:   ld e,a        ; the die unplayed?        ld a,(DIE1)        ld b,a        ld a,(DIE2)        cp b        jr z,.ok        ld a,e        cp b        jr nz,.d2        ld a,(U1)        or a        jr z,.ok.d2:    ld a,(DIE2)        cp e        scf        ret nz        ld a,(U2)        or a        scf        ret nz.ok:    ; with only one die to play, the larger if it can be        ld a,(NMAX)        dec a        jr nz,.st        ld a,(MUSTHI)        or a        jr z,.st        ld a,(DHI)        cp e        scf        ret nz.st:    push de        ld hl,GB        ld de,W        call copy64        pop de        ld a,(CURS)        ld b,a        push de        call step        pop de        ret c        ; the rest: as many dice as the roll must play        ld hl,W        push de        ld de,ANIM        call copy64        pop de        ld a,(NDONE)        ld b,a        ld a,(NMAX)        sub b        dec a        jr z,.yes        ld (NNEED),a        ld hl,GDIE        ld (hl),e        ld a,(DIE1)        ld b,a        ld a,(DIE2)        cp b        jr z,.dbl        cp e        jr nz,.oth        ld a,b.oth:   ld (hl),a.dbl:   inc l        ld (hl),e        inc l        ld (hl),e        inc l        ld (hl),e        ld (GDOUBLE),a        ld hl,ANIM        ld (GROOT),hl        xor a        ld (GKEYED),a        ld (GPRUNE),a        push de        ld a,(NNEED)        call measure        pop de        ld hl,NNEED        cp (hl)        ret c.yes:   call lstep        ld hl,ANIM        push de        ld de,GB        call copy64        pop de        ld hl,NDONE        inc (hl)        ; which die        ld a,(DIE1)        cp e        jr nz,.u2        ld hl,U1        ld a,(hl)        or a        jr z,.set.u2:    ld hl,U2.set:   ld (hl),1        ld e,0        call cursor        ld bc,$0430        call tone        or a        ret; mx: HL -> a board, DHI/DLO the dice -> A = how many can be played, and; MUSTHI: only one can, and the larger can.mx:     ld (GROOT),hl        ld de,W        call copy64        xor a        ld (GKEYED),a        ld (GPRUNE),a        ld (MUSTHI),a        ld a,(DHI)        ld b,a        ld a,(DLO)        cp b        jr nz,.nd        ld hl,GDIE        ld (hl),a        inc l        ld (hl),a        inc l        ld (hl),a        inc l        ld (hl),a        ld (GDOUBLE),a        ld a,4        jp measure.nd:    xor a        ld (GDOUBLE),a        call order_a        call measure2        ld (MXA),a        cp 2        ret z        call order_b        call measure2        ld b,a        ld a,(MXA)        cp b        jr nc,.a        ld a,b        ret.a:     or a        ret z        ld (MUSTHI),a        ret; ---------------------------------------------------------------- Gnubby; cmove: Gnubby plays DHI, DLO, shown a checker at a time.cmove:  ld hl,S_THINK        call say        ld a,3        ld (MOOD),a        call drawall        ld hl,GB        ld de,ROOT        call turn        call think        or a        jr nz,.can        ld hl,0                 ; Gnubby's turn, no play        ld a,1        call lmove        call tsay        ld hl,S_ICANT        call tstr        call tdone        ld a,1        ld (PLAYED),a        ld a,2        ld (MOOD),a        call drawall        ld b,100        jp pause.can:   ; its steps: the play generated again, the one giving RESULT        ld hl,match        ld (USERFN),hl        ld hl,ROOT        call gen        ld hl,CSTEPS            ; Gnubby's turn, as played        ld a,1        call lmove        ld hl,ROOT        ld de,ANIM        call copy64        call tsay        ld hl,S_IPLAY        call tstr        ld a,4        ld (CNT),a        ld hl,CSTEPS.s:     ld a,(hl)        cp $ff        jr z,.done        push hl        ld b,a        ld de,4        add hl,de        ld e,(hl)        push de        push bc        ; its notation: from/to, * for a hit        ld a,b        cp 24        jr nz,.pt        ld hl,S_BAR        call tstr        jr .to.pt:    inc a        call tnum.to:    ld a,'/'        call tchr        pop bc        pop de        push bc        push de        ld hl,ANIM        ld de,W        call copy64        pop de        pop bc        call step        push af        ld a,b        sub e        jr nc,.on        ld hl,S_OFF        call tstr        jr .hit.on:    inc a        call tnum.hit:   pop af        or a        jr z,.nh        ld a,'*'        call tchr.nh:    ld a,' '        call tchr        ld hl,W        ld de,ANIM        call copy64        ld hl,ANIM        ld de,GB        call turn        call drawall        ld bc,$0440        call tone        ld b,25        call pause        pop hl        inc hl        ld a,(CNT)        dec a        ld (CNT),a        jr nz,.s.done:  call tdone        ld a,1        ld (PLAYED),a        ld (MOOD),a        call drawall        ld b,50        jp pause; sayplay: says HL, after Gnubby's play if he has just played (without; its "I PLAY" if it would not fit on a line).sayplay:        ld a,(PLAYED)        or a        jp z,say        xor a        ld (PLAYED),a        push hl        ld hl,TXT        xor a        ld bc,0        cpir        ld a,c        add a,33                ; the length with its 0 is -C: carry if        ld hl,TXT               ; 32 or less        jr c,.fit        ld hl,TXT+7.fit:   ld de,TXT2.c:     ld a,(hl)        ld (de),a        inc hl        inc de        or a        jr nz,.c        call tsay        ld hl,TXT2        call tstr        ld a,'|'        call tchr        pop hl        call tstr        jp tdone; think: decide, with the outcomes' weights at the score, keeping the; dice (it leaves DHI, DLO at the last reply it looked at); the chosen; play's position in CRES (the engine's ROOT2, free until it next; decides: RESULT is the move generator's board).CRES    equ ROOT2think:  call vwset.d:     call decide.e:     push af        ld hl,RESULT        ld de,CRES        call copy64        call setdice        pop af        ret; copy64: the 64 bytes at HL to DE.copy64: ld bc,64        ldir        ret; match: a generated play: its steps kept if it is the chosen one.match:  ld hl,W        ld de,CRES        ld b,64.c:     ld a,(de)        cp (hl)        ret nz        inc hl        inc de        djnz .c        call mksteps        ld hl,NEWSTEPS        ld de,CSTEPS        ld bc,8        ldir        ret; ----------------------------------------------------------------- text; Building what Gnubby says: tsay starts, tstr/tnum/tdig/tchr add, tdone; says it.tsay:   ld hl,TXT        ld (TXP),hl        rettstr:   ld de,(TXP).l:     ld a,(hl)        or a        jr z,.e        ld (de),a        inc hl        inc de        jr .l.e:     ld (TXP),de        rettdig:   add a,'0'tchr:   ld hl,(TXP)        ld (hl),a        inc hl        ld (TXP),hl        rettnum:   ld l,a        ld h,0        ld de,(TXP)        call utoa        ld (TXP),de        rettdone:  xor a        call tchr        ld hl,TXT        jp say