Advertisement
Marrin

Reversi moves

Mar 3rd, 2013
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.         !to "test.prg", cbm
  3.         !convtab pet
  4.        
  5.         chrout = $ffd2
  6.         getchr = $ffe4
  7.        
  8.         turn_length = $02
  9.         xt = $9e
  10.         yt = $9f
  11.         xd = $b0
  12.         yd = $b1
  13.         current_player = $b2
  14.         buffer_length = $b3
  15.         x = $b4
  16.         y = $b5
  17.        
  18.         * = $c000
  19.  
  20.         ldy #63                 ; Init board, current_player & other_player_mod
  21.         lda #"."
  22. -       sta board,y
  23.         dey
  24.         bpl -
  25.         lda #"w"
  26.         sta current_player
  27.         sta board + 3*8+3
  28.         sta board + 4*8+4
  29.         lda #"b"
  30.         sta other_player_mod+1
  31.         sta board + 3*8+4
  32.         sta board + 4*8+3
  33.        
  34.         jmp print_board
  35.  
  36. main_loop
  37.         lda #0                  ; Read line into buffer.
  38.         sta buffer_length
  39. -       jsr getchr
  40.         beq -
  41.         ldy buffer_length
  42.         sta buffer,y
  43.         inc buffer_length
  44.         cmp #3                  ; RUN/STOP
  45.         beq exit
  46.         jsr chrout
  47.         cmp #13                 ; RETURN
  48.         bne -
  49.  
  50.         ldy #end_txt_length - 1 ; buffer == "end\n" ?
  51. -       lda buffer,y
  52.         cmp end_txt,y
  53.         bne +
  54.         dey
  55.         bpl -
  56. exit
  57.         rts
  58. +
  59.         lda buffer              ; convert coordinate
  60.         sec
  61.         sbc #"a"
  62.         sta x
  63.         lda buffer+1
  64.         clc
  65.         sbc #"0"
  66.         sta y
  67.        
  68.         asl                     ; board(x,y) = current_player
  69.         asl
  70.         asl
  71.         ora x
  72.         tay
  73.         lda current_player
  74.         sta board,y
  75.        
  76.         lda #-1                 ; for xd = -1 to...
  77.         sta xd
  78. outer_loop
  79.         lda #-1                 ; for yd = -1 to...
  80.         sta yd
  81. inner_loop
  82.         ;lda yd                  ; if (xd | yd) = 0 then continue.
  83.         ora xd
  84.         beq inner_loop_continue
  85.         lda x                   ; xt, yt = x, y
  86.         sta xt
  87.         lda y
  88.         sta yt
  89.         lda #0                  ; turn_length = 0
  90.         sta turn_length
  91.  
  92. check_line_loop
  93.         clc                     ; xt += xd
  94.         lda xt
  95.         adc xd
  96.         sta xt
  97.         clc                     ; yt += yd
  98.         lda yt
  99.         adc yd
  100.         sta yt
  101.        
  102.         ;lda yt                  ; check if xt and yt between 0 and 7
  103.         ora xt
  104.         and #%11111000
  105.         bne no_turns
  106.        
  107.         lda yt                  ; Y = yt*8+xt
  108.         asl
  109.         asl
  110.         asl
  111.         ora xt
  112.         tay
  113.        
  114.         lda board,y             ; A = board(xt,yt)
  115. other_player_mod                ; A != other_player ?
  116.         cmp #0
  117.         bne line_end
  118.        
  119.         tya                     ; store index into board in turn_coordinates.
  120.         ldy turn_length
  121.         sta turn_coordinates,y
  122.         inc turn_length
  123.         bne check_line_loop
  124.        
  125. line_end
  126.         cmp current_player      ; board(xt,yt) == current_player ?
  127.         beq check_line_loop_end
  128. no_turns
  129.         lda #0
  130.         sta turn_length
  131. check_line_loop_end
  132.        
  133.         lda turn_length         ; turns ?
  134.         beq skip_turn_loop
  135.         tay                     ; do turns.
  136.         lda current_player
  137. turn_loop
  138.         ldx turn_coordinates-1,y
  139.         sta board,x
  140.         dey
  141.         bne turn_loop
  142. skip_turn_loop
  143.  
  144. inner_loop_continue
  145.         inc yd                  ; next yd
  146.         lda yd
  147.         cmp #2
  148.         bne inner_loop
  149.        
  150.         inc xd                  ; next xd
  151.         lda xd
  152.         cmp #2
  153.         bne outer_loop
  154.  
  155.         lda current_player      ; swap players.
  156.         ldy other_player_mod+1
  157.         sta other_player_mod+1
  158.         sty current_player
  159.        
  160. print_board
  161.         ldy #0
  162. print_loop
  163.         tya                     ; index divisible by 8 ?
  164.         and #%00000111
  165.         bne +
  166.         lda #13                 ; print newline.
  167.         jsr chrout
  168. +       lda board,y
  169.         jsr chrout
  170.         iny
  171.         cpy #64
  172.         bne print_loop
  173.         lda #13
  174.         jsr chrout
  175.        
  176.         jmp main_loop
  177. ;--------------------------------------
  178. !align 255, 0, 0
  179. end_txt
  180.         !pet "end", 13
  181.         end_txt_length = * - end_txt
  182. ;--------------------------------------
  183. !align 255, 0, 0
  184. board
  185.         !fill 64
  186. buffer
  187.         !fill 16
  188. turn_coordinates
  189.         !fill 8
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement