-
Sep 9th, 2014, 03:28 PM
#1
[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.
-
Sep 9th, 2014, 03:51 PM
#2
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
-
Sep 9th, 2014, 06:59 PM
#3
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
-
Sep 9th, 2014, 11:15 PM
#4
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")
-
Sep 10th, 2014, 01:57 AM
#5
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.
-
Sep 10th, 2014, 02:48 AM
#6
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
-
Sep 10th, 2014, 07:14 AM
#7
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
-
Sep 10th, 2014, 10:01 AM
#8
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
-
Sep 10th, 2014, 10:30 AM
#9
Re: Convert row, col to single square number
Originally Posted by Logophobic
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
-
Sep 10th, 2014, 11:09 AM
#10
Re: Convert row, col to single square number
Originally Posted by Nightwalker83
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.
-
Sep 10th, 2014, 11:14 AM
#11
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.
-
Sep 10th, 2014, 02:09 PM
#12
Re: Convert row, col to single square number
Originally Posted by MarkT
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.
-
Sep 10th, 2014, 02:14 PM
#13
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.
-
Sep 10th, 2014, 04:54 PM
#14
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.
-
Sep 11th, 2014, 09:59 PM
#15
Re: Convert row, col to single square number
Originally Posted by jmsrickland
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
-
Sep 12th, 2014, 01:13 AM
#16
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.
-
Sep 12th, 2014, 02:49 AM
#17
Re: Convert row, col to single square number
Originally Posted by jmsrickland
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
-
Sep 12th, 2014, 03:53 AM
#18
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.
-
Sep 12th, 2014, 05:47 AM
#19
Re: Convert row, col to single square number
Originally Posted by Logophobic
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
-
Sep 12th, 2014, 06:02 AM
#20
Re: Convert row, col to single square number
Originally Posted by jmsrickland
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.
-
Sep 12th, 2014, 06:57 AM
#21
Re: Convert row, col to single square number
Originally Posted by Logophobic
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.
-
Sep 12th, 2014, 07:26 AM
#22
Re: Convert row, col to single square number
Originally Posted by MarkT
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
-
Sep 12th, 2014, 08:12 AM
#23
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
-
Sep 12th, 2014, 08:20 AM
#24
Re: Convert row, col to single square number
Originally Posted by MarkT
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.
-
Sep 12th, 2014, 08:34 AM
#25
Re: Convert row, col to single square number
Originally Posted by Logophobic
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.
Originally Posted by jmsrickland
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
-
Sep 12th, 2014, 08:37 AM
#26
Re: Convert row, col to single square number
Originally Posted by Nightwalker83
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.
-
Sep 12th, 2014, 09:04 AM
#27
Re: Convert row, col to single square number
Originally Posted by MarkT
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 ...
-
Sep 12th, 2014, 12:42 PM
#28
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.
-
Sep 12th, 2014, 06:37 PM
#29
Re: Convert row, col to single square number
Originally Posted by Logophobic
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
-
Sep 12th, 2014, 09:37 PM
#30
Re: Convert row, col to single square number
Originally Posted by Nightwalker83
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|