;Mastermind v1.1 ;by Jonah Cohen ;http://jonah.ticalc.org #include "TI86.inc" .plugin lite86 .org _asm_exec_ram nop jp start .dw 0 .dw description description: .db "Mastermind " title: .db "v1.1 " author: .db "by Jonah Cohen",0 .db $19,$28,"http://jonah.ticalc.org",0 start: ld hl,try_num ld b,8 call _CLRLP ;set answer balls ld b,4 ball_loop push bc push hl call _OP1SET0 call _OP2SET4 call _RANDINT ;get random ball value from 0-4 call _CONVOP1 pop hl pop bc ld (hl),a inc hl djnz ball_loop call _flushAllMenus call _runIndicOff ld hl,title_screen ld de,$fc00 DispRLE: ld bc,1024 ;we need to copy DispRLEL: ld a,(hl) ;get the next byte cp $91 ;is it a run? jr z,DispRLERun ;then we need to decode the run ldi ;copy the byte, and update counters DispRLEC: ld a,b ;check the low byte and or c ;the high byte for 0 jr nz,DispRLEL ;if not, then we're not done either jr done_title ;if it's zero, we're done DispRLERun: inc hl ;move to the run value ld a,(hl) ;get the run value inc hl ;move to the run count push hl ;save source pointer ld h,(hl) ;get the run count ex de,hl ;swap source and destination pointers DispRLERunL: ld (hl),a ;copy the byte inc hl ;increase destination pointer dec bc ;decrease byte count dec d ;decrease run count jr nz,DispRLERunL ;if we're not done, then loop ex de,hl ;swap pointers back pop hl ;recover source pointer inc hl ;advance the source pointer jr DispRLEC ;check to see if we should loop done_title: set 3,(iy+5) ;inverse video ld hl,title ld bc,$201f call vputs_bc call vputs res 3,(iy+5) ;normal video call getkey cp K_EXIT jp z,exit call _clrLCD ld hl,main_screen ld de,$fc37 ld bc,8 ld a,11 ;number of rows copy_main_screen: push bc ldir pop bc ex de,hl add hl,bc ex de,hl dec a jr nz,copy_main_screen ld hl,author+2 ld bc,$1045 call vputs_bc ;draw answer box ld hl,$fe49 ld de,12 ld bc,$0aff draw_answer_box: ld (hl),$3f inc hl ld (hl),c inc hl ld (hl),c inc hl ld (hl),c inc hl ld (hl),$80 add hl,de djnz draw_answer_box ;draw vertical lines ld l,$00 ld a,4 call vert_line ld l,$04 ld a,16 call vert_line ld l,$06 ld a,4 call vert_line ;draw horizontal bars ld hl,$fc00 ld de,16*8+10 horiz_lines: ld (hl),$07 inc hl ld bc,$05ff horiz_line: ld (hl),c inc hl djnz horiz_line ld (hl),$fc add hl,de jr nc,horiz_lines ;draw numbers ld b,7 ld de,$0101 num_loop: ld (_penCol),de ld a,b add a,'0' push bc push de call _vputmap pop de pop bc ld a,d add a,9 ld d,a djnz num_loop call draw_balls ;*****************************main loop main_loop: call getkey sub K_LEFT jp z,move_left dec a ;right jp z,move_right dec a ;up jp z,update_pegs sub K_SECOND-K_UP jp z,toggle_ball dec a ;exit jr nz,main_loop ;***************************exit sequences lose: ld hl,lose_text jr disp_exit win: ld hl,win_text disp_exit: ld (iy+5),%00001010 call vputs ld (iy+5),a dec a ;-1 ld (try_num),a ld hl,ansball1 ld de,ball1 ld bc,4 ldir ld hl,$fe59 ;start of black squares ld de,13 ld b,8 exit_loop: ld (hl),$20 inc hl ld (hl),d inc hl ld (hl),d inc hl ld (hl),d add hl,de djnz exit_loop call draw_balls call cursor call getkey exit: jp _clrWindow ;**************************update the pegs update_pegs: ld hl,black ld bc,8 ld (hl),b ;no blacks yet inc hl ld (hl),b ;no whites yet ld hl,ball1 ld de,tempball ldir ld de,tempans ld b,4 check_black: ld a,(de) cp (hl) jr nz,black_not_found ld a,-1 ;trash value, since it's been used ld (de),a dec a ;trash value, since it's been used ld (hl),a ld a,(black) inc a ld (black),a black_not_found: inc de inc hl djnz check_black ld b,4 check_white: dec hl call get_white djnz check_white ld a,(black) push af ;save number of black ld a,(try_num) call get_y ld b,37 ;x coordinate do_black: ld a,(black) or a jr z,do_white ld ix,black_box call putsprite dec a ld (black),a inc b inc b inc b inc b jr do_black do_white: ld a,(white) or a jr z,finish_update ld ix,white_box call putsprite dec a ld (white),a inc b inc b inc b inc b jr do_white finish_update: call cursor pop af ;retrieve number of blacks cp 4 ;if all 4 are black, then you win jp z,win ld hl,try_num ;increment try number inc (hl) ld a,7 cp (hl) jp z,lose call draw_balls ;draw the new balls jp main_loop get_white: ;updates white if ball is found push hl push bc ld de,tempans ld b,4 compare_loop: ld a,(de) cp (hl) jr z,found inc de djnz compare_loop jr finish_get_value ;z flag set by djnz found: ld a,-1 ;trash value, since it's been used ld (de),a dec a ;trash value, since it's been used ld (hl),a ld hl,white inc (hl) finish_get_value: pop bc pop hl ret ;**************************movement move_right: call cursor ld hl,ball_num inc (hl) ld a,4 cp (hl) push af push hl call nz,cursor pop hl pop af jp nz,main_loop dec (hl) call cursor jp main_loop move_left: call cursor ld hl,ball_num dec (hl) ld a,-1 cp (hl) push af push hl call nz,cursor pop hl pop af jp nz,main_loop inc (hl) call cursor jp main_loop get_y: ld a,(try_num) inc a jr nz,return_y ld c,37 ;answer balls ret return_y: ld b,a ld a,64 y_loop: sub 9 djnz y_loop ld c,a ret get_x: ;a=ball number to give x coordinates for ld b,a ld a,(try_num) inc a ld a,-1 ;x coordinate of first ball-7 jr nz,return_x ;answer balls ld a,68 return_x: inc b x_loop: add a,7 djnz x_loop ld b,a ret ;**************************toggle the ball toggle_ball: call draw_balls ld a,(ball_num) ld hl,ball1 ld e,a ld d,0 add hl,de inc (hl) ld a,(hl) cp 5 jr nz,finish_toggle xor a ld (hl),a finish_toggle: call draw_balls jp main_loop ;**************************drawing draw_balls: ld a,3 draw_ball_loop: push af push af push af call get_y pop af call get_x pop af ld h,ball1/256 ld l,a ld a,(hl) ;get ball number add a,a add a,a add a,a ;multiply by 8, since 8 bytes per sprite ld ix,ball_sprites ld e,a ld d,0 add ix,de call putsprite pop af dec a jp p,draw_ball_loop cursor: call get_y ld a,(ball_num) call get_x ld ix,highlight ;**************************putsprite putsprite: push af push bc ld h,$fc>>2 ld a,c add a,a add a,a ld l,a ld a,b rra add hl,hl rra add hl,hl rra or l ld l,a ld a,b and 7 ld (putspritex-1),a ld bc,$0800 putspritel: ex de,hl ld l,(ix) ld h,c inc ix jr putspritex putspritex: add hl,hl add hl,hl add hl,hl add hl,hl add hl,hl add hl,hl add hl,hl add hl,hl ex de,hl ld a,d xor (hl) ld (hl),a inc hl ld a,e xor (hl) ld (hl),a ld de,15 add hl,de djnz putspritel pop bc pop af ret ;**************************misc routines vputs: ld c,(hl) inc hl ld b,(hl) inc hl vputs_bc: ld (_penCol),bc jp _vputs getkey: halt call _getcsc or a jr z,getkey res 4,(iy+9) ret vert_line: ld h,$fc ld de,16 vert_line_loop: ld (hl),a add hl,de jr nc,vert_line_loop ret ;**************************data win_text: .db $17,$1a," Congratulations! You win!",0 lose_text: .db $21,$1a," You lose. Try again.",0 ball_sprites: .db %00000000 .db %00111100 .db %01000010 .db %01000010 .db %01000010 .db %01000010 .db %00111100 .db %00000000 .db %00000000 .db %00111100 .db %01111110 .db %01111110 .db %01111110 .db %01111110 .db %00111100 .db %00000000 .db %00000000 .db %00111100 .db %01101010 .db %01010110 .db %01101010 .db %01010110 .db %00111100 .db %00000000 .db %00000000 .db %00111100 .db %01100110 .db %01011010 .db %01011010 .db %01100110 .db %00111100 .db %00000000 .db %00000000 .db %00111100 .db %01011010 .db %01111110 .db %01111110 .db %01011010 .db %00111100 .db %00000000 highlight: .db %11111111 .db %10000001 .db %10000001 .db %10000001 .db %10000001 .db %10000001 .db %10000001 .db %11111111 black_box: .db %00000000 .db %00000000 .db %00000000 .db %11100000 .db %11100000 .db %11100000 .db %00000000 .db %00000000 white_box: .db %00000000 .db %00000000 .db %00000000 .db %11100000 .db %10100000 .db %11100000 .db %00000000 .db %00000000 title_screen: .db $91,$ff,$ab,$c7,$ff,$e3,$91,$ff,$04,$f8,$38,$7f .db $ff,$f8,$ff,$ff,$f0,$70,$c7,$ff,$e3,$91,$ff,$04 .db $f8,$38,$7f,$ff,$f8,$ff,$ff,$f0,$70,$ff,$ff,$e3 .db $91,$ff,$04,$f8,$38,$7c,$78,$60,$38,$71,$30,$70 .db $fc,$4f,$03,$91,$ff,$04,$f8,$30,$70,$10,$20,$30 .db $30,$10,$60,$c4,$06,$03,$91,$ff,$04,$f8,$10,$61 .db $00,$18,$e3,$10,$00,$20,$c4,$00,$03,$91,$ff,$04 .db $f8,$10,$63,$03,$18,$e3,$11,$80,$20,$c4,$60,$63 .db $91,$ff,$04,$f8,$10,$63,$03,$18,$e3,$11,$80,$20 .db $c4,$60,$63,$91,$ff,$04,$f8,$00,$63,$01,$18,$e3 .db $11,$80,$00,$c4,$60,$63,$91,$ff,$04,$f8,$02,$7c .db $10,$f8,$e0,$11,$f0,$04,$c4,$60,$63,$91,$ff,$04 .db $f8,$82,$78,$18,$38,$e0,$11,$f1,$04,$c4,$60,$63 .db $91,$ff,$04,$f8,$82,$71,$1e,$18,$e3,$f1,$f1,$04 .db $c4,$60,$63,$91,$ff,$04,$f8,$86,$63,$03,$18,$e3 .db $11,$f1,$0c,$c4,$60,$63,$91,$ff,$04,$f8,$c6,$63 .db $03,$18,$e3,$11,$f1,$8c,$c4,$60,$63,$91,$ff,$04 .db $f8,$c6,$63,$03,$18,$63,$11,$f1,$8c,$c4,$60,$63 .db $91,$ff,$04,$f8,$c6,$60,$10,$3c,$30,$11,$f1,$8c .db $c4,$60,$03,$91,$ff,$04,$f8,$ce,$70,$18,$7c,$38 .db $31,$f1,$9c,$c4,$62,$03,$91,$ff,$ff,$91,$ff,$ff .db $91,$ff,$54 main_screen: .db $07,$38,$00,$30,$00,$39,$D8,$03 .db $07,$38,$00,$30,$00,$39,$C0,$03 .db $07,$BB,$EF,$FB,$EF,$BD,$DB,$DF .db $07,$BB,$6F,$B3,$6D,$BD,$DA,$DB .db $07,$FB,$6D,$B3,$6D,$BF,$DA,$DB .db $07,$F9,$EF,$33,$6C,$3F,$DA,$DB .db $07,$F9,$E7,$B3,$EC,$3F,$DA,$DB .db $06,$FB,$6F,$B3,$6C,$37,$DA,$DB .db $06,$DB,$6D,$B3,$6C,$36,$DA,$DB .db $06,$DB,$6D,$BB,$6C,$36,$DA,$DB .db $06,$DB,$EF,$BB,$EC,$36,$DA,$DF try_num = $8100-4 ball_num = try_num+1 black = ball_num+1 white = black+1 ball1 = white+1 ;on $00 boundary ball2 = ball1+1 ball3 = ball2+1 ball4 = ball3+1 ansball1 = ball4+1 ansball2 = ansball1+1 ansball3 = ansball2+1 ansball4 = ansball3+1 tempball = ansball4+1 ;4 bytes tempans = tempball+4 ;4 bytes .end