Author Topic: Four in a Row - R&D  (Read 1086 times)

caballero

  • Member
  • ****
  • Posts: 973
    • Abre Ojos Ensamblador
Re: Four in a Row - R&D
« Reply #30 on: May 09, 2018, 04:06:55 AM »
Good starting point :t
En un lugar de la Mancha de cuyo nombre no quiero acordarme

daydreamer

  • Member
  • ***
  • Posts: 417
Re: Four in a Row - R&D
« Reply #31 on: May 09, 2018, 05:19:41 AM »
good start
when researching unicode, I found out there is also non-language things that can be useful if you make simple:
 games,geometric shape s
Chess, Checkers/Draughts
Domino Tiles
Japanese Chess
Mahjong Tiles
Playing Cards
Card suits
Miscellaneous Symbols and Arrows
so with newer OS you maybe have unicode support when running console it would maybe be possible with a console card game,chess etc


Quote from Flashdance
Nick  :  When you give up your dream, you die.

zedd151

  • Member
  • ****
  • Posts: 592
  • -------------
Re: Four in a Row - R&D
« Reply #32 on: May 09, 2018, 06:33:32 AM »

Good starting point :t



good start
when researching unicode, I found out there is also non-language things that can be useful if you make simple....



Thanks to both of you. This idea has been rattling around in my head for some time.  Now I have the opportunity to see it through to fruition.       :biggrin:
@ Micros**t  --> 

Coming Soon "Taming Ten" - A tutorial for taming Windows 10, to gain more control over YOUR OWN computer.

zedd151

  • Member
  • ****
  • Posts: 592
  • -------------
Re: Four in a Row - R&D
« Reply #33 on: May 09, 2018, 09:25:12 AM »
Now THIS is not very elegant. Sure it gets the job done, but covfefe...

Code: [Select]

    .if byte ptr [esi] == -1
      .if byte ptr [esi+1] == 1
        .if byte ptr [esi+2] == 1
          .if byte ptr [esi+3] != 2
            .if byte ptr [esi+3] != 0
           
              mov byte ptr [esi], 2
              mov byte ptr [esi-8], -1
              jmp @f

            .endif
          .endif
        .endif
      .endif
    .endif


... will be prone to buggy code, especially when a few dozen are needed.    :icon_eek:
But in assembly, there's always another way.

I was just trying something, and got lost along the way.

Instead of the above code, how about using seperate byte sized  variables for the played
pieces? The code then could look like this....

Code: [Select]


    .if s40==-1 && s41==1 && s42==1 && 43!=2 || s40==-1 && s41 ==1 && s42==1 && s43!=0
        mov s40, 2
        mov s32, -1
    .endif

Looks a bit tidier, I think. But I don't know what the assembled cmps and jmps will look like.
« Last Edit: May 09, 2018, 02:57:43 PM by zedd151 »
@ Micros**t  --> 

Coming Soon "Taming Ten" - A tutorial for taming Windows 10, to gain more control over YOUR OWN computer.

jj2007

  • Member
  • *****
  • Posts: 8442
  • Assembler is fun ;-)
    • MasmBasic
Re: Four in a Row - R&D
« Reply #34 on: May 09, 2018, 05:48:09 PM »
Code: [Select]
    mov al, [esi+3]
    .if byte ptr [esi] == -1 && byte ptr [esi+1] == 1 && byte ptr [esi+2] == 1 && al != 2 && al
              mov byte ptr [esi], 2
              mov byte ptr [esi-8], -1
              jmp @f
    .endif

There is a more efficient version, though. Having an unconditional jmp inside a bunch of conditions always means that you could save two bytes ;-)

LordAdef

  • Member
  • ****
  • Posts: 588
Re: Four in a Row - R&D
« Reply #35 on: May 10, 2018, 12:50:42 PM »
how about a bitwise operation with mask instead of all those if?

zedd151

  • Member
  • ****
  • Posts: 592
  • -------------
