Page 1 of 1

nested arrays in material.inc.php

Posted: 18 May 2023, 20:31
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?

Re: nested arrays in material.inc.php

Posted: 18 May 2023, 22:06
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

Re: nested arrays in material.inc.php

Posted: 19 May 2023, 01:12
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!

Re: nested arrays in material.inc.php

Posted: 19 May 2023, 02:16
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!