robsoft

Darryl's connect 4

Jul 5th, 2017
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;; Darryl Sloan's finished Connect 4
  2. ; https://www.youtube.com/watch?v=uWqdk-1_NIg
  3. ; 5/7/2017
  4. ; https://gist.github.com/robsoft/96260761d30377be67ebf807d0a04397
  5.  
  6.  
  7.           org 50000
  8. last_k    equ 23560
  9.  
  10.           ld hl, udgs
  11.           ld (23675), hl
  12.  
  13.           ld a, 2
  14.           call 5633
  15.  
  16. ; draw game title
  17.           ld de, banner
  18.           ld bc, eobanr-banner
  19.           call 8252
  20.  
  21. ; draw board
  22. bdraw     ld de, board
  23.           ld bc, eoboard-board
  24.           call 8252
  25.           ld hl, board+1
  26.           ld a, (hl)
  27.           inc a
  28.           ld (hl), a
  29.           cp 16 ; finish looping when lines 12 to 16 are drawn
  30.           jr nz, bdraw
  31.  
  32. ; draw player line
  33.           ld de, player
  34.           ld bc, eoplyr-player
  35.           call 8252
  36.  
  37. ; show player
  38. ploop     ld hl, 22816
  39.           ld bc, (ppos)
  40.           add hl, bc
  41.           ld a, (pcol)
  42.           ld (hl), a
  43.  
  44. ; invite player input
  45.           ld hl, last_k
  46.           ld a, (hl)
  47.           cp 112
  48.           jr z, pright
  49.           cp 111
  50.           jr z, pleft
  51.           cp 32
  52.           jr z, pfire
  53.           cp 114
  54.           jr z, newgame
  55.           jr ploop
  56.  
  57. ; restart game
  58. newgame   ld hl, drop ; reset available drops for all 7 columns
  59.           ld b, 7
  60. rstdrp    ld (hl), 6 ; 6 means empty column (i.e. 6 drops remaining)
  61.           inc hl ; move forward 1 byte within DROP until all 7 are reset
  62.           djnz rstdrp
  63. ; clear board
  64.           ld a, 6 ; A keeps track of rows
  65.           ld b, 7 ; B keeps track of columns
  66.           ld hl, 22860 ; addr 22860 is top-left attribute of board
  67.           ld de, 25 ; 25 is the number of spaces from the end of one row to the beginning of the next
  68. empty     ld (hl), 15 ; 15 is paper blue, ink white, i.e. an empty slot
  69.           inc hl ; move forward one attribute
  70.           djnz empty ; repeat until a row of seven slots is cleared
  71.           ld b, 7 ; reset B in readiness for next row
  72.           add hl, de ; skip to start of next row
  73.           dec a ; subtract 1 from remaining rows
  74.           cp 0 ; if all 6 rows are complete, end
  75.           jp z, clrkey ; "
  76.           jr empty ; if not, repeat
  77.  
  78. ; player moves right
  79. pright    ld a, (ppos)
  80.           cp 18
  81.           jr z, ploop
  82.           ld hl, 22816
  83.           ld bc, (ppos)
  84.           add hl,bc
  85.           ld (hl), 63
  86.           ld a, (ppos)
  87.           inc a
  88.           ld (ppos), a
  89.           jp clrkey
  90.  
  91. ; player moves left
  92. pleft     ld a, (ppos)
  93.           cp 12
  94.           jr z, ploop
  95.           ld hl, 22816
  96.           ld bc, (ppos)
  97.           add hl, bc
  98.           ld (hl), 63
  99.           ld a, (ppos)
  100.           dec a
  101.           ld (ppos), a
  102.           jp clrkey
  103.  
  104. ; player fires
  105. pfire     ld a, (ppos) ; get mathematical x position of player (i.e. x=12 is 1)
  106.           sub 11 ; "
  107.           ld hl, drop-1 ; point at mem loc prior to first column (because the loop incs)
  108.           ld b, a ; use b to loop until correct column is reached
  109. cntr      inc hl ; increase HL to scan across drop vars
  110.           djnz cntr ; "
  111.           ld a, (hl) ; load number of rows to descend into a
  112.           ld (tdrop), a ; store A in TDROP (temporary drop)
  113.           cp 0 ; if no spaces remain in column, jump back to player input loop
  114.           jp z, clrkey ; "
  115.           ld hl, 22816 ; erase player from screen
  116.           ld bc, (ppos) ; "
  117.           add hl, bc ; "
  118.           ld (hl), 63 ; "
  119.  
  120. ; counter drops
  121.           ld a, (tdrop)
  122. desnd     ld bc, 32 ; redraw player 1 row lower
  123.           add hl, bc ; "
  124.           ld d, a ; remember A temporarily in D
  125.           ld a, (pcol+1)
  126.           ld (hl), a ; "
  127.           ld a, d ; put D back into A
  128.           ld b, 5 ; create a brief delay between counter movements
  129. stall     halt ; wait for an interrupt
  130.           djnz stall ; loop
  131.           ld (hl), 15 ; draw counter
  132.           dec a ; A controls iterations of loop
  133.           jr nz, desnd ; loop until descent finished
  134.           ld a, (pcol+1) ; redraw counter in resting position
  135.           ld (hl), a ; "
  136.  
  137. ; decrement remaining slots by 1
  138.           ld a, (ppos) ; get mathematical x position of player (i.e. x=12 is 1)
  139.           sub 11 ; "
  140.           ld hl, drop-1 ; point at mem loc prior to first column (because the loop incs)
  141.           ld b, a ; use b to loop until correct column is reached
  142. cntr2     inc hl ; increase HL to scan across drop vars
  143.           djnz cntr2 ; "
  144.           ld a, (tdrop) ; subtract one from slots remaining in selected column
  145.           dec a ; "
  146.           ld (tdrop), a ; "
  147.           ld (hl), a ; "
  148.  
  149. ; change colour for other player
  150.           ld hl, ppos ; set player position back to centre
  151.           ld (hl), 15 ; "
  152.           ld a, (pcol) ; add 4 to colour (changing red to yellow), for above board
  153.           add a, 4 ; "
  154.           ld (pcol), a ; "
  155.           ld a, (pcol+1) ; add 4 to colour (changing red to yellow), for within board
  156.           add a, 4 ; "
  157.           ld (pcol+1), a ; "
  158.           cp 18 ; if colour is invalid, jump to BADCOL
  159.           jr z, badcol ; "
  160.           jr clrkey ; clear keyboard
  161. badcol    ld a, 58 ; change invalid colour to red
  162.           ld (pcol), a ; "
  163.           ld a, 10 ; "
  164.           ld (pcol+1), a ; "
  165.           jr clrkey ; clear keyboard
  166.  
  167. ; clear last keypress and loop back for new input
  168. clrkey    ld hl, last_k
  169.           ld (hl), 0
  170.           jp ploop
  171.  
  172. ; variables
  173. ppos      defb 15, 0
  174. pcol      defb 58, 10
  175. drop      defb 6, 6, 6, 6, 6, 6, 6
  176. tdrop     defb 0
  177.  
  178. banner    defb 22, 3, 11, "CONNECT 4"
  179. eobanr    equ $
  180.  
  181. ; board setup
  182. board     defb 22, 10, 12
  183.           defb 16, 7, 17, 1, 144, 144, 144, 144, 144, 144, 144
  184. eoboard   equ $
  185.  
  186. player    defb 22, 9, 12
  187.           defb 16, 7, 17, 7, 144, 144, 144, 144, 144, 144, 144
  188. eoplyr    equ $
  189.  
  190. ; graphics setup
  191. udgs      defb 0, 24, 60, 126, 126, 60, 24, 0
  192.  
  193. end 50000
Advertisement