eris2010

Documentation: http://frombelow.net/projects/eris2010/
Clone: git clone https://git.frombelow.net/eris2010.git
Log | Files | Refs | README | LICENSE

computer_player_test.asm (15526B)


      1 ;;; Copyright 2021 Gerd Beuster (gerd@frombelow.net). This is free
      2 ;;; software under the GNU GPL v3 license or any later version. See
      3 ;;; COPYING in the root directory for details.
      4 
      5 computer_player_test:
      6 	.block
      7 	jsr clear_memory
      8         ;; jsr mirror_board_test
      9         ;; jsr win_by_column
     10         ;; jsr win_by_diagonal
     11         ;; jsr block_by_column
     12         ;; jsr fork_row_column
     13         ;; jsr test_block_fork
     14         ;; jsr test_play_opposite_corner
     15         ;; jsr test_play_empty_corner
     16         ;; jsr test_perfect_vs_random_computer_player
     17         ;; jsr test_random_vs_perfect_computer_player
     18         jsr test_perfect_vs_perfect_computer_player
     19 loop:
     20         jmp loop
     21 	.bend
     22 
     23 clear_memory:
     24 	;; Clear RAM for debugging purposes
     25 	.block
     26 	lda #<end_of_code
     27 	sta tmp
     28 	lda #>end_of_code
     29 	sta tmp+1
     30 clear_memory_loop:	
     31 	lda #$aa
     32 	sta (tmp)
     33 	inc tmp
     34 	bne clear_memory_loop
     35 	inc tmp+1
     36 	lda tmp+1
     37 	cmp #$80
     38 	bne clear_memory_loop
     39 	rts
     40 	.bend
     41 
     42 
     43 	
     44 SET_BOARD:	.macro
     45         lda #\1
     46         sta main_board
     47         lda #\2
     48         sta main_board+1
     49         lda #\3
     50         sta main_board+2
     51 	.endm
     52 
     53 CHECK_BOARD:	.macro
     54         lda board_mirrored
     55         cmp #\1
     56         bne check_board_err
     57         lda board_mirrored+1
     58         cmp #\2
     59         bne check_board_err
     60         lda board_mirrored+2
     61         cmp #\3
     62         beq check_board_cont
     63 check_board_err:	
     64         jmp error
     65 check_board_cont:	
     66 	.endm
     67 
     68 mirror_board_test:
     69         .block
     70         jsr init_board
     71         #mem.STORE_WORD board_mirrored,other_board_ptr
     72         ;; Test rotation of board
     73         ;; One piece on field 0.
     74         ;; Rotation should change nothing
     75         #SET_BOARD %00000011,%00000000,%00000000
     76         jsr mirror_board
     77         #CHECK_BOARD %00000011,%00000000,%00000000
     78         ;; First row to first column
     79         #SET_BOARD %00111001,%00000000,%00000000
     80         jsr mirror_board
     81         #CHECK_BOARD %00000001,%00000010,%00000011
     82         ;; Second row to Second column
     83         #SET_BOARD %00000000,%00111001,%00000000
     84         jsr mirror_board
     85         #CHECK_BOARD %00000100,%00001000,%00001100
     86         ;; Third row to third column
     87         #SET_BOARD %00000000,%00000000,%00111001
     88         jsr mirror_board
     89         #CHECK_BOARD %00010000,%00100000,%00110000
     90         ;; Complex board pattern
     91         #SET_BOARD %00100100,%00110011,%00011001
     92         jsr mirror_board
     93         #CHECK_BOARD %011100,%100001,%011110
     94 no_error:
     95         #io.PRINTSNL "OK"
     96         rts
     97 error:  
     98         #io.PRINTSNL "ERROR"
     99         rts
    100         .bend
    101 
    102         ;; Computer finishes game
    103         ;; by completing column
    104 win_by_column:
    105         ;; First column
    106         jsr init_board
    107         jsr computer_perfect_init
    108         #SET_FIELD piece_x, 0
    109         #SET_FIELD piece_x, 3
    110         jsr play_win_by_column
    111         jsr init_board
    112         jsr computer_perfect_init
    113         #SET_FIELD piece_x, 0
    114         #SET_FIELD piece_x, 6
    115         jsr play_win_by_column
    116         jsr init_board
    117         jsr computer_perfect_init
    118         #SET_FIELD piece_x, 3
    119         #SET_FIELD piece_x, 6
    120         jsr play_win_by_column
    121         ;; Second column
    122         jsr init_board
    123         jsr computer_perfect_init
    124         #SET_FIELD piece_x, 1
    125         #SET_FIELD piece_x, 4
    126         jsr play_win_by_column
    127         jsr init_board
    128         jsr computer_perfect_init
    129         #SET_FIELD piece_x, 1
    130         #SET_FIELD piece_x, 7
    131         jsr play_win_by_column
    132         jsr init_board
    133         jsr computer_perfect_init
    134         #SET_FIELD piece_x, 4
    135         #SET_FIELD piece_x, 7
    136         jsr play_win_by_column
    137         ;; Third column
    138         jsr init_board
    139         jsr computer_perfect_init
    140         #SET_FIELD piece_x, 2
    141         #SET_FIELD piece_x, 5
    142         jsr play_win_by_column
    143         jsr init_board
    144         jsr computer_perfect_init
    145         #SET_FIELD piece_x, 2
    146         #SET_FIELD piece_x, 8
    147         jsr play_win_by_column
    148         jsr init_board
    149         jsr computer_perfect_init
    150         #SET_FIELD piece_x, 5
    151         #SET_FIELD piece_x, 8
    152         jsr play_win_by_column
    153         rts
    154 play_win_by_column:     
    155         jsr print_board
    156         jsr io.putnl
    157         lda #piece_x
    158         jsr computer_perfect_ply
    159         pha
    160         jsr print_board
    161         pla
    162         jsr get_game_state
    163         cmp #piece_x
    164         bne win_by_column_error
    165         #io.PRINTSNL "OK"
    166         rts
    167 win_by_column_error:  
    168         #io.PRINTSNL "ERROR"
    169         rts
    170 
    171 win_by_diagonal:
    172         ;;
    173         ;; First diagonal
    174         ;; 
    175         ;; Lower right field empty
    176         jsr init_board
    177         jsr computer_perfect_init
    178         #SET_FIELD piece_x, 0
    179         #SET_FIELD piece_x, 4
    180         jsr play_win_by_diagonal
    181         ;; Middle field empty
    182         jsr init_board
    183         jsr computer_perfect_init
    184         #SET_FIELD piece_x, 0
    185         #SET_FIELD piece_x, 8
    186         jsr play_win_by_diagonal
    187         ;; Upper left field empty
    188         jsr init_board
    189         jsr computer_perfect_init
    190         #SET_FIELD piece_x, 4
    191         #SET_FIELD piece_x, 8
    192         jsr play_win_by_diagonal
    193         ;;
    194         ;; Second diagonal
    195         ;; 
    196         ;; Lower left field empty
    197         jsr init_board
    198         jsr computer_perfect_init
    199         #SET_FIELD piece_x, 2
    200         #SET_FIELD piece_x, 4
    201         jsr play_win_by_diagonal
    202         ;; Middle field empty
    203         jsr init_board
    204         jsr computer_perfect_init
    205         #SET_FIELD piece_x, 2
    206         #SET_FIELD piece_x, 6
    207         jsr play_win_by_diagonal
    208         ;; Upper right field empty
    209         jsr init_board
    210         jsr computer_perfect_init
    211         #SET_FIELD piece_x, 4
    212         #SET_FIELD piece_x, 6
    213         jsr play_win_by_diagonal
    214         rts
    215 play_win_by_diagonal:
    216         jsr print_board
    217         jsr io.putnl
    218         lda #piece_x
    219         jsr computer_perfect_ply
    220         pha
    221         jsr print_board
    222         pla
    223         jsr get_game_state
    224         cmp #piece_x
    225         bne win_by_diagonal_error
    226         #io.PRINTSNL "OK"
    227         rts
    228 win_by_diagonal_error:  
    229         #io.PRINTSNL "ERROR"
    230         rts
    231         
    232 block_by_column:
    233         ;; First column
    234         jsr init_board
    235         jsr computer_perfect_init
    236         #SET_FIELD piece_x, 0
    237         #SET_FIELD piece_x, 3
    238         jsr play_block_by_column
    239         jsr init_board
    240         jsr computer_perfect_init
    241         #SET_FIELD piece_x, 0
    242         #SET_FIELD piece_x, 6
    243         jsr play_block_by_column
    244         jsr init_board
    245         jsr computer_perfect_init
    246         #SET_FIELD piece_x, 3
    247         #SET_FIELD piece_x, 6
    248         jsr play_block_by_column
    249         ;; Second column
    250         jsr init_board
    251         jsr computer_perfect_init
    252         #SET_FIELD piece_x, 1
    253         #SET_FIELD piece_x, 4
    254         jsr play_block_by_column
    255         jsr init_board
    256         jsr computer_perfect_init
    257         #SET_FIELD piece_x, 1
    258         #SET_FIELD piece_x, 7
    259         jsr play_block_by_column
    260         jsr init_board
    261         jsr computer_perfect_init
    262         #SET_FIELD piece_x, 4
    263         #SET_FIELD piece_x, 7
    264         jsr play_block_by_column
    265         ;; Third column
    266         jsr init_board
    267         jsr computer_perfect_init
    268         #SET_FIELD piece_x, 2
    269         #SET_FIELD piece_x, 5
    270         jsr play_block_by_column
    271         jsr init_board
    272         jsr computer_perfect_init
    273         #SET_FIELD piece_x, 2
    274         #SET_FIELD piece_x, 8
    275         jsr play_block_by_column
    276         jsr init_board
    277         jsr computer_perfect_init
    278         #SET_FIELD piece_x, 5
    279         #SET_FIELD piece_x, 8
    280         jsr play_block_by_column
    281         rts
    282 play_block_by_column:     
    283         jsr print_board
    284         jsr io.putnl
    285         lda #piece_o
    286         jsr computer_perfect_ply
    287         pha
    288         jsr print_board
    289         pla
    290         jsr io.getc
    291         rts
    292 
    293 fork_row_column:
    294         .block
    295         ;; Row/column fork
    296         jsr init_board
    297         jsr computer_perfect_init
    298         #SET_FIELD piece_x, 0
    299         #SET_FIELD piece_x, 1
    300         #SET_FIELD piece_o, 2
    301         #SET_FIELD piece_o, 3
    302         #SET_FIELD piece_x, 6
    303         pha
    304         jsr print_board
    305         pla
    306         jsr io.putnl
    307         lda #piece_o
    308         jsr computer_perfect_ply
    309         pha
    310         jsr print_board
    311         pla
    312         ;; Check for correct ply
    313         #GET_FIELD main_board,5
    314         #CHECK_ERROR piece_o
    315         jsr io.getc
    316         ;; Row/diagonal fork
    317         jsr init_board
    318         ;; lda #$ac ; Test runs perfectly with this initialization of the lfsr
    319         ;; sta lfsr.state
    320         ;; sta lfsr.state+1
    321         jsr computer_perfect_init
    322         #SET_FIELD piece_x, 0
    323         #SET_FIELD piece_o, 3
    324         #SET_FIELD piece_x, 6
    325         jsr io.putnl
    326         pha
    327         jsr print_board
    328         pla
    329         lda #piece_x
    330         jsr computer_perfect_ply
    331         pha
    332         jsr io.putnl
    333         jsr print_board
    334         pla
    335         ;; Check for correct ply
    336         #GET_FIELD main_board,2
    337         #CHECK_ERROR piece_x
    338         jsr io.getc
    339         ;; Another Row/diagonal fork
    340         jsr init_board
    341         jsr computer_perfect_init
    342         #SET_FIELD piece_x, 0
    343         #SET_FIELD piece_x, 3
    344         #SET_FIELD piece_o, 6
    345         #SET_FIELD piece_o, 7
    346         #SET_FIELD piece_x, 8
    347         pha
    348         jsr print_board
    349         pla
    350         jsr io.putnl
    351         lda #piece_o
    352         jsr computer_perfect_ply
    353         pha
    354         jsr print_board
    355         pla
    356         ;; Check for correct ply
    357         #GET_FIELD main_board,4
    358         #CHECK_ERROR piece_o
    359         jsr io.getc
    360         rts
    361         .bend
    362 
    363 test_block_fork:
    364         .block
    365         jsr computer_perfect_init
    366         #mem.STORE_WORD main_board,board_ptr
    367         #SET_FIELD piece_x, 0
    368         #SET_FIELD piece_o, 3
    369         #SET_FIELD piece_x, 4
    370         #SET_FIELD piece_o, 8
    371         jsr io.putnl
    372         pha
    373         jsr print_board
    374         pla
    375         lda #piece_o
    376         jsr computer_perfect_ply
    377         pha
    378         jsr io.putnl
    379         jsr print_board
    380         pla
    381         ;; Check for correct ply
    382         #GET_FIELD main_board,1
    383         #CHECK_ERROR piece_o
    384         jsr io.getc
    385         ;; Check if computer creates
    386         ;; two-in-a-row/column/diagonal
    387         ;; when when a fork by the opponet is
    388         ;; possible.
    389         ;; The following scenario
    390         ;; can be created by the plys
    391         ;; X -> 5
    392         ;; O -> 1
    393         ;; X -> 9
    394         jsr computer_perfect_init
    395         #mem.STORE_WORD main_board,board_ptr
    396         #SET_FIELD piece_o, 0
    397         #SET_FIELD piece_x, 4
    398         #SET_FIELD piece_x, 8
    399         jsr io.putnl
    400         pha
    401         jsr print_board
    402         pla
    403         lda #piece_o
    404         jsr computer_perfect_ply
    405         pha
    406         jsr io.putnl
    407         jsr print_board
    408         pla
    409         ;; Check for correct ply
    410         #GET_FIELD main_board,1
    411         #CHECK_ERROR piece_o
    412         jsr io.getc
    413         ;; The following scenario
    414         ;; can be created by the plys
    415         ;; X -> 1
    416         ;; O -> 5
    417         ;; X -> 9
    418         jsr computer_perfect_init
    419         #mem.STORE_WORD main_board,board_ptr
    420         #SET_FIELD piece_x, 0
    421         #SET_FIELD piece_o, 4
    422         #SET_FIELD piece_x, 8
    423         jsr io.putnl
    424         pha
    425         jsr print_board
    426         pla
    427         lda #piece_o
    428         jsr computer_perfect_ply
    429         pha
    430         jsr io.putnl
    431         jsr print_board
    432         pla
    433         ;; Check for correct ply
    434         #GET_FIELD main_board,3
    435         #CHECK_ERROR piece_o
    436         jsr io.getc
    437         rts
    438         ;; The following scenario
    439         ;; can be created by the plys
    440         ;; X -> 5
    441         ;; O -> 1
    442         ;; X -> 9
    443         jsr computer_perfect_init
    444         #mem.STORE_WORD main_board,board_ptr
    445         #SET_FIELD piece_x, 4
    446         #SET_FIELD piece_o, 0
    447         #SET_FIELD piece_x, 8
    448         jsr io.putnl
    449         pha
    450         jsr print_board
    451         pla
    452         lda #piece_o
    453         jsr computer_perfect_ply
    454         pha
    455         jsr io.putnl
    456         jsr print_board
    457         pla
    458         ;; Check for correct ply
    459         #GET_FIELD main_board,2
    460         #CHECK_ERROR piece_o
    461         jsr io.getc
    462         rts
    463         .bend
    464 
    465 test_play_opposite_corner:
    466         .block
    467         jsr computer_perfect_init
    468         #mem.STORE_WORD main_board,board_ptr
    469         #SET_FIELD piece_x, 0
    470         #SET_FIELD piece_o, 4
    471         jsr io.putnl
    472         pha
    473         jsr print_board
    474         pla
    475         lda #piece_o
    476         jsr computer_perfect_ply
    477         pha
    478         jsr io.putnl
    479         jsr print_board
    480         pla
    481         ;; Check for correct ply
    482         #GET_FIELD main_board,8
    483         #CHECK_ERROR piece_o
    484         jsr io.getc
    485         rts
    486         .bend
    487 
    488 test_play_empty_corner:
    489         .block
    490         jsr computer_perfect_init
    491         #mem.STORE_WORD main_board,board_ptr
    492         #SET_FIELD piece_x, 4
    493         #SET_FIELD piece_o, 0
    494         jsr io.putnl
    495         pha
    496         jsr print_board
    497         pla
    498         lda #piece_o
    499         jsr computer_perfect_ply
    500         pha
    501         jsr io.putnl
    502         jsr print_board
    503         pla
    504         ;; Check for correct ply
    505         #GET_FIELD main_board,2
    506         #CHECK_ERROR piece_o
    507         jsr io.getc
    508         rts
    509         .bend
    510 
    511 test_perfect_vs_random_computer_player:
    512         .block
    513 game_loop:      
    514         ;; Set player X
    515         #mem.STORE_WORD computer_perfect_init, player_x_init_ptr
    516         #mem.STORE_WORD computer_perfect_ply, player_x_ply_ptr
    517         ;; Set player O
    518         #mem.STORE_WORD computer_random_init, player_o_init_ptr
    519         #mem.STORE_WORD computer_random_ply, player_o_ply_ptr
    520         jsr play_game
    521         jsr get_game_state
    522         cmp #piece_o            ; Random player
    523         bne game_loop           ; Should never win
    524         #io.PRINTSNL 'Error'
    525 error:
    526         jmp error
    527         .bend
    528 test_random_vs_perfect_computer_player:
    529         .block
    530 game_loop:      
    531         ;; Set player X
    532         #mem.STORE_WORD computer_random_init, player_x_init_ptr
    533         #mem.STORE_WORD computer_random_ply, player_x_ply_ptr
    534         ;; Set player O
    535         #mem.STORE_WORD computer_perfect_init, player_o_init_ptr
    536         #mem.STORE_WORD computer_perfect_ply, player_o_ply_ptr
    537         jsr play_game
    538         jsr get_game_state
    539         cmp #piece_x            ; Random player
    540         bne game_loop           ; Should never win
    541         #io.PRINTSNL 'Error'
    542 error:
    543         jmp error
    544         .bend
    545 test_perfect_vs_perfect_computer_player:
    546         .block
    547 game_loop:      
    548 	;;  Check if it is time to quit
    549 	jsr io.getc_nonblocking
    550 	tax
    551 	cmp #'q'		; Quit?
    552 	bne start
    553 	jmp ($fffc)		; Reset
    554 start:	
    555         ;; Set player X
    556         #mem.STORE_WORD computer_perfect_init, player_x_init_ptr
    557         #mem.STORE_WORD computer_perfect_ply, player_x_ply_ptr
    558         ;; Set player O
    559         #mem.STORE_WORD computer_perfect_init, player_o_init_ptr
    560         #mem.STORE_WORD computer_perfect_ply, player_o_ply_ptr
    561         jsr play_game
    562         jsr get_game_state
    563         cmp #piece_none         ; All games should end in
    564         beq game_loop           ; a draw.
    565         #io.PRINTSNL 'Error'
    566 error:
    567         jmp error
    568         .bend
    569 
    570 print_random_ply:
    571         .block
    572         ;; Debug output - Computer ply
    573         pha
    574         phx
    575         clc
    576         adc #'0'
    577         jsr io.putc
    578         #io.PRINTS ' plays '
    579         pla
    580         pha
    581         adc #'0'
    582         jsr io.putc
    583         jsr io.putnl
    584         plx
    585         pla
    586         rts
    587         .bend
    588 
    589 print_ply:
    590         .block
    591         ;; Debug output - Computer ply
    592         pha
    593         phy
    594         phx
    595         clc
    596         adc #'0'
    597         jsr io.putc
    598         #io.PRINTS ' plays '
    599         pla
    600         pha
    601         adc #'0'
    602         jsr io.putc
    603         lda #'/'
    604         jsr io.putc
    605         plx
    606         pla
    607         pha
    608         phx
    609         adc #'0'
    610         jsr io.putc
    611         jsr io.putnl
    612         plx
    613         ply
    614         pla
    615         rts
    616         .bend
    617