nested arrays in material.inc.php

Game development with Board Game Arena Studio
Post Reply
Ditto11
Posts: 121
Joined: 31 March 2023, 19:14

nested arrays in material.inc.php

Post by Ditto11 »

Hoping to find a way to do this to help streamline the code and make it easy to maintain/read:

if, in the material.inc.php, I have this (this is from Chess game, defining how a piece moves):

Code: Select all

$this->gameMaterial = array(
	1 => array(
		"moveMatrixByPlayerAndSymbol" => array(
			1 => array(
					"rook" => array(
								array("x" => 0, "y" => 1 ),	array("x" => 0, "y" => -1 ),	array("x" => 1, "y" => 0 ),	array("x" => -1, "y" => 0 ),
								array("x" => 0, "y" => 2 ),	array("x" => 0, "y" => -2 ),	array("x" => 2, "y" => 0 ),	array("x" => -2, "y" => 0 ),
								array("x" => 0, "y" => 3 ),	array("x" => 0, "y" => -3 ),	array("x" => 3, "y" => 0 ),	array("x" => -3, "y" => 0 ),
								array("x" => 0, "y" => 4 ),	array("x" => 0, "y" => -4 ),	array("x" => 4, "y" => 0 ),	array("x" => -4, "y" => 0 ),
								array("x" => 0, "y" => 5 ),	array("x" => 0, "y" => -5 ),	array("x" => 5, "y" => 0 ),	array("x" => -5, "y" => 0 ),
								array("x" => 0, "y" => 6 ),	array("x" => 0, "y" => -6 ),	array("x" => 6, "y" => 0 ),	array("x" => -6, "y" => 0 ),
								array("x" => 0, "y" => 7 ),	array("x" => 0, "y" => -7 ),	array("x" => 7, "y" => 0 ),	array("x" => -7, "y" => 0 ),								
							 ),
					"knight" => array(
					... etc
Is there a way of building it by defining the types of moves first, such as:

Code: Select all

$this->pcOrthogonal = array ( 
								array("x" => 0, "y" => 1 ),	array("x" => 0, "y" => -1 ),	array("x" => 1, "y" => 0 ),	array("x" => -1, "y" => 0 ),
								array("x" => 0, "y" => 2 ),	array("x" => 0, "y" => -2 ),	array("x" => 2, "y" => 0 ),	array("x" => -2, "y" => 0 ),
								array("x" => 0, "y" => 3 ),	array("x" => 0, "y" => -3 ),	array("x" => 3, "y" => 0 ),	array("x" => -3, "y" => 0 ),
								array("x" => 0, "y" => 4 ),	array("x" => 0, "y" => -4 ),	array("x" => 4, "y" => 0 ),	array("x" => -4, "y" => 0 ),
								array("x" => 0, "y" => 5 ),	array("x" => 0, "y" => -5 ),	array("x" => 5, "y" => 0 ),	array("x" => -5, "y" => 0 ),
								array("x" => 0, "y" => 6 ),	array("x" => 0, "y" => -6 ),	array("x" => 6, "y" => 0 ),	array("x" => -6, "y" => 0 ),
								array("x" => 0, "y" => 7 ),	array("x" => 0, "y" => -7 ),	array("x" => 7, "y" => 0 ),	array("x" => -7, "y" => 0 ),								
				);
$this->pcDiagonal = array ( .... build it similar );

$this->gameMaterial = array(
	1 => array(
		"moveMatrixByPlayerAndSymbol" => array(
			1 => array(
					"rook" => array(
								$this->pcOrthogonal
							 ),
					"queen" => array(
								$this->pcOrthogonal,
								$this->pcDiagonal,
								),
					...
?? I tried exactly that, but it of course fails .. so wondering if this is even possible, and if so, what's the syntax for it?
User avatar
robinzig
Posts: 461
Joined: 11 February 2021, 18:23

Re: nested arrays in material.inc.php

Post by robinzig »

Code: Select all

"rook" => array($this->pcOrthogonal)
should be just

Code: Select all

"rook" => $this->pcOrthogonal
in order to be equivalent to the first sample
Ditto11
Posts: 121
Joined: 31 March 2023, 19:14

Re: nested arrays in material.inc.php

Post by Ditto11 »

robinzig wrote: 18 May 2023, 22:06

Code: Select all

"rook" => array($this->pcOrthogonal)
should be just

Code: Select all

"rook" => $this->pcOrthogonal
in order to be equivalent to the first sample
Oh I see .. so bury it inside like:

Code: Select all

1 => array(
					"rook" => $this->pcOrthogonal
			),
							 
?? (I had an extra level :) got it)

I'll try that when I get a chance . .thanks!
Ditto11
Posts: 121
Joined: 31 March 2023, 19:14

Re: nested arrays in material.inc.php

Post by Ditto11 »

sweet, thank you very much, works great!

to build the queen, I used:

Code: Select all

"queen" => array_merge ( $this->pcOrthogonal , $this->pcDiagonal ),
which seems to work perfectly ;) thank you kindly for the help!
Post Reply

Return to “Developers”