Arrays for board coordinates (fields)

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Scr1pter
Posts: 1272
Joined: 06 Aug 2017, 08:21
Location: Germany

Arrays for board coordinates (fields)

06 Dec 2018, 16:24

Hey guys,

Let's say I would like to emulate a board game like Chess, Queen, Kamisado etc.
Those board games have 64 fields (8x8).

My first idea was to create an Associative Array which contains the middle points of all fields.
E.g. Middle of A1 would be x50, y50 / B1's middle would be x150, y50 etc.

A while ago I wanted to create an Associative Array which contained a lot of elements, and it did not work.
(It seems I reached some limit).
I think for these 64 elements (which have both an X and a Y value), it won't work either.

For this reason I have two other ideas:
1)
Creating 64 associative arrays, named A1, B1, C1 etc. with x and y values.

Code: Select all

A1 := {x:50, y:50}
B1 := {x:150, y:50}
C1 := {x:250, y:50}
etc.
Probably a bit too much of a good thing...
(We're speaking about 64 arrays. :crazy: )

2)
Creating only 2 arrays which contain each 8 values.
The first array is for the X values and the second one for the Y's.
After linking the elements, I should theoretically retrieve the exact coordinates of a specific field.
Element 5 of Array 1 (x) and element 3 of Array 2 (Y) would be: E3 -> 450x250

Code: Select all

xArray := {"A": 50, "B": 150, "C": 250, "D": 350, "E": 450 etc.}
yArray := {"1": 50, "2": 150, "3": 250, "4": 350, "5": 450 etc.}
What would you suggest to do?

Best regards
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
oif2003
Posts: 214
Joined: 17 Oct 2018, 11:43
Contact:

Re: Arrays for board coordinates (fields)

06 Dec 2018, 16:28

HI,
I'm not sure if I understand what you are asking for here, but can this be achieved with a 2 dimensional array?
User avatar
sinkfaze
Posts: 616
Joined: 01 Oct 2013, 08:01

Re: Arrays for board coordinates (fields)

06 Dec 2018, 16:58

If you're trying to assign evenly spaced coordinates for each tile on a chess board, for example, you could probably just skip the array and do the math:

Code: Select all

tile=C3
StringLower, tile, tile
MsgBox %	"x" ((Asc(SubStr(tile,1,1))-96) * 50) " y" (SubStr(tile,0) * 50)
User avatar
Scr1pter
Posts: 1272
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: Arrays for board coordinates (fields)

06 Dec 2018, 17:33

Hi guys,

Let me try to explain it a bit better.
When I want to place objects on a board game (e.g. creating levels, puzzles etc. ),
it's easier for me to define a text file which contains information like:
white_king - A1
black_king - E5

instead of:
white_king - x50, y50
black_king - x550, y450

In my case it's not about Chess, but since everybody knows it, I wanted to use it as example.
That principle can be used for any board game which has fields/tiles.

@oif2003:
2 dimensional array sounds indeed interesting.
But unfortunately I can't really figure out anything here:
https://autohotkey.com/docs/Objects.htm ... _of_Arrays

@sinkfaze:
Wouldn't that mean I would have to do this 64 times?
I mean it's still better than working with x500, y450,
but I thought Arrays could help me there.

Regards
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
oif2003
Posts: 214
Joined: 17 Oct 2018, 11:43
Contact:

Re: Arrays for board coordinates (fields)

06 Dec 2018, 17:59

If you use a 2D array, you effectively have a cartesian plane, similar to what you encountered in basic algebra, the (x, y) type stuff. You can then define points, such as Board[1][2] and assign it a value such as "white_king". As for reading in data, converting between "A1" and (1, 1) should be pretty easy.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Arrays for board coordinates (fields)

06 Dec 2018, 18:16

Code: Select all

Board := 
(LTrim Join
	{
		"A": 
		[
			{"x": 0, "y": 0}, 
			{"x": 10, "y": 0}, 
			{"x": 20, "y": 0}
		], 
		"B": 
		[
			{"x": 0, "y": 10}, 
			{"x": 10, "y": 10}, 
			{"x": 20, "y": 10}
		]
	}
)

MsgBox % Board.A[2].x
MsgBox % Board.B[3].y
User avatar
sinkfaze
Posts: 616
Joined: 01 Oct 2013, 08:01

Re: Arrays for board coordinates (fields)

07 Dec 2018, 09:20

Scr1pter wrote:
06 Dec 2018, 17:33
@sinkfaze:
Wouldn't that mean I would have to do this 64 times?
I mean it's still better than working with x500, y450,
but I thought Arrays could help me there.
Well my point was that you may not need an object at all if you're just using the array as a substitute for the math. I mean, you could pop the math into a function which will return an object if you want:


Code: Select all

white_king :=	"A1"
black_king :=	"E5"

MsgBox %	"x" Tile(white_king).x " y" Tile(white_king).y
MsgBox %	"x" Tile(black_king).x " y" Tile(black_king).y

; t :=	Tile(white_king)
; MsgBox %	"x" t.x " y" t.y

Tile(an) {
	StringLower, an, an
	return	{x:(Asc(SubStr(an,1,1))-96) * 50,y:SubStr(an,0) * 50}
}
If you really feel like you want to push it all to an object though, I would load it through two loops:

Code: Select all

axis=abcdefgh
tiles :=	{}
Loop, Parse, axis
{
	a :=	A_LoopField
	Loop, 8
	{
		n :=	A_Index
		tiles[a n] :=	{x:(Asc(a)-96) * 50,y:n * 50}
	}
}

MsgBox %	"x" tiles["A1"].x " y" tiles["A1"].y
MsgBox %	"x" tiles["E5"].x " y" tiles["E5"].y

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, Lamron750, mikeyww and 223 guests