Tictactoe
Tictactoe
Tictactoe
data segment
new_line db 13, 10, "$"
game_pointer db 9 DUP(?)
win_flag db 0
player db "0$"
stack segment
dw 128 dup(?)
ends
extra segment
ends
code segment
start:
; set segment registers
mov ax, data
mov ds, ax
mov ax, extra
mov es, ax
; game start
call set_game_pointer
main_loop:
call clear_screen
call update_draw
call check
call change_player
jmp main_loop
change_player:
lea si, player
xor ds:[si], 1
ret
update_draw:
mov bl, game_pointer[bx]
mov bh, 0
draw_x:
mov cl, "x"
jmp update
draw_o:
mov cl, "o"
jmp update
update:
mov ds:[bx], cl
ret
check:
call check_line
ret
check_line:
mov cx, 0
check_line_loop:
cmp cx, 0
je first_line
cmp cx, 1
je second_line
cmp cx, 2
je third_line
call check_column
ret
first_line:
mov si, 0
jmp do_check_line
second_line:
mov si, 3
jmp do_check_line
third_line:
mov si, 6
jmp do_check_line
do_check_line:
inc cx
mov bh, 0
mov bl, game_pointer[si]
mov al, ds:[bx]
cmp al, "_"
je check_line_loop
inc si
mov bl, game_pointer[si]
cmp al, ds:[bx]
jne check_line_loop
inc si
mov bl, game_pointer[si]
cmp al, ds:[bx]
jne check_line_loop
mov win_flag, 1
ret
check_column:
mov cx, 0
check_column_loop:
cmp cx, 0
je first_column
cmp cx, 1
je second_column
cmp cx, 2
je third_column
call check_diagonal
ret
first_column:
mov si, 0
jmp do_check_column
second_column:
mov si, 1
jmp do_check_column
third_column:
mov si, 2
jmp do_check_column
do_check_column:
inc cx
mov bh, 0
mov bl, game_pointer[si]
mov al, ds:[bx]
cmp al, "_"
je check_column_loop
add si, 3
mov bl, game_pointer[si]
cmp al, ds:[bx]
jne check_column_loop
add si, 3
mov bl, game_pointer[si]
cmp al, ds:[bx]
jne check_column_loop
mov win_flag, 1
ret
check_diagonal:
mov cx, 0
check_diagonal_loop:
cmp cx, 0
je first_diagonal
cmp cx, 1
je second_diagonal
ret
first_diagonal:
mov si, 0
mov dx, 4 ;tamanho do pulo
jmp do_check_diagonal
second_diagonal:
mov si, 2
mov dx, 2
jmp do_check_diagonal
do_check_diagonal:
inc cx
mov bh, 0
mov bl, game_pointer[si]
mov al, ds:[bx]
cmp al, "_"
je check_diagonal_loop
add si, dx
mov bl, game_pointer[si]
cmp al, ds:[bx]
jne check_diagonal_loop
add si, dx
mov bl, game_pointer[si]
cmp al, ds:[bx]
jne check_diagonal_loop
mov win_flag, 1
ret
game_over:
call clear_screen
jmp fim
set_game_pointer:
lea si, game_draw
lea bx, game_pointer
mov cx, 9
loop_1:
cmp cx, 6
je add_1
cmp cx, 3
je add_1
jmp add_2
add_1:
add si, 1
jmp add_2
add_2:
mov ds:[bx], si
add si, 2
inc bx
loop loop_1
ret
ret
mov ah, 0
int 10h
ret
ret
fim:
jmp fim
code ends
end start