Results 1 to 30 of 30

Thread: [RESOLVED] Convert row, col to single square number

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Resolved [RESOLVED] Convert row, col to single square number

    Code:
    How do I convert the following array Row/Col
    
    1,8  2,8  3,8  4,8  5,8  6,8  7,8  8,8
    1,7  2,7  3,7  4,7  5,7  6,7  7,7  8,7
    1,6  2,6  3,6  4,6  5,6  6,6  7,6  8,6
    
    etc
    
    1,1  2,1  3,1  4,1  5,1  6,1  7,1  8,1
    
    to this
    
    00 01 02 03 04 05 06 07
    08 09 10 11 12 13 14 15
    16 17 18 19 20 21 22 23
    
    etc
    
    56 57 58 59 60 61 62 63


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  2. #2
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: Convert row, col to single square number

    the general formula for converting a 2D array to a 1D array is:

    i = row * ColCount + col
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

  3. #3
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: Convert row, col to single square number

    For this specific case, you'll need something a little different:

    i = 8 * (8 - Row) + Col - 1

    Or, if you prefer:

    i = 8 * (8 - Rank) + File - 1

  4. #4
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: Convert row, col to single square number

    How are you keeping these functions and variables? Are you converting every square every time using a function, or are you using the functions to generate some kind of array/collection/UDT at form_load?

    Like for example you need to know 1,8 is 00

    would you simply get it from your array/collection/UDT
    OR
    would you use a function MyFunction("1,8") (return: "00")

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Convert row, col to single square number

    I working with two different arrays one with squares numbered 0 to 63 and the other by col/row. I need to convert the col/row to an absolute square number to pass to a function that expects a number from 0 to 63 from a function that requires col/row format. I'm using the conversion code from Logophobic's post to do this.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  6. #6
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Convert row, col to single square number

    How are the row and column numbers stored?
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  7. #7
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: Convert row, col to single square number

    I would just bite the bullet and create an array from the start and then have a couple helper functions to resolve the squares. Maybe something like this.

    In module
    Code:
    Option Explicit
    Dim Squares(8, 8) As Integer
    Dim Coords(63) As String
    
    Public Type Square
        Row As Integer
        Column As Integer
    End Type
    
    
    Public Sub InitializeArray()
    Dim r As Integer
    Dim c As Integer
    Dim x As Integer
    
        For c = 8 To 1 Step -1
            For r = 1 To 8
                Squares(r, c) = x
                Coords(x) = r & "," & c
                x = x + 1
            Next r
        Next c
    End Sub
    
    Public Function GetSquareFromRowAndCol(ByVal Row As Integer, ByVal col As Integer)
        GetSquareFromRowAndCol = Squares(Row, col)
    End Function
    
    Public Function GetRowAndColFromSquare(ByVal Position As Integer) As Square
    Dim sq As Square
    Dim pos() As String
    
        pos = Split(Coords(Position), ",")
        
        sq.Row = pos(0)
        sq.Column = pos(1)
        
        GetRowAndColFromSquare = sq
    End Function
    And then to use in your form
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
    Dim sq As Square
    Dim index As Integer
    
        index = GetSquareFromRowAndCol(2, 1)
        sq = GetRowAndColFromSquare(57)
        
        Debug.Print "Square = " & index
        Debug.Print "Coordinates = " & sq.Row & "," & sq.Column
    End Sub
    
    Private Sub Form_Load()
        InitializeArray
    End Sub
    Last edited by MarkT; Sep 10th, 2014 at 07:23 AM. Reason: updated module code

  8. #8
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: Convert row, col to single square number

    Gaah!! Why would you suggest anything with strings?

    If you're going to store pre-calculated values in arrays, at least stick with numeric types.
    I wouldn't bother wrapping them in a function, either. The overhead of a function call itself is probably more expensive than the simple, direct in-line expression. Kind of defeats the purpose of having the arrays.

    Code:
    Public IndexFromRowAndCol(1 To 8, 1 To 8) As Long
    Public ColFromIndex(63) As Long
    Public RowFromIndex(63) As Long

  9. #9
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: Convert row, col to single square number

    Quote Originally Posted by Logophobic View Post
    Gaah!! Why would you suggest anything with strings?
    Probably because it was done before the first cup of coffee. Anyways I think I would still use either a funciton or a public property get statement to retrieve the values. It is not going to be an expensive call and would ensure the variables would never be overwritten.

    Code:
    Option Explicit
    Dim Squares(1 To 8, 1 To 8) As Integer
    Dim Coords(63) As Square
    
    Public Type Square
        row As Integer
        Column As Integer
    End Type
    
    
    Public Sub InitializeArray()
    Dim r As Integer
    Dim c As Integer
    Dim x As Integer
    
        For c = 8 To 1 Step -1
            For r = 1 To 8
                Squares(r, c) = x
                Coords(x).row = r
                Coords(x).Column = c
                x = x + 1
            Next r
        Next c
    End Sub
    
    Public Function GetSquareFromRowAndCol(ByVal row As Integer, ByVal col As Integer)
        GetSquareFromRowAndCol = Squares(row, col)
    End Function
    
    Public Function GetRowAndColFromSquare(ByVal Position As Integer) As Square
        GetRowAndColFromSquare = Coords(Position)
    End Function

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Convert row, col to single square number

    Quote Originally Posted by Nightwalker83 View Post
    How are the row and column numbers stored?
    What do you mean, NW. Doesn't my 1st post show you that?


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  11. #11

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Convert row, col to single square number

    Why would I want other arrays, function calling, UDT's etc when the expression from Logophobic using a simple single line statement does exactly what I need; a simple conversion from x,y to single square number?


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  12. #12
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: Convert row, col to single square number

    Quote Originally Posted by MarkT View Post
    Code:
    Option Explicit
    Dim Squares(1 To 8, 1 To 8) As Integer
    Dim Coords(63) As Square
    
    Public Type Square
        row As Integer
        Column As Integer
    End Type
    
    
    Public Sub InitializeArray()
    Dim r As Integer
    Dim c As Integer
    Dim x As Integer
    
        For c = 8 To 1 Step -1
            For r = 1 To 8
                Squares(r, c) = x
                Coords(x).row = r
                Coords(x).Column = c
                x = x + 1
            Next r
        Next c
    End Sub
    
    Public Function GetSquareFromRowAndCol(ByVal row As Integer, ByVal col As Integer)
        GetSquareFromRowAndCol = Squares(row, col)
    End Function
    
    Public Function GetRowAndColFromSquare(ByVal Position As Integer) As Square
        GetRowAndColFromSquare = Coords(Position)
    End Function
    I would use this code before using any kind of "simple" expression function.
    You access the pre-calculated variable in memory there is no calculation for every time you need to access a simple pre-calculated in memory variable. Maybe for what jms is doing its fine, but I would still choose to go the longer path and simply use the memory to get variables faster.
    Also it allows you to add more properties (if needed) for your squares.

  13. #13
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: Convert row, col to single square number

    Did I miss the code where an index was being returned without an additional prepopulated array. As far as the UDT, I thought that would be a good way to return the coordinates of a square based on the squares index in a single call. If I understand Logophobic's code, you would need to make 2 calls to get the coordinates. One to get the row index value and then one for the column index value. The reason I choose to use a function to return these values is I believe it is a best practice to have variable scope as narrow as possible. In general, I try to avoid public variables as much as possible.

  14. #14

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Convert row, col to single square number

    It's not something that's going to occur on every move in the game. It's used to check when Black is in a position to castle I need to check if Black is in check, going to pass through a check, or wind up in check. In stead of checking this on every move of Black I only test when Black's King and Rook are in the correct position to castle which is just a now and then situation. Once Black does castle then I do not need to test again. This is why the single line method is just right for me.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  15. #15
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Convert row, col to single square number

    Quote Originally Posted by jmsrickland View Post
    What do you mean, NW. Doesn't my 1st post show you that?
    Is each row/column value being stored in a variable, such as row: 1, column: 1?
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  16. #16

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Convert row, col to single square number

    Yes. Each row/col represents a square on the board and it stores the piece value. I have two modules, one written using an array like this one and the other module uses an array that is numbered 0 to 63. There is a function in one module that needs to call another function in the other module so I have to convert the row/col to a single digit square number but I do not need to convert back. The conversion only takes place under certain circumstances so it is not executed at every move. Actually only when the black king has a possible castle I have to check to see if he can so I call a function that scans the board (the single number cell array) for all possibilities to keep in the rules of the game. The function that does the call is in the module that uses the row/col array and the function that scans the board uses the 0-63 board pattern.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  17. #17
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Convert row, col to single square number

    Quote Originally Posted by jmsrickland View Post
    Yes. Each row/col represents a square on the board and it stores the piece value.
    What I am asking is do you store that number of the squared in a variable? If so and the number is separated by the "," such as you have posted in post #1 you do something such as

    Code:
    Private Sub Command1_Click()
     MsgBox (convertTosquare(2, 1))
    End Sub
    
    Private Function convertTosquare(row As Integer, col As Integer)
     Dim i As Integer
    i = 8 * (8 - col) + row - 1
      convertTosquare = i
    End Function
    Edit:

    I fixed the code to use MarkT 's formula, see post #21 below.
    Last edited by Nightwalker83; Sep 12th, 2014 at 07:25 AM. Reason: Fixed code!
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  18. #18
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: Convert row, col to single square number

    NW, there' not much to it: he has two arrays, a two-dimensional array (1 to 8, 1 to 8) and a one-dimensional array (0 to 63). Both are representations of a chessboard, and he simply needs to get an index to the 1D array given index values for the 2D array.

  19. #19
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Convert row, col to single square number

    Quote Originally Posted by Logophobic View Post
    NW, there' not much to it: he has two arrays, a two-dimensional array (1 to 8, 1 to 8) and a one-dimensional array (0 to 63). Both are representations of a chessboard, and he simply needs to get an index to the 1D array given index values for the 2D array.
    Yeah I realise that hence my attempt to solve the problem in post #17.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  20. #20
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: Convert row, col to single square number

    Quote Originally Posted by jmsrickland View Post
    I'm using the conversion code from Logophobic's post...
    I gave the solution in post #3, and I believe this thread should be resolved.

  21. #21
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: Convert row, col to single square number

    Quote Originally Posted by Logophobic View Post
    I gave the solution in post #3, and I believe this thread should be resolved.
    Maybe but I doesn't appear that the formula you came up with generates the proper indexes based on the example JM posted in his first post. For example 2,1 in the first post equals 57. By your formula, it calculates to 48.

    I think the proper formula would be
    i = 8 * (8 - Col) + Row - 1
    Last edited by MarkT; Sep 12th, 2014 at 07:04 AM.

  22. #22
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Convert row, col to single square number

    Quote Originally Posted by MarkT View Post
    Maybe but I doesn't appear that the formula you came up with generates the proper indexes based on the example JM posted in his first post. For example 2,1 in the first post equals 57. By your formula, it calculates to 48.

    I think the proper formula would be
    i = 8 * (8 - Col) + Row - 1
    I changed my code in post #17 to use your formula and it works!

    Edit:

    I just tested and for 1, 1 it returns 56?
    Last edited by Nightwalker83; Sep 12th, 2014 at 07:33 AM. Reason: Adding more!
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  23. #23
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: Convert row, col to single square number

    That is correct. (1, 1) is bottom left in the 2D array, and (56) is the corresponding element of the 1D array.
    Code:
    1,8 . . . 8,8
     .         .
     .         .
     .         .
    1,1 . . . 8,1
    
     00 . . . 07
     .         .
     .         .
     .         .
     56 . . . 63

  24. #24
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: Convert row, col to single square number

    Quote Originally Posted by MarkT View Post
    Maybe but I doesn't appear that the formula you came up with generates the proper indexes based on the example JM posted in his first post. For example 2,1 in the first post equals 57. By your formula, it calculates to 48.

    I think the proper formula would be
    i = 8 * (8 - Col) + Row - 1
    You are swapping rows with columns: (2, 1) in the first post is column 2, row 1 which my formula correctly translates to 57.

  25. #25
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: Convert row, col to single square number

    Quote Originally Posted by Logophobic View Post
    You are swapping rows with columns: (2, 1) in the first post is column 2, row 1 which my formula correctly translates to 57.
    According to JMs post, the sequence is row,column not column,row. If the coordinates are in fact being stated as row,column then adjusted formula is correct otherwise yours is.
    Quote Originally Posted by jmsrickland View Post
    Code:
    How do I convert the following array Row/Col
    1,8  2,8  3,8  4,8  5,8  6,8  7,8  8,8
    1,7  2,7  3,7  4,7  5,7  6,7  7,7  8,7
    1,6  2,6  3,6  4,6  5,6  6,6  7,6  8,6
    
    etc
    
    1,1  2,1  3,1  4,1  5,1  6,1  7,1  8,1
    
    to this
    
    00 01 02 03 04 05 06 07
    08 09 10 11 12 13 14 15
    16 17 18 19 20 21 22 23
    
    etc
    
    56 57 58 59 60 61 62 63

  26. #26
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: Convert row, col to single square number

    Quote Originally Posted by Nightwalker83 View Post
    I changed my code in post #17 to use your formula and it works!

    Edit:

    I just tested and for 1, 1 it returns 56?
    1,1 is a bad example to test with. It will return the same number if you are using either
    i = 8 * (8 - Row) + Col - 1 translates to i = 8 * (8 - 1) + 1 - 1
    or
    i = 8 * (8 - Col) + Row - 1 translates to i = 8 * (8 - 1) + 1 - 1
    Last edited by MarkT; Sep 12th, 2014 at 08:43 AM.

  27. #27
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: Convert row, col to single square number

    Quote Originally Posted by MarkT View Post
    According to JMs post, the sequence is row,column not column,row. If the coordinates are in fact being stated as row,column then adjusted formula is correct otherwise yours is.
    Yes, he did say Row/Col, but the numbers clearly show (Col, Row) indexing.

    The top row (i.e. row 8) is 1,8 2,8 3,8 ...

  28. #28

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: [RESOLVED] Convert row, col to single square number

    Logophobic is correct.

    I did state in the title row,col but that was just out of habit of saying row first then col and the illustration in my 1st post is col,row which is correct. Post 23 is correct and post 3 is correct which is what I am using

    Because I didn't plan ahead with this game I wrote the board logic way back when and it used an array of Image Controls index 0 to 63 so everything in the code was written based on that array order. Then later (now) I decided to write the engine to play the user and I used an array that matches how the board is actually laid out which is the 2D array and didn't realize that once I got into it I was going to have to call some of the functions in the original code so that is why I have to convert 2D to 1D

    Sorry about the misleading title


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  29. #29
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Convert row, col to single square number

    Quote Originally Posted by Logophobic View Post
    That is correct. (1, 1) is bottom left in the 2D array, and (56) is the corresponding element of the 1D array.
    jmsrickland could have put that in the first post.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  30. #30

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Convert row, col to single square number

    Quote Originally Posted by Nightwalker83 View Post
    jmsrickland could have put that in the first post.
    I did. Here is what I posted:

    1,8 2,8 3,8 4,8 5,8 6,8 7,8 8,8
    1,7 2,7 3,7 4,7 5,7 6,7 7,7 8,7
    1,6 2,6 3,6 4,6 5,6 6,6 7,6 8,6

    etc

    1,1 2,1 3,1 4,1 5,1 6,1 7,1 8,1
    ^
    Lower left corner of 2D Array

    to this

    00 01 02 03 04 05 06 07
    08 09 10 11 12 13 14 15
    16 17 18 19 20 21 22 23

    etc

    56 57 58 59 60 61 62 63
    ^
    Lower left corner of 1D Array
    Last edited by jmsrickland; Sep 12th, 2014 at 09:41 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width