Re: Four in a Row - R&D
« Reply #36 on: May 10, 2018, 01:45:20 PM »
how about a bitwise operation with mask instead of all those if?

I had thought about doing something like that, but as I've never done it - I'm not sure exactly how to implement it. But since the game is still in R&D, I'll experiment.  Thanks for the tip.   :icon14:
@ Micros**t  --> 

Coming Soon "Taming Ten" - A tutorial for taming Windows 10, to gain more control over YOUR OWN computer.

LordAdef

  • Member
  • ****
  • Posts: 588
Re: Four in a Row - R&D
« Reply #37 on: May 10, 2018, 01:52:35 PM »
one more thing, and this is actually a question:


.if byte ptr [esi] == -1


Does this really work?

zedd151

  • Member
  • ****
  • Posts: 592
  • -------------
Re: Four in a Row - R&D
« Reply #38 on: May 10, 2018, 03:56:44 PM »
@ Micros**t  --> 

Coming Soon "Taming Ten" - A tutorial for taming Windows 10, to gain more control over YOUR OWN computer.

daydreamer

  • Member
  • ***
  • Posts: 417
Re: Four in a Row - R&D
« Reply #39 on: May 10, 2018, 05:28:52 PM »
Zedd,backtrack in this thread to your earlier posted a great data structure for console version
There is ascii char data,but also strings and nibbles
And a nice structure made for x8 scaling indirect register

Quote from Flashdance
Nick  :  When you give up your dream, you die.

AW

  • Member
  • *****
  • Posts: 1302
  • Let's Make ASM Great Again!
Re: Four in a Row - R&D
« Reply #40 on: May 10, 2018, 06:50:09 PM »
I found this http://www.cplusplus.com/forum/general/120742/. I did not convert it to MASM because I have no time right now, but is straightforward.
However, I fixed it because it had a few bugs, built and tested. Looks cool right now. I include also the .exe


zedd151

  • Member
  • ****
  • Posts: 592
  • -------------
Re: Four in a Row - R&D
« Reply #41 on: May 11, 2018, 01:03:40 AM »
... Looks cool right now. I include also the .exe

Thanks aw27, but as I don't know c++ it won't help me too much. The only thing I can do with it is look at the exe under the hood.


@daydreamer

Thanks, I might just do that and fully develop the console version.
@ Micros**t  --> 

Coming Soon "Taming Ten" - A tutorial for taming Windows 10, to gain more control over YOUR OWN computer.

AW

  • Member
  • *****
  • Posts: 1302
  • Let's Make ASM Great Again!
Re: Four in a Row - R&D
« Reply #42 on: May 11, 2018, 02:02:54 AM »
All right, look at it as pseudo code then, a nod is as good as a wink (to a blind horse, according to the old say)  :biggrin:

zedd151

  • Member
  • ****
  • Posts: 592
  • -------------
Four in a Row - Two Player Mode
« Reply #43 on: May 13, 2018, 04:35:54 AM »
Code: [Select]

        include \masm32\include\masm32rt.inc

        WinMain     proto
        WndProc     proto :dword, :dword, :dword, :dword
        human       proto :dword, :dword, :dword
        computer    proto :dword, :dword, :dword
        checkdone   proto :dword
        randb       proto :dword       
        DisplayBmp  proto :dword, :dword, :dword, :dword, :dword
       
    .data
   
        topx        db 8 dup (0)
        board       db 40 dup (0)
        zz          db 8 dup (-1)

        count       dd 0
        hInstance   dd 0
        hWnd        dd 0
        hDC         dd 0
       
        hMemDC      dd 0
        hBoard      dd 0
        hRed        dd 0
        hBlue       dd 0
       
        column      dd 0
        xx          dd 0
        yy          dd 0
        done        dd 0
        turn        dd 1
       
        wc              WNDCLASSEX  <>
        bord            db "board.bmp", 0
        blue            db "blue.bmp", 0
        red             db "red.bmp", 0
        stWin           db "STATIC",0
        szDisplayName   db "Four in a Row", 0
        szClassName     db "Game", 0

    .code

    start:
        invoke WinMain
        invoke ExitProcess, hInstance

    WinMain proc
    local msg:MSG, Wwd:dword, Wht:dword, Wtx:dword, Wty:dword

        push ebx
       
        invoke GetModuleHandle, NULL
        mov hInstance, eax
        mov wc.cbSize, sizeof WNDCLASSEX
        mov wc.style, CS_HREDRAW or CS_VREDRAW or CS_BYTEALIGNWINDOW
        mov wc.lpfnWndProc, offset WndProc
        m2m wc.hInstance, hInstance
        invoke CreateSolidBrush, 0000ffffh
        mov wc.hbrBackground, eax
        mov wc.lpszClassName, offset szClassName
        invoke LoadCursor, NULL, IDC_ARROW
        mov wc.hCursor, eax
        invoke RegisterClassEx, addr wc
       
        push ebx
        mov Wwd, 455
        mov Wht, 414
        lea edi, Wtx
        mov edx, Wwd
        mov ebx, 0
        @@:
        push edx
        invoke GetSystemMetrics, ebx
        pop edx
        shr eax, 1
        shr edx, 1
        sub eax, edx
        mov [edi], eax
        lea edi, Wty
        mov edx, Wht
        inc ebx
        cmp ebx, 2
        jnz @b
        pop ebx
                             
        invoke CreateWindowEx, WS_EX_LEFT, addr szClassName,
        addr szDisplayName, WS_SYSMENU, Wtx, Wty, Wwd, Wht, 0, 0, hInstance, 0
        mov hWnd, eax
        invoke ShowWindow, hWnd, SW_SHOWNORMAL
        invoke UpdateWindow, hWnd

    StartLoop:
      invoke GetMessage,ADDR msg,NULL,0,0
      cmp eax, 0
      je ExitLoop
      invoke TranslateMessage, ADDR msg
      invoke DispatchMessage,  ADDR msg
      jmp StartLoop
    ExitLoop:
        pop ebx
        ret
    WinMain endp

    WndProc proc hWin:dword, uMsg:dword, wParam:dword, lParam:dword
    LOCAL Ps     :PAINTSTRUCT
        .if uMsg == WM_CREATE
                   
   
        .elseif uMsg == WM_LBUTTONDOWN
       
            .if turn==1
           
                invoke human, hWnd, lParam, addr board
               
                ret
               
            .elseif turn==2
           
                invoke computer, hWnd, lParam, addr board
               
            .endif
       
        .elseif uMsg == WM_PAINT
       
        jmp ttt
        cords dd 0,64,128,192,256,320,384,448   ; ------- coordinates
        ttt:
        push esi
        push edi
        push ebx
       
        lea esi, cords
        mov edi, 448
        mov ebx, 448
        @@:
        invoke line, hWin, 0, 0, [esi], edi, [esi]
        add esi, 4
        cmp [esi], ebx
        jl @b
        lea esi, cords
        mov edi, 384
        mov ebx, 512
        @@:
        invoke line, hWin, 0,  [esi], 0, [esi], edi
        add esi, 4
        cmp [esi], ebx
        jl @b

        pop ebx
        pop edi
        pop esi
       
        .elseif uMsg == WM_CLOSE
            invoke PostQuitMessage, NULL
            return 0

        .endif
        invoke DefWindowProc, hWin, uMsg, wParam, lParam
        ret
    WndProc endp

    human proc, hwin:dword, lparm:dword, bdoffs:dword
        local cnum:dword, xxx:dword, yyy:dword
       
        mov eax, lparm
        and eax, 0FFFFh
        shr eax,6 ; ------- column number (0-6)
        mov cnum, eax
        shl eax, 6 ; ------- y coord of row
        mov yyy, eax
        mov eax, bdoffs
        add eax, cnum ; -------- location of column on board
        cmp byte ptr [eax-8], -1
        jz ww
        mov edx, 320
    hhh:
        mov ecx, edx           
        mov xxx, ecx
        sar ecx, 3
    .if byte ptr [eax+ecx] == -1     ; if space is playable
        mov byte ptr [eax+ecx], 1    ; mark space occupied
        sub ecx, 8
        mov byte ptr [eax+ecx], -1   ; same column, mark space in row above playable
        add xxx, 3 ; adj for bmp size
        add yyy, 3 ; adj for bmp size
        invoke DisplayBmp, hwin, 120, yyy, xxx, 30
        mov turn, 2
        jmp ww
    .endif
        sub edx, 64
        jmp hhh
   
    ww:
        ret
    human endp

    checkdone proc bdoffs:dword ;---->> not implemented yet
        ret
    checkdone endp

    ;--->> At this point, the computer player is only a second human player
   
    computer proc hwin:dword, lparm:dword, bdoffs:dword
    local cnum:dword, xxx:dword, yyy:dword
       
        mov eax, lparm
        and eax, 0FFFFh
        shr eax,6 ; ------- column number (0-6)
        mov cnum, eax
        shl eax, 6 ; ------- y coord of row
        mov yyy, eax
        mov eax, bdoffs
        add eax, cnum ; -------- location of column on board
        cmp byte ptr [eax-8], -1
        jz ww
        mov edx, 320
    hhh:
        mov ecx, edx           
        mov xxx, ecx
        sar ecx, 3
    .if byte ptr [eax+ecx] == -1     ; if space is playable
        mov byte ptr [eax+ecx], 1    ; mark space occupied
        sub ecx, 8
        mov byte ptr [eax+ecx], -1   ; same column, mark space in row above playable
        add xxx, 3 ; adj for bmp size
        add yyy, 3 ; adj for bmp size
        invoke DisplayBmp, hwin, 110, yyy, xxx, 20
        mov turn, 1
        jmp ww
    .endif
        sub edx, 64
        jmp hhh
   
    ww:
        ret
    computer endp

   
    randb proc base:dword
    local random_seed:dword
        invoke GetTickCount
        add eax, random_seed
        xor edx, edx
        mov ecx, 127773
        div ecx
        mov ecx, eax
        mov eax, 432836
        mul edx
        sub ecx, eax
        xor edx, edx
        mov eax, ecx
        mov random_seed, ecx
        div base
        mov eax, edx
        ;inc eax
        ret
    randb endp

    DisplayBmp proc hParent:DWORD,bmpID:DWORD,x:DWORD,y:DWORD,ID:DWORD
        LOCAL hModule:DWORD
        LOCAL hBmp   :DWORD
        LOCAL hImage :DWORD
        invoke GetModuleHandle,NULL
        mov hModule, eax
        invoke CreateWindowEx,WS_EX_LEFT,
                ADDR stWin,NULL,
                WS_CHILD or WS_VISIBLE or SS_BITMAP,
                x,y,0,0,hParent,ID,
                hModule,NULL
        mov hImage, eax
        invoke LoadBitmap,hModule,bmpID
        mov hBmp, eax
        invoke SendMessage,hImage,STM_SETIMAGE,IMAGE_BITMAP,hBmp
        mov eax, hImage
        ret
    DisplayBmp endp

    end start

I know it's not much, but I AM working on the AI for the computer 'player'. Might even be a rewrite of a few things....

still a quirk with this one  ;)

code above, exe in attachment - use same resources, etc.
@ Micros**t  --> 

Coming Soon "Taming Ten" - A tutorial for taming Windows 10, to gain more control over YOUR OWN computer.

zedd151

  • Member
  • ****
  • Posts: 592
  • -------------
Artificial Intelligence?
« Reply #44 on: May 13, 2018, 02:50:01 PM »
Or simply artificial randomness.    :biggrin:

Attached is a version where the computer randomly places a piece immediately after the players move.
No checking of win/lose/draw condition yet.

Working on a new, improved 4 in a row...
@ Micros**t  --> 

Coming Soon "Taming Ten" - A tutorial for taming Windows 10, to gain more control over YOUR OWN computer.