;#**************************
;#*    X-0-X    =  MAIN()
;#************************** 21/10/2007
cB     equ cBLACK
cW     equ cWHITE

macro main_loop
{

; ®«ãç¨âì áâ ­¤ àâ­ë¥ æ¢¥â  ®ª®­
;========================================
mov eax, 48
mov ebx, 3
mov ecx, colors
mov edx, 40
int 0x40



; äã­ªæ¨ï ®âà¨á®¢ª¨ ®ª­ 
;========================================
draw_window:
        mov     eax,12                ; äã­ªæ¨ï 12:     á®®¡é¨âì á¨á⥬¥
        mov     ebx,1                 ; ¯®¤äã­ªæ¨ï 1,  ­ ç «® ¯¥à¥à¨á®¢ª¨
        int     0x40
                                      ; =====  ŽŠŽ !!!
        mov     eax,0                 ; äã­ªæ¨ï 0: ®¯à¥¤¥«¨âì ¨ ¢ë¢¥á⨠®ª­®
        mov     ebx,100*65536+250     ; [x ­ ç «ì­ë©] *65536 + [x à §¬¥à]
        mov     ecx,ebx  ;,100*65536+250     ; [y ­ ç «ì­ë©] *65536 + [y à §¬¥à]
        mov     edx, 0x34FFFFFF

        mov     esi, 0x808899AA       ; 梥⠧ £®«®¢ª  RRGGBB
        mov     edi, caption          ; § £®«®¢®ª
        int     0x40

;-----------------------------------#-----------------------------------------
; SCENE OF GAME - PLAY YARD - BOARD - DESK - PLAYING FIELD
;---------------
 playing_field                        ;macro in macs.inc
;-------------------------------------------------------------------------------
;                              ----ŠŽŠˆ----
;-------------------------------------------------------------------------------
;buttons:
        mov     eax,8                 ;
        mov     ebx,200*65536+35      ;
        mov     ecx,15*65536+20       ;
        mov     edx,200               ;== NEW GAME ==
        mov     esi, [colors+0x18]
        int     0x40
;--------------------------------------------
        mov     eax,4                 ;  ­ ¯¨á âì ⥪áâ ­  ª­®¯ª¥ NEW
        mov     ebx,210*65536+20
        mov     ecx,[colors+0x1C]
        mov     edx,textend
        mov     esi,3
        int     0x40
 endbut:
        mov     eax,12
        mov     ebx,2
        int     0x40
;============================= draw_window: END
 event_wait:
        mov     eax,23        ;==‘Ž›’ˆŸ==
        mov     ebx,100       ;¦¤ âì ᥪ㭤ã
        int     0x40
;-------------------------------------------------------------------------------
; ⨯ ᮡëâ¨ï ¢®§¢à éñ­ ¢ eax, ¤ «¥¥ ¯à®¢¥à塞, ª ª®¥ ᮡë⨥ ¯à®¨§®è«®
;-------------------------------------------------------------------------------
        cmp     eax,1        ; § ¯à®á ­  ¯¥à¥à¨á®¢ªã?
        je      redraw
        cmp     eax,2        ; ­ ¦ â  ª« ¢¨è  ª« ¢¨ âãàë?
        je      key
        cmp     eax,3        ; ­ ¦ â  ª­®¯ª  ¢ ®ª­¥ ¯à®£à ¬¬ë?
        je      button
        cmp     eax,6        ; mouse - click or move?
        je      MOUSE
        jmp     event_wait   ; ¢®§¢à é ¥¬áï ª ­ ç «ã 横«  ®¦¨¤ ­¨ï ᮡë⨩
                             ; ¥á«¨ ᮡë⨩  ­¥â
;-------------------------------------------------------------------------------
;  ®¡à ¡®âª  ᮡë⨩
;-------------------------------------------------------------------------------
redraw:
        call    draw_window  ; ¢ë§ë¢ ¥¬ äã­ªæ¨î draw_window ¨
        jmp     event_wait   ; ¢®§¢à é ¥¬áï ­ § ¤ ª 横«ã ®¦¨¤ ­¨ï
key:
        mov     eax,2        ; áç¨â뢠¥¬ ª®¤ ­ ¦ â®© ª« ¢¨è¨. ‚®§¢à é¥­ ¢ ah.
        int     0x40         ; Š« ¢¨è  ¤®«¦­  ¡ëâì ¯à®ç¨â ­  ¤«ï ®ç¨á⪨
        jmp     event_wait   ; ¢®§¢à â ª event_wait
button:
        mov     eax,17       ; áç¨â뢠¥¬ ¨¤¥­â¨ä¨ª â®à ­ ¦ â®© ª­®¯ª¨
        int     0x40         ; ¢®§¢à é¥­ ¢ ah.
;-------------------------------------------------------------------------------
; ᬮâਬ, ª ª ï ª­®¯ª  ¡ë«  ­ ¦ â  ¨ ᮮ⢥âáâ¢ãî騬 ®¡à §®¬ ॠ£¨à㥬.
;-------------------------------------------------------------------------------
        cmp     ah, 1          ; ª­®¯ª  á id=1 ("§ ªàëâì")?
        jne     noclose        ;
  clos0:                       ;
        mov     eax,-1         ; äã­ªæ¨ï -1: § ¢¥àè¨âì ¯à®£à ¬¬ã
        int     0x40           ;
  noclose:                     ; ª­®¯ª  id=200( "New" )
        mov dword[pox],0
        mov dword[pox+4],0     ;clear memory
        mov dword[pox_],0      ;rest and other stuff
        mov word[poxl],0       ; bits of pole X
        mov word[pool],0       ; bits of pole O
        ;mov byte[fill],0       ; ®¡­ã«ï¥¬ áç¥â稪 室®¢
        ;mov byte[xwin],0       ; flag - x winner
        ;mov byte[owin],0       ; flag - o winner
        jmp     redraw         ; ç¨á⮥ ¨£à®¢®¥ ¯®«¥ ==>>
;-------------------------------------------------------------------------------
; ᬮâਬ, £¤¥ ¡ë«  ­ ¦ â  ¬ëèì
;-------------------------------------------------------------------------------
 MOUSE:
        mov eax,37      ; ¬ëèìî..
        mov ebx,2
        int 0x40
        cmp eax,0       ; ..­¨ç¥£® ­¥ ­ ¦ â® - ¦¤ âì ᮡë⨩
        je  event_wait
                        ;­ ¦ â  ª­®¯ª  ¬ëè¨
        mov eax,37      ; cursor coords
        mov ebx,1
        int 0x40        ;eax=x*65536+y
        mov [px],eax    ;[px][py]  -  ª®®à¤¨­ â  ⪭ã⮣® ¬¥áâ 
	
	cmp [px],word 40
        jb  event_wait	
	
	cmp [px],word 200
        ja  event_wait	
	
	cmp [py],word 40
        jb  event_wait	
	
	cmp [py],word 200
        ja  event_wait	

          xor eax,eax
     g11:                      ;1st cell======
          cmp word[px],92
          ja g12               ;>92
          cmp word[py],92
          ja g12               ;>92

          ;mov dword[idx],0     ;N=(0..8)
          jmp puts

     g12:                      ;2nd cell========
          inc al
          cmp word[py],145
          ja g13               ;>142
          cmp word[px],92
          ja g13               ;>92
          ;mov dword[idx],1     ;N=(0..8)
          jmp puts
     g13:                      ;3=========
          inc al
          cmp word[px],92
          ja g21               ;>92
          ;mov dword[idx],2     ;N=(0..8)
          jmp puts
     g21:                      ;4========
          inc al
          cmp word[px],145
          ja g22
          cmp word[py],92
          ja g22
          ;mov dword[idx],3     ;N=(0..8)
          jmp puts
     g22:                      ;5=======
          inc al
          cmp word[px],145
          ja g23
          cmp word[py],145
          ja g23
          ;mov dword[idx],4     ;N=(0..8)
          jmp puts
     g23:                      ;6=======
          inc al
          cmp word[px],142
          ja g31               ;>142
          ;mov dword[idx],5     ;N=(0..8)
          jmp puts
     g31:                      ;7=======
          inc al
          cmp word[py],92
          ja g32
          ;mov dword[idx],6     ;N=(0..8)
          jmp puts
     g32:                      ;8======
          inc al
          cmp word[py],145
          ja g33
          ;mov dword[idx],7     ;N=(0..8)
          jmp puts
     g33:                      ;9======
          inc al
          ;mov dword[idx],8     ;N=(0..8)

   puts:                            ;㦥 ®¯à¥¤¥«¨«¨ ª«¥âªã
           push eax
           pop dword[idx]
          mov esi,dword[idx]        ;
          cmp byte[pox+esi],0       ;¯®«¥ ᢮¡®¤­®? (0-᢮¡®¤­®¥ ¯®«¥)
          jne event_wait            ;¢®§¢à é ¥¬áï ­ § ¤ ª 横«ã ®¦¨¤ ­¨ï

          shl esi,2                 ;*4
          mov eax,dword[pole+esi]
          mov dword[px],eax           ;x coord from tabl ->px
          ;mov ax,word[pole+esi+2]
          ;mov word[py],ax           ;y coord from tabl ->py

          call x_put                ;"X"

          mov esi,dword[idx]        ;esi - cell number  =0..8
          mov byte[pox+esi],'x'
          inc byte[fill]            ;turn+1

          xor eax,eax               ;eax=0
          inc eax                   ;bit=1       eax=1
          mov ecx,dword[idx]        ;  esi=n
          shl  ax,cl                ; <<n-bit   bit[n]=1
          or word[poxl],ax          ;set bit in [poxl]

          call x_win                ;¯à®¢¥àª  ¢ë¨£àëè  •
          cmp byte[xwin],1          ;ä« £ ¯®¡¥¤ë ãáâ ­®¢«¥­?
          jne del5                  ;­¥â - ¯¥à¥áª®ª ­  delay

        mov     eax,4                 ; äã­ª 4: ­ ¯¨á âì ⥪áâ 'WINNER!'
        mov     ebx,18*65536+215      ; [x ­ ç] *65536 + [y ­ ç]
        mov     ecx, [colors+0x20]    ; 梥â ⥪áâ  RRGGBB
        mov     edx,texts             ; 㪠§ â¥«ì ­  ­ ç «® ⥪áâ 
        mov     esi,textend-texts     ; ¤«¨­  ⥪áâ  ¢ ¡ ©â å
        int     0x40                  ;
        jmp   event_wait              ; ¢®§¢à é ¥¬áï ­ § ¤ ª 横«ã ®¦¨¤ ­¨ï

;===========================================================
 del5:    mov eax,5                 ;=== delay 0,5 sec ===
          mov ebx,50
          int 0x40

          cmp byte[fill],8    ;¢á¥ ¯®«ï § ­ïâë - 室¨âì ­¥ªã¤ 
          jnb event_wait      ;¢®§¢à é ¥¬áï ­ § ¤ ª 横«ã ®¦¨¤ ­¨ï - ª®­¥æ.

   rnew:                      ;
          call rndnext              ; á«ãç ©­®¥ ç¨á«® ¢ eax
          and eax, 0111b            ; eax = 0..7
          mov esi,eax               ; eax->esi
          call rndnext              ; á«ãç ©­®¥ ç¨á«® ¢ eax
          and eax, 01b              ; eax= 0..1
          add esi,eax               ; esi+=eax
                                    ;esi=0..8 - index
          cmp byte[pox+esi],0       ;á«ãç ©­®¥ ¯®«¥ ᢮¡®¤­®?
          jne rnew                  ;­¥â - ­®¢®¥ á«ãç ©­®¥ ç¨á«®

          cmp byte[pox+4],0         ;業âà ᢮¡®¤¥­?
          jne @F                    ;­¥â - á«ãç ©­ë© 室 (㦥 ¢ esi)
          mov esi,4                 ;¤   - 室¨¬ ¢ 業âà (4) ¢¬¥áâ® á«ãç.
     @@:
          mov dword[idx],esi        ;á®åà ­¨¬ ¢ idx
                                    ;esi=0..8 - index
          call o_com                ;¯à®¢¥àª  24å ª®¬¡¨­ æ¨© XX0
          call x_com                ;¯à®¢¥àª  24å ª®¬¡¨­ æ¨© 00*

          mov esi,dword[idx]        ;idx-¨­¤¥ªá ª«¥âª¨(0..8) ªã¤  室
          shl esi,2                 ; esi*4 - ¯®«¥ á®á⮨⠨§ § ¯¨á¥© ¯® dword
          mov eax,dword[pole+esi]     ;
          mov dword[px],eax           ;x coord from tabl ->px
        ;  mov ax,word[pole+esi+2]   ;
        ;  mov word[py],ax           ;y coord from tabl ->py
          call o_put                ; à¨á㥬 ­®«¨ª 'O'

          mov esi,dword[idx]
          mov byte[pox+esi],'o'
          inc byte[fill]            ;turn++

          xor eax,eax               ;eax=0
          inc eax                   ;bit=1       eax=1
          mov ecx,dword[idx]        ;  ecx=n
          shl  ax,cl                ; <<n-bit   bit[n]=1
          or word[pool],ax          ;set bit in [pool]

          call o_win
          cmp byte[owin],1
          jne event_wait

        mov     eax,4                 ; äã­ª 4: ­ ¯¨á âì ⥪áâ 'LOOSER!'
        mov     ebx,48*65536+215      ; [x ­ ç] *65536 + [y ­ ç]
        mov     ecx, [colors+0x20]    ; 梥â ⥪áâ  RRGGBB
        mov     edx,lootxt            ; 㪠§ â¥«ì ­  ­ ç «® ⥪áâ 
        mov     esi,text-lootxt       ; ¤«¨­  ⥪áâ  ¢ ¡ ©â å
        int     0x40                  ;
        jmp   event_wait              ; ¢®§¢à é ¥¬áï ­ § ¤ ª 横«ã ®¦¨¤ ­¨ï
;===========================================--
;
}