; ; TIC TAC TOE v1.0 ; David Phillips ; program started: 04/23/98 ; last update: 04/27/98 ; #include "ti86asm.inc" #include "asm86.h" .org _asm_exec_ram shelltitle: nop ; for the title line in shells (ASE) jp drawtitle ; jump to real start of program .dw $0000 .dw title drawtitle: call BUSY_OFF ; turn off the busy indicator call _flushallmenus ; clear menus, to use last 2 lines for text call _clrLCD ; clear the display ld hl,$0000 ; load in the upper-left corner (1, 1) ld (_curRow),hl ; actually change the position ld hl,titletext ; load the address of the title screen call _puts ; print the title screen call pause ; call our pause subroutine drawboard: call _clrLCD ; clear the screen ld hl,$0000 ; just like above ld (_curRow),hl ; ... ld hl,boardtext ; ... call _puts ; see title ld b,$09 ; we need to loop 9 times for 9 squares ld hl,board ; load in the address of the board clearboard: ld (hl),$00 ; set the square to 0 inc hl ; increase the pointer to the next square djnz clearboard ; decrease B by one and if not 0, loop gametop: ld hl,$1205 ; print turn at (6, 19) ld (_curRow),hl ; ... ld a,(turn) ; load who's turn it is so we can check it cp $01 ; check who's turn: X's == $01, O's == $02 jr z,xturn ; if it was set to 1 then jump to X's turn ld hl,playero ; it must be Y's turn--set text to Y's turn jr drawturn ; skip over code for X's turn xturn: ld hl,playerx ; set text to X's turn drawturn: call _puts ; finally we draw the text for whoever's turn keyloop: call GET_KEY ; wait for a key from the keyboard cp K_EXIT ; if it's exit, then we're done jp z,end ; jump to the end of game code cp K_1 ; was it key 1? jr z,key1 ; then jump to key 1 code cp K_2 jr z,key2 cp K_3 jr z,key3 cp K_4 jr z,key4 cp K_5 ; all this is the same jr z,key5 ; check if it's that key, then jump cp K_6 jr z,key6 cp K_7 jr z,key7 cp K_8 jr z,key8 cp K_9 jr z,key9 jr keyloop ; we didn't get our key, so get another one key1: ld de,$06 ; DE holds the offset of our square in memory ld hl,$0204 ; load in the cursor position for the piece jr updateboard ; now we can draw the piece key2: ld de,$07 ; load square position on board ld hl,$0604 ; load cursor position jr updateboard ; draw the square key3: ld de,$08 ld hl,$0A04 jr updateboard key4: ld de,$03 ld hl,$0202 jr updateboard key5: ld de,$04 ld hl,$0602 jr updateboard key6: ld de,$05 ld hl,$0A02 jr updateboard key7: ld de,$00 ld hl,$0200 jr updateboard key8: ld de,$01 ld hl,$0600 jr updateboard key9: ld de,$02 ld hl,$0A00 jr updateboard updateboard: ld (_curRow),hl ; set the cursor to the proper spot ld hl,board ; load in the address of the board add hl,de ; add the offset of our square xor a ; set a to 0 (faster than LD A,0) cp (hl) ; is this square empty (set to 0)? jr z,placepiece ; then we can put one here ld hl,$0006 ; print error message at (7, 1) ld (_curRow),hl ; actually set the cursor position ld hl,empty ; load the address of the error message call _puts ; print the error message call pause ; wait for the key press ld hl,$0006 ; where we printed the error message ld (_curRow),hl ; set the cursor to that spot ld hl,blankline ; load in the address of the blank line call _puts ; print the line jp gametop ; go to the top of the game loop placepiece: ld a,(turn) ; load in who's turn it is ld (hl),a ; mark the square as that person's cp $01 ; is it X's turn? jr z,xplace ; if it is jump there ld a,'O' ; must be O's, so draw an 'O' jr drawpiece ; skip over X's turn, since it's O's xplace: ld a,'X' ; draw an 'X' for X's turn drawpiece: call _putc ; put the character on the screen flipturn: ld hl,turn ; load in who's turn it is ld a,(hl) ; copy it to A for checking cp $01 ; is it X's turn? jr z,oturnnext ; then it's O's turn next, jump there ld (hl),$01 ; it must be X's turn next, set it to that jp gametop ; we're done, go on to next move oturnnext: ld (hl),$02 ; same as above, but for O's turn next jp gametop ; just like above pause: call GET_KEY ; wait for a key to be pressed cp K_EXIT ; is it exit? jr z,pausequit ; then jump to exit the program cp K_ENTER ; was it enter? jr nz,pause ; if it wasn't, jump to wait for another ret ; return, since it had to be pausequit: pop hl ; since we were CALLed and not RETing, we jr end ; have to pop the call address off the stack ; or the next RET will return to that address end: ld hl,$0000 ; just like every other time... ld (_curRow),hl ; ... call _clrLCD ; ... ret ; exit by returning to whatever called us title: .db "Tic Tac Toe v1.0 by David P",0 titletext: .db " T i c T a c T o e ", .db " ", .db " by David Phillips ", .db " electrum@tfs.net ", .db " ", .db " Copyright 1998 ", .db " ", .db " [2 Player] ",0 boardtext: .db " | | Keys:", .db " ---+---+--- 789 ", .db " | | 456 ", .db " ---+---+--- 123 ", .db " | | ", .db " Turn: ", .db " ", .db " Tic Tac Toe v1.0",0 playerx: .db "X's",0 playero: .db "O's",0 empty: .db "Space must be empty!",0 blankline: .db " ",0 turn: .db $01 board: .db $00,$00,$00, $00,$00,$00, $00,$00,$00 .end