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

#### caballero

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

- There are 10 kind of people: those who know binary numbers and those who don't -

#### daydreamer

• Member
• Posts: 611
• Navy on its way...
##### 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.m
*wears a flameproof asbestos suit*

#### zedd151

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

Good starting point

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.
I'm not always the sharpest knife in the drawer, but I have my moments.

#### zedd151

• Member
• Posts: 864
##### 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.
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 »
I'm not always the sharpest knife in the drawer, but I have my moments.

#### jj2007

• Member
• Posts: 8956
• Assembler is fun ;-)
##### 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 ;-)

• Member
• Posts: 608
##### Re: Four in a Row - R&D
« Reply #35 on: May 10, 2018, 12:50:42 PM »

#### zedd151

• Member
• Posts: 864
##### Re: Four in a Row - R&D
« Reply #36 on: May 10, 2018, 01:45:20 PM »

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.
I'm not always the sharpest knife in the drawer, but I have my moments.

• Member
• Posts: 608
##### 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: 864
##### Re: Four in a Row - R&D
« Reply #38 on: May 10, 2018, 03:56:44 PM »
I'm not always the sharpest knife in the drawer, but I have my moments.

#### daydreamer

• Member
• Posts: 611
• Navy on its way...
##### 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.m
*wears a flameproof asbestos suit*

#### AW

• Member
• Posts: 1657
• 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: 864
##### 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.
I'm not always the sharpest knife in the drawer, but I have my moments.

#### AW

• Member
• Posts: 1657
• 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)

#### zedd151

• Member
• Posts: 864
##### 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
mov wc.hCursor, eax

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

mov hWnd, eax
invoke ShowWindow, hWnd, SW_SHOWNORMAL
invoke UpdateWindow, hWnd

StartLoop:
cmp eax, 0
je ExitLoop
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]
cmp [esi], ebx
jl @b
lea esi, cords
mov edi, 384
mov ebx, 512
@@:
invoke line, hWin, 0,  [esi], 0, [esi], edi
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
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
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
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,
WS_CHILD or WS_VISIBLE or SS_BITMAP,
x,y,0,0,hParent,ID,
hModule,NULL
mov hImage, eax
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.

« Last Edit: July 20, 2018, 03:57:44 AM by zedd151 »
I'm not always the sharpest knife in the drawer, but I have my moments.

#### zedd151

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

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...

fourx.zip
« Last Edit: July 20, 2018, 03:58:48 AM by zedd151 »
I'm not always the sharpest knife in the drawer, but I have my moments.