home *** CD-ROM | disk | FTP | other *** search
/ ticalc.org / ticalc_org_rev_b.iso / archives / 86 / asm / source / tictac.asm < prev    next >
Encoding:
Assembly Source File  |  2001-07-01  |  5.7 KB  |  218 lines

  1. ; TIC TAC TOE v1.0
  2. ; David Phillips <electrum@tfs.net>
  3. ; program started: 04/23/98
  4. ; last     update: 04/27/98
  5. ;
  6.  
  7. #include "ti86asm.inc"
  8. #include "asm86.h"
  9.  
  10. .org _asm_exec_ram
  11.  
  12. shelltitle:
  13.     nop                        ; for the title line in shells (ASE)
  14.  
  15.     jp drawtitle            ; jump to real start of program
  16.  
  17.  
  18.  
  19.     .dw $0000
  20.  
  21.     .dw title
  22.  
  23. drawtitle:
  24.     call BUSY_OFF            ; turn off the busy indicator
  25.     call _flushallmenus        ; clear menus, to use last 2 lines for text
  26.     call _clrLCD            ; clear the display
  27.     ld hl,$0000                ; load in the upper-left corner (1, 1)
  28.     ld (_curRow),hl            ; actually change the position
  29.     ld hl,titletext            ; load the address of the title screen
  30.     call _puts                ; print the title screen
  31.     call pause                ; call our pause subroutine
  32.  
  33. drawboard:
  34.     call _clrLCD            ; clear the screen
  35.     ld hl,$0000                ; just like above
  36.     ld (_curRow),hl            ; ...
  37.     ld hl,boardtext            ; ...
  38.     call _puts                ; see title
  39.  
  40.     ld b,$09                ; we need to loop 9 times for 9 squares
  41.     ld hl,board                ; load in the address of the board
  42. clearboard:
  43.     ld (hl),$00                ; set the square to 0
  44.     inc hl                    ; increase the pointer to the next square
  45.     djnz clearboard            ; decrease B by one and if not 0, loop
  46.  
  47. gametop:
  48.     ld hl,$1205                ; print turn at (6, 19)
  49.     ld (_curRow),hl            ; ...
  50.     ld a,(turn)                ; load who's turn it is so we can check it
  51.     cp $01                    ; check who's turn: X's == $01, O's == $02
  52.     jr z,xturn                ; if it was set to 1 then jump to X's turn
  53.     ld hl,playero            ; it must be Y's turn--set text to Y's turn
  54.     jr drawturn                ; skip over code for X's turn
  55. xturn:
  56.     ld hl,playerx            ; set text to X's turn
  57. drawturn:
  58.     call _puts                ; finally we draw the text for whoever's turn
  59.  
  60. keyloop:
  61.     call GET_KEY            ; wait for a key from the keyboard
  62.     cp K_EXIT                ; if it's exit, then we're done
  63.     jp z,end                ; jump to the end of game code
  64.     cp K_1                    ; was it key 1?
  65.     jr z,key1                ; then jump to key 1 code
  66.     cp K_2
  67.     jr z,key2
  68.     cp K_3
  69.     jr z,key3
  70.     cp K_4
  71.     jr z,key4
  72.     cp K_5                    ; all this is the same
  73.     jr z,key5                ; check if it's that key, then jump
  74.     cp K_6
  75.     jr z,key6
  76.     cp K_7
  77.     jr z,key7
  78.     cp K_8
  79.     jr z,key8
  80.     cp K_9
  81.     jr z,key9
  82.     jr keyloop                ; we didn't get our key, so get another one
  83. key1:
  84.     ld de,$06                ; DE holds the offset of our square in memory
  85.     ld hl,$0204                ; load in the cursor position for the piece
  86.     jr updateboard            ; now we can draw the piece
  87. key2:
  88.     ld de,$07                ; load square position on board
  89.     ld hl,$0604                ; load cursor position
  90.     jr updateboard            ; draw the square
  91. key3:
  92.     ld de,$08
  93.     ld hl,$0A04
  94.     jr updateboard
  95. key4:
  96.     ld de,$03
  97.     ld hl,$0202
  98.     jr updateboard
  99. key5:
  100.     ld de,$04
  101.     ld hl,$0602
  102.     jr updateboard
  103. key6:
  104.     ld de,$05
  105.     ld hl,$0A02
  106.     jr updateboard
  107. key7:
  108.     ld de,$00
  109.     ld hl,$0200
  110.     jr updateboard
  111. key8:
  112.     ld de,$01
  113.     ld hl,$0600
  114.     jr updateboard
  115. key9:
  116.     ld de,$02
  117.     ld hl,$0A00
  118.     jr updateboard
  119.  
  120. updateboard:
  121.     ld (_curRow),hl            ; set the cursor to the proper spot
  122.     ld hl,board                ; load in the address of the board
  123.     add hl,de                ; add the offset of our square
  124.     xor a                    ; set a to 0 (faster than LD A,0)
  125.     cp (hl)                    ; is this square empty (set to 0)?
  126.     jr z,placepiece            ; then we can put one here
  127.     ld hl,$0006                ; print error message at (7, 1)
  128.     ld (_curRow),hl            ; actually set the cursor position
  129.     ld hl,empty                ; load the address of the error message
  130.     call _puts                ; print the error message
  131.     call pause                ; wait for the key press
  132.     ld hl,$0006                ; where we printed the error message
  133.     ld (_curRow),hl            ; set the cursor to that spot
  134.     ld hl,blankline            ; load in the address of the blank line
  135.     call _puts                ; print the line
  136.     jp gametop                ; go to the top of the game loop
  137. placepiece:
  138.     ld a,(turn)                ; load in who's turn it is
  139.     ld (hl),a                ; mark the square as that person's
  140.     cp $01                    ; is it X's turn?
  141.     jr z,xplace                ; if it is jump there
  142.     ld a,'O'                ; must be O's, so draw an 'O'
  143.     jr drawpiece            ; skip over X's turn, since it's O's
  144. xplace:
  145.     ld a,'X'                ; draw an 'X' for X's turn
  146. drawpiece:
  147.     call _putc                ; put the character on the screen
  148.  
  149. flipturn:
  150.     ld hl,turn                ; load in who's turn it is
  151.     ld a,(hl)                ; copy it to A for checking
  152.     cp $01                    ; is it X's turn?
  153.     jr z,oturnnext            ; then it's O's turn next, jump there
  154.     ld (hl),$01                ; it must be X's turn next, set it to that
  155.     jp gametop                ; we're done, go on to next move
  156. oturnnext:
  157.     ld (hl),$02                ; same as above, but for O's turn next
  158.     jp gametop                ; just like above
  159.     
  160.  
  161. pause:
  162.     call GET_KEY            ; wait for a key to be pressed
  163.     cp K_EXIT                ; is it exit?
  164.     jr z,pausequit            ; then jump to exit the program
  165.     cp K_ENTER                ; was it enter?
  166.     jr nz,pause                ; if it wasn't, jump to wait for another
  167.     ret                        ; return, since it had to be
  168. pausequit:
  169.     pop hl                    ; since we were CALLed and not RETing, we
  170.     jr end                    ; have to pop the call address off the stack
  171.                             ; or the next RET will return to that address
  172. end:
  173.     ld hl,$0000                ; just like every other time...
  174.     ld (_curRow),hl            ; ...
  175.     call _clrLCD            ; ...
  176.     ret                        ; exit by returning to whatever called us
  177.  
  178. title:
  179.     .db "Tic Tac Toe v1.0 by David P",0
  180.  
  181. titletext:
  182.     .db " T i c  T a c  T o e ",
  183.     .db "                     ",
  184.     .db "  by David Phillips  ",
  185.     .db "  electrum@tfs.net   ",
  186.     .db "                     ",
  187.     .db "   Copyright 1998    ",
  188.     .db "                     ",
  189.      .db "     [2 Player]     ",0
  190.  
  191. boardtext:
  192.     .db "    |   |       Keys:",
  193.     .db " ---+---+---     789 ",
  194.     .db "    |   |        456 ",
  195.     .db " ---+---+---     123 ",
  196.     .db "    |   |            ",
  197.     .db "            Turn:    ",
  198.     .db "                     ",
  199.     .db "  Tic Tac Toe v1.0",0
  200.  
  201. playerx:
  202.     .db "X's",0
  203. playero:
  204.     .db "O's",0
  205.  
  206. empty:
  207.     .db "Space must be empty!",0
  208.  
  209. blankline:
  210.     .db "                    ",0
  211.  
  212. turn:
  213.     .db $01
  214.  
  215. board:
  216.     .db $00,$00,$00,
  217.         $00,$00,$00,
  218.         $00,$00,$00
  219.  
  220. .end
  221.