Advertisement
Guest User

X-O

a guest
May 18th, 2014
754
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;***************************************
  2. ;*****Game developed by Saitei**********
  3. ;**************************(18.05.2014)*
  4.  
  5. stsg    segment para    stack   'stack'
  6.     dw  32  dup(?)  ;64 bytes
  7. stsg    ends
  8.  
  9. ;********TURN ENUM******
  10. X_TURN = 1
  11. O_TURN = 2
  12. ;***********************
  13.  
  14. ;*******COLORS**********
  15. YELLOW = 1110b
  16. RED = 1100b
  17. GREEN = 0010b
  18. WHITE = 1111b
  19. ;***********************
  20.  
  21. FIELD_X = 34
  22. FIELD_Y = 6
  23.  
  24. dssg    segment para    'data'
  25.     game_end    db  0
  26.     game_field  db  9   dup(0) 
  27.     turn    db  X_TURN
  28.     win db  0
  29.     border_horizont_char    db  196,0
  30.     border_vert_char    db  179,0
  31.     border_krest_char   db  197,0
  32.     x_wins_str  db  "X WINS!!!","$"
  33.     o_wins_str  db  "O WINS!!!","$"
  34.     nobody_wins_str db  "NOBODY WINS =(","$"
  35.     cont_str    db  "Restart? (y\n)","$"
  36. dssg    ends
  37.  
  38. cdsg    segment para    'code'
  39.     assume  cs:cdsg,ds:dssg,ss:stsg
  40.  
  41. cls proc
  42.     pusha
  43.     mov ax,0600h
  44.     mov bh,7
  45.     xor cx,cx
  46.     mov dx,184fh
  47.     int 10h
  48.     popa
  49.     ret
  50. endp
  51.  
  52. PrintColored    macro   char,_color
  53.     pusha
  54.     mov ah,09
  55.     mov al,char
  56.     mov bh,00
  57.     mov bx,_color
  58.     mov cx,01
  59.     int 10h
  60.     popa
  61. endm
  62.  
  63. SetCurPos   macro   x,y
  64.     pusha
  65.     mov bh,0
  66.     mov dh,y
  67.     mov dl,x
  68.     mov ah,2
  69.     int 10h
  70.     popa
  71. endm
  72.  
  73. HideBlinkingCursor  macro
  74.     push    cx
  75.     push    ax
  76.     mov ch,32
  77.     mov ah,1
  78.     int 10h
  79.     pop ax
  80.     pop cx
  81. endm
  82.  
  83. DrawBoard   macro
  84.     SetCurPos   FIELD_X,FIELD_Y
  85.     PrintColored    '0',RED
  86.     SetCurPos   FIELD_X+1,FIELD_Y
  87.     PrintColored    border_vert_char,YELLOW
  88.     SetCurPos   FIELD_X+2,FIELD_Y
  89.     PrintColored    '1',RED
  90.     SetCurPos   FIELD_X+3,FIELD_Y
  91.     PrintColored    border_vert_char,YELLOW
  92.     SetCurPos   FIELD_X+4,FIELD_Y
  93.     PrintColored    '2',RED
  94.     ;*********************************************
  95.     SetCurPos   FIELD_X,FIELD_Y+1
  96.     PrintColored    border_horizont_char,YELLOW
  97.     SetCurPos   FIELD_X+1,FIELD_Y+1
  98.     PrintColored    border_krest_char,YELLOW
  99.     SetCurPos   FIELD_X+2,FIELD_Y+1
  100.     PrintColored    border_horizont_char,YELLOW
  101.     SetCurPos   FIELD_X+3,FIELD_Y+1
  102.     PrintColored    border_krest_char,YELLOW
  103.     SetCurPos   FIELD_X+4,FIELD_Y+1
  104.     PrintColored    border_horizont_char,YELLOW
  105.     ;*********************************************
  106.     SetCurPos   FIELD_X,FIELD_Y+2
  107.     PrintColored    '3',RED
  108.     SetCurPos   FIELD_X+1,FIELD_Y+2
  109.     PrintColored    border_vert_char,YELLOW
  110.     SetCurPos   FIELD_X+2,FIELD_Y+2
  111.     PrintColored    '4',RED
  112.     SetCurPos   FIELD_X+3,FIELD_Y+2
  113.     PrintColored    border_vert_char,YELLOW
  114.     SetCurPos   FIELD_X+4,FIELD_Y+2
  115.     PrintColored    '5',RED
  116.     ;*********************************************
  117.     SetCurPos   FIELD_X,FIELD_Y+3
  118.     PrintColored    border_horizont_char,YELLOW
  119.     SetCurPos   FIELD_X+1,FIELD_Y+3
  120.     PrintColored    border_krest_char,YELLOW
  121.     SetCurPos   FIELD_X+2,FIELD_Y+3
  122.     PrintColored    border_horizont_char,YELLOW
  123.     SetCurPos   FIELD_X+3,FIELD_Y+3
  124.     PrintColored    border_krest_char,YELLOW
  125.     SetCurPos   FIELD_X+4,FIELD_Y+3
  126.     PrintColored    border_horizont_char,YELLOW
  127.     ;*********************************************
  128.     SetCurPos   FIELD_X,FIELD_Y+4
  129.     PrintColored    '6',RED
  130.     SetCurPos   FIELD_X+1,FIELD_Y+4
  131.     PrintColored    border_vert_char,YELLOW
  132.     SetCurPos   FIELD_X+2,FIELD_Y+4
  133.     PrintColored    '7',RED
  134.     SetCurPos   FIELD_X+3,FIELD_Y+4
  135.     PrintColored    border_vert_char,YELLOW
  136.     SetCurPos   FIELD_X+4,FIELD_Y+4
  137.     PrintColored    '8',RED
  138. endm
  139.  
  140. Print   proc   
  141.     pusha
  142.     cmp al,0
  143.     je  pex
  144.     mov ah,02h
  145.     cmp al,2
  146.     je  ytt
  147.     mov dl,'X'
  148.     PrintColored    dl,GREEN
  149.     jmp pex
  150.     ytt:
  151.     mov dl,'O'
  152.     PrintColored    dl,WHITE
  153.     pex:
  154.     popa
  155.     ret
  156. endp
  157.  
  158. CheckWin    proc
  159. pusha
  160.     ;******hor. 1 line********;
  161.     cmp game_field[0],al
  162.     jne second_line
  163.     cmp game_field[1],al
  164.     jne second_line
  165.     cmp game_field[2],al
  166.     jne second_line
  167.     mov win,al
  168.     jmp retu
  169.     ;******hor. 2 line*******;
  170.     second_line:
  171.     cmp game_field[3],al
  172.     jne th_line
  173.     cmp game_field[4],al
  174.     jne th_line
  175.     cmp game_field[5],al
  176.     jne th_line
  177.     mov win,al
  178.     jmp retu
  179.     ;*****hor. 3 line*******;
  180.     th_line:
  181.     cmp game_field[6],al
  182.     jne f_v_line
  183.     cmp game_field[7],al
  184.     jne f_v_line
  185.     cmp game_field[8],al
  186.     jne f_v_line
  187.     mov win,al
  188.     jmp retu
  189.     ;***vert. 1 line*******;
  190.     f_v_line:
  191.     cmp game_field[0],al
  192.     jne s_v_line
  193.     cmp game_field[3],al
  194.     jne s_v_line
  195.     cmp game_field[6],al
  196.     jne s_v_line
  197.     mov win,al
  198.     jmp retu
  199.     ;***vert. 2 line*******;
  200.     s_v_line:
  201.     cmp game_field[1],al
  202.     jne t_v_line
  203.     cmp game_field[4],al
  204.     jne t_v_line
  205.     cmp game_field[7],al
  206.     jne t_v_line
  207.     mov win,al
  208.     jmp retu
  209.     ;***vert. 3 line*******;
  210.     t_v_line:
  211.     cmp game_field[2],al
  212.     jne f_d_line
  213.     cmp game_field[5],al
  214.     jne f_d_line
  215.     cmp game_field[8],al
  216.     jne f_d_line
  217.     mov win,al
  218.     jmp retu
  219.     ;***diag. 1 line*******;
  220.     f_d_line:
  221.     cmp game_field[0],al
  222.     jne s_d_line
  223.     cmp game_field[4],al
  224.     jne s_d_line
  225.     cmp game_field[8],al
  226.     jne s_d_line
  227.     mov win,al
  228.     jmp retu
  229.     ;***diag. 2 line*******;
  230.     s_d_line:
  231.     cmp game_field[2],al
  232.     jne retu
  233.     cmp game_field[4],al
  234.     jne retu
  235.     cmp game_field[6],al
  236.     jne retu
  237.     mov win,al 
  238.     jmp retu       
  239. retu:
  240. popa
  241. ret
  242. endp
  243.  
  244. Draw    proc
  245.     pusha
  246.     SetCurPos   FIELD_X,FIELD_Y
  247.     mov al,game_field[0]
  248.     call    Print  
  249.     SetCurPos   FIELD_X+2,FIELD_Y
  250.     mov al,game_field[1]
  251.     call    Print
  252.     SetCurPos   FIELD_X+4,FIELD_Y
  253.     mov al,game_field[2]
  254.     call    Print
  255.     ;**********************************;
  256.     SetCurPos   FIELD_X,FIELD_Y+2
  257.     mov al,game_field[3]
  258.     call    Print
  259.     SetCurPos   FIELD_X+2,FIELD_Y+2
  260.     mov al,game_field[4]
  261.     call    Print
  262.     SetCurPos   FIELD_X+4,FIELD_Y+2
  263.     mov al,game_field[5]
  264.     call    Print
  265.     ;**********************************;
  266.     SetCurPos   FIELD_X,FIELD_Y+4
  267.     mov al,game_field[6]
  268.     call    Print
  269.     SetCurPos   FIELD_X+2,FIELD_Y+4
  270.     mov al,game_field[7]
  271.     call    Print
  272.     SetCurPos   FIELD_X+4,FIELD_Y+4
  273.     mov al,game_field[8]
  274.     call    Print
  275.     popa
  276.     ret
  277. endp
  278.  
  279. Move    macro
  280.     pusha
  281.     _err:
  282.     SetCurPos   1,1
  283.     mov ah,1h
  284.     int 21h
  285.     SetCurPos   1,1
  286.     PrintColored    ' ',RED
  287.     mov ah,0
  288.     xor si,si
  289.     sub ax,48
  290.     add si,ax
  291.     cmp si,0
  292.     jl  _err
  293.     cmp si,8
  294.     jg  _err
  295.    
  296.     cmp game_field[si],0
  297.     jne _err
  298.     mov al,turn
  299.     mov game_field[si],al
  300.    
  301.     cmp turn,O_TURN
  302.     je  YT
  303.     mov turn,O_TURN
  304.     jmp EEX
  305.     YT:
  306.     mov turn,X_TURN
  307.     EEX:   
  308.     popa
  309. endm
  310.  
  311. Update  proc
  312.     Move   
  313. ret
  314. endp
  315.  
  316. PrintStr    macro   str
  317. push    ax
  318. push    dx
  319.     mov dx,offset   str
  320.     mov ah,9
  321.     int 21h
  322. pop dx
  323. pop ax
  324. endm
  325.  
  326. prg proc    far
  327.     push    ds
  328.     xor ax,ax
  329.     push    ax
  330.     mov ax,dssg
  331.     mov ds,ax
  332.    
  333.     RESTART:
  334.     mov cx,9
  335.     mov si,8
  336.     cc:
  337.         mov game_field[si],0
  338.         dec si 
  339.     loop    cc
  340.  
  341.     mov turn,X_TURN
  342.     mov win,0
  343.     call    cls
  344.     HideBlinkingCursor
  345.     DrawBoard
  346.    
  347.     GAME_LOOP:
  348.         call    Update
  349.         call    Draw
  350.         cmp game_end,1
  351.         je  GAME_EXIT
  352.        
  353.         ;******CHECK******
  354.         mov al,X_TURN
  355.         call    CheckWin
  356.         mov al,O_TURN
  357.         call    CheckWin
  358.         cmp win,0
  359.         jne W_I_N
  360.         mov cx,9
  361.         mov si,8
  362.         sss:
  363.             cmp game_field[si],0
  364.             je  GAME_LOOP
  365.             dec si
  366.         loop    sss
  367.         jmp W_I_N
  368.         ;*****************
  369.     jmp GAME_LOOP
  370.     W_I_N:
  371.         SetCurPos   FIELD_X-2,FIELD_Y-3
  372.         cmp win, X_TURN
  373.         je  XWINS
  374.         cmp win,O_TURN
  375.         je  OWINS
  376.             PrintStr    nobody_wins_str
  377.             SetCurPos   FIELD_X-4,FIELD_Y-2
  378.             PrintStr    cont_str
  379.         jmp sc
  380.         XWINS:
  381.             PrintStr    x_wins_str
  382.             SetCurPos   FIELD_X-4,FIELD_Y-2
  383.             PrintStr    cont_str       
  384.             jmp sc
  385.         OWINS:
  386.             PrintStr    o_wins_str
  387.             SetCurPos   FIELD_X-4,FIELD_Y-2
  388.             PrintStr    cont_str
  389.             jmp sc
  390.         sc:
  391.             mov ah,07h
  392.             int 21h
  393.             cmp al,'y'
  394.             je  Restart
  395.             cmp al,'Y'
  396.             je  Restart
  397.             cmp al,'n'
  398.             je  GAME_EXIT
  399.             cmp al,'N'
  400.             je  GAME_EXIT      
  401.         jmp     sc
  402.     GAME_EXIT:
  403. ret
  404. prg endp
  405. cdsg    ends
  406. end prg
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement