locate matrix (FIXED by fixing matrix)

Advanced game related topics
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

locate matrix (FIXED by fixing matrix)

Post by Rook Zimbabwe »

OK this is a rewrite:

I had a problem locating tiles im my array. I had the problem because I was using a 2D matrix to hold locations for HEXAGONAL tiles.

Yes, making a SQUARE tiled boardgame would have been far too easy.

But I hard coded this NEW example that does work.

I took out the logic, I don't wanna make it too easy for you. If you need it hit me a PM! :D

Code: Select all

; hex display

Enumeration
    #Board
    #Player
    #CPU
    #Cursor
    #CursorPoint
EndEnumeration

#Wide = 800
#High = 600

Structure Tile
    x.f
    y.f
    ax.f
    ay.f
EndStructure

Global NewList Tile.Tile()

Global Dim BOARD(8,5) ; array to hold WHAT type of tile goes where (0 = BLANK, 1 = PLAYER, 2=CPU)
Global Dim BX(8,5) ; X locations for tiles
Global Dim BY(8,5) ; Y locations for tiles

Global N
Global NE
Global SE
Global S
Global SW
Global NW
Global LEGALFLAG


    BX(0,0) = 134 ; + 64
    BX(0,2) = 229
    BX(0,4) = 324
    BX(1,1) = 181
    BX(1,3) = 276
    BX(1,5) = 371
    
    BX(2,0) = 134
    BX(2,2) = 229
    BX(2,4) = 324
    BX(3,1) = 181
    BX(3,3) = 276
    BX(3,5) = 371

    BX(4,0) = 134
    BX(4,2) = 229
    BX(4,4) = 324
    BX(5,1) = 181
    BX(5,3) = 276
    BX(5,5) = 371
    
    BX(6,0) = 134
    BX(6,2) = 229
    BX(6,4) = 324
    BX(7,1) = 181
    BX(7,3) = 276
    BX(7,5) = 371
    
    BX(8,0) = 134
    BX(8,2) = 229
    BX(8,4) = 324
    
    BY(0,0) = 10
    BY(0,2) = 10
    BY(0,4) = 10
    BY(1,1) = 41
    BY(1,3) = 41
    BY(1,5) = 41
    
    BY(2,0) = 72
    BY(2,2) = 72
    BY(2,4) = 72
    BY(3,1) = 104
    BY(3,3) = 104
    BY(3,5) = 104

    BY(4,0) = 135
    BY(4,2) = 135
    BY(4,4) = 135
    BY(5,1) = 166
    BY(5,3) = 166
    BY(5,5) = 166

    
    BY(6,0) = 197
    BY(6,2) = 197
    BY(6,4) = 197
    BY(7,1) = 228
    BY(7,3) = 228
    BY(7,5) = 228
    
    BY(8,0) = 259
    BY(8,2) = 259
    BY(8,4) = 259

    
For x = 0 To 8
    For y = 0 To 5
    BOARD(x,y) = 0 ; fill board with "blank" tiles
    Next
Next
    
;- PROCEDURES
Procedure CheckforLegal(AX,AY, Color)
; This is where we check using the modified array
; note that even though every cell in the Board() array has a 0 in it
; we are reading offset rows

EndProcedure

;- EOP
If InitSprite() = 0 Or InitKeyboard() = 0 Or InitMouse() = 0
    MessageRequester("Error", "DirectX 7+ Fail!", 0)
    End
EndIf

If OpenScreen ( #Wide, #High,32,"Standard") = 0
    MessageRequester("Error", "No screen could be Initialized...", 0)
    End
EndIf

If InitSprite3D() = 0
    MessageRequester("DirectX fail !", "Cannot open DirectX..."+Chr(10)+Chr(10)+"You must have DirectX 7+"+Chr(10)+"To play this game!", #PB_MessageRequester_Ok)
    End
EndIf

LoadSprite(#Cursor, "cursor0.bmp",#PB_Sprite_Memory)
LoadSprite(#Player, "blue.bmp",#PB_Sprite_Memory)
LoadSprite(#CPU, "red.bmp",#PB_Sprite_Memory)
LoadSprite(#Board, "green.bmp",#PB_Sprite_Memory)
LoadSprite(#CursorPoint, "point0.bmp",#PB_Sprite_Memory)

Maus.Point

For y = 0 To 5
    For x = 0 To 8
        AddElement(Tile())
        Tile()\x = BX(x,y)
        Tile()\y = BY(x,y)
        Tile()\ax = xray
        Tile()\ay = yray
        xray =  xray + 1
        If xray > 8
            xray = 0
        EndIf
    Next
    yray = yray  + 1
Next

playercount = 2
cpucount = 2

Repeat
    playercount = 0
    cpucount = 0
    ClearScreen(RGB(0,0,0))
    
    ExamineMouse()
    ExamineKeyboard()
    
    Maus\x = MouseX()
    Maus\y = MouseY()
    
    Start3D()
    
    ; just  a simple read the array and throw down the right tile
    For y = 0 To 5
        For x = 0 To 8
            CHOWDERMONKEY = Board(X,Y)
            Select CHOWDERMONKEY
                Case 0
                    DisplayTransparentSprite(#Board, BX(X,y) ,BY(x,Y))
                Case 1
                    DisplayTransparentSprite(#Player, BX(X,y) ,BY(x,Y))
                    cpucount = cpucount + 1
                Case 2
                    DisplayTransparentSprite(#CPU, BX(X,y) ,BY(x,Y))
                    playercount = playercount + 1
            EndSelect
        Next
    Next
    
    ; just to test the sprites
    If MouseButton(1)
        ForEach(Tile())
        If SpritePixelCollision(#CursorPoint, Maus\x,Maus\y, #Board, Tile()\x, Tile()\y) ; 1011
            ax = Tile()\ax
            ay = Tile()\ay
            color = Board(ax,ay)
            If color = 0
                ;CheckforLegal(AX,AY,2)
                ;If LEGALFLAG = 1
                colr = 1
                board(ax,ay) = colr
                ;EndIf
            EndIf
            Break
        EndIf
        Next
    EndIf

If MouseButton(2)
    ForEach(Tile())
    If SpritePixelCollision(#CursorPoint, Maus\x,Maus\y, #Board, Tile()\x, Tile()\y) ; 1011
        ax = Tile()\ax
        ay = Tile()\ay
        color = Board(ax,ay)
        If color = 0
            ;CheckforLegal(AX,AY,1)
            colr = 2
           ; If LEGALFLAG = 1
                board(ax,ay) = colr
           ; EndIf
            
        EndIf
        Break
    EndIf
    Next
EndIf
A = board(0,0)
B = board(0,1)
C = board(0,2)
D = board(0,3)
E = board(0,4)
F = board(0,5)


StartDrawing( ScreenOutput())
FrontColor(RGB(255,255,255))
BackColor(RGB(0,0,0))
DrawText(10, 65, "AX: "+ Str(ax)) ;+Str(playercount))
DrawText(10, 85, "AY: "+ Str(ay)) ;+Str(cpucount))
DrawText(10, 183, "TILE: "+Str(Tile()\x)+"/"+Str(Tile()\y))

DrawText(10,220,"ROW0: "+Str(Board(0,0))+Str(board(0,1))+Str(board(0,2))+Str(board(0,3))+Str(board(0,4))+Str(board(0,5)))
DrawText(10,235,"ROW1: "+Str(Board(1,0))+Str(board(1,1))+Str(board(1,2))+Str(board(1,3))+Str(board(1,4))+Str(board(1,5)))
DrawText(10,250,"ROW2: "+Str(Board(2,0))+Str(board(2,1))+Str(board(2,2))+Str(board(2,3))+Str(board(2,4))+Str(board(2,5)))
DrawText(10,265,"ROW3: "+Str(Board(3,0))+Str(board(3,1))+Str(board(3,2))+Str(board(3,3))+Str(board(3,4))+Str(board(3,5)))
DrawText(10,280,"ROW4: "+Str(Board(4,0))+Str(board(4,1))+Str(board(4,2))+Str(board(4,3))+Str(board(4,4))+Str(board(4,5)))
DrawText(10,295,"ROW5: "+Str(Board(5,0))+Str(board(5,1))+Str(board(5,2))+Str(board(5,3))+Str(board(5,4))+Str(board(5,5)))
DrawText(10,310,"ROW6: "+Str(Board(6,0))+Str(board(6,1))+Str(board(6,2))+Str(board(6,3))+Str(board(6,4))+Str(board(6,5)))
DrawText(10,325,"ROW7: "+Str(Board(7,0))+Str(board(7,1))+Str(board(7,2))+Str(board(7,3))+Str(board(7,4))+Str(board(7,5)))
DrawText(10,340,"ROW8: "+Str(Board(8,0))+Str(board(8,1))+Str(board(8,2))+Str(board(8,3))+Str(board(8,4))+Str(board(8,5)))

DrawText(10,390,"Mouse: "+Str(Maus\x)+"::"+Str(Maus\y))

StopDrawing()

DisplayTransparentSprite(#Cursor, Maus\x  , Maus\y )
DisplayTransparentSprite(#CursorPoint, Maus\x  , Maus\y )

Stop3D()

FlipBuffers()

If KeyboardPushed(#PB_Key_Escape)
Quit = 1
EndIf

Delay(3)

Until Quit

End
Works with the crud level graphics I supplied... Now for a screenshot of my game in action (so far!)
{{{ POSTED IN NEXT POST BY ME!}}
Last edited by Rook Zimbabwe on Wed Jul 16, 2008 8:28 pm, edited 2 times in total.
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post by Rook Zimbabwe »

OK it is improved!

Image

8)

Now If only I could figure out where that tile at 0,0 came from!
Last edited by Rook Zimbabwe on Wed Jul 16, 2008 8:37 pm, edited 3 times in total.
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post by Rook Zimbabwe »

How the array is supposed to work:

Our array is named Board()

Currently (in this example) it has a size of 6,6 and this array holds the color of the tile on the board: 0 = empty, 1 = Player and 2 = CPU / 99 = ignore

When we put a piece down we need to check in the 6 radians around the piece for the opposite colored piece (thus making a legal move (for now!))

Our search grid lookss like this:
Image

The problem comes in that it won't search accurately.

The piece is placed at 2,3... occasionally it won't look at the NW or SW corners...

In the second example of the code you can right click to make a ring of opposite colored stones and then left click to put the players colored stone in the center. Most times you will see all colored flags showing 1. This is correct.

But sometimes it misses a corner... or worse the N or S check which should be the easiest!

8)
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post by Rook Zimbabwe »

Well this is very frustrating.

It checks well in the center of the board... it seems to be the edges where it messes up.

I know my edge trim code is messed up!

The only solution I can see thus far is to add an extra row at top and bottom and on both sides and fill those with the 99 (no click here) units.

Ay Yah!!! :evil:
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
citystate
Enthusiast
Enthusiast
Posts: 638
Joined: Sun Feb 12, 2006 10:06 pm

Post by citystate »

you could just check the coordinates before applying them - if x or y is less than 0 or greater than 5, ignore the click - only action it when the values are within the range...

looks like the relative coordinates are
  • <-1,0> - up
    <0,-1> - up-left
    <0,+1> - up-right
    <+1,-1> - down-left
    <+1,+1> - down-right
    <+1,0> -down
incidently, your grid appears to follow (y,x) labeling rules - don't know if that's being reflected in your code though
there is no sig, only zuul (and the following disclaimer)

WARNING: may be talking out of his hat
User avatar
Demivec
Addict
Addict
Posts: 4283
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Post by Demivec »

Rook Zimbabwe wrote:When we put a piece down we need to check in the 6 radians around the piece for the opposite colored piece (thus making a legal move (for now!))
Your picture confuses me a little, are you simply checking the 6 radial hexes adjacent to the hex or are you checking 6 lines originating at the hex and extending to the edge of your map?
y3an
User
User
Posts: 56
Joined: Sun Mar 09, 2008 6:06 am

Post by y3an »

Hi,

I have not seen in your code a test for .. heuu .. to know if X line is pair or not.

On this graph, with Player on 2,3.. NO = (-0 / -1 ) works
(2-0, 3-1) = 2,2

Image

Now , put the player on the 2,4 Tile.

(2-0, 4-1) = .. ( found the solution and don't win a price !!! [I'm not Jesus, sorry] )
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post by Rook Zimbabwe »

At the moment I am simply checking the 6 hex around the target to see if an opposite colored tile is in at least 1 of those adjacent hexes... AND to see how many.

EDIT:: Crud!!!! I hadn't caught on to the fact Yes Y3an, you are right... you win the prize!

I may have to go to alternate array indexing where I simple skip every other cell in the array..

OK I have done this... it should simplify checking in the 6 radians!

Image

:D
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
Post Reply