Forum Moderators: coopster

Message Too Old, No Replies

Sub 2D array

need 3x3 from 5x5 2D array

         

mcibor

10:38 pm on Sep 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all!

I have a very unusual problem - I want to create a visualization of the map. The map is stored as a string variable:

$map = "0 0 0 0 0
0 1 1 0 0
0 0 1 2 0
0 3 2 1 0
0 0 0 0 0";

this kind of storing is only to improve creating of new maps, I don't know if it's the best option (map is only numerical, separated by " " and "\n".
Then I usually explode it into an array:
$arrmap = explode(" ", explode("\n", $map));//didn't work, so I created a function:

function readarr($string2d) {
$temp = explode("\n", $string2d);
foreach($temp as $string) {
$temp2[] = explode(" ", $string); }
return $temp2; }

So I get $arrmap[1][2] = 0 (or 1, didn't test it yet)
However I need only a 3x3 piece of map with midle in X, Y
How to get it?
I can still change the $map, as I'm in the process of creation, not modification.

I can do it manually => new_array = ($arr[$x - 1][$y - 1], $arr[$x - 1][$y]...)
but maybe there's a better way to do that
Hope to hear from you

Best regards
Michal Cibor

jatar_k

1:26 am on Sep 7, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



when you say 3x3, which values do you need? first 3 in every row?

mcibor

8:53 am on Sep 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



2D Jatar :) So 3x3 means eg:

0 1 1
0 0 1
0 3 2

or
1 2 0
2 1 0
0 0 0

In the first example the middle point is x=1 y=2. And in the second x=3 y=3.

Down is Y, across is X, both starting at the top left corner.
I need all points surrounding the specified point XY - therefore 3x3

To explain better I will give an example (but it's very manual, and I wish to find a better solution)

function read3x3($string2d, $x, $y) {
$temp = explode("\n", $string2d);
foreach($temp as $string) {
$temp2[] = explode(" ", $string); }

$arr3x3 = array($temp2[$x - 1][$y - 1], $temp2[$x][$y - 1], $temp2[$x + 1][$y - 1],
$temp2[$x - 1][$y], $temp2[$x][$y], $temp2[$x + 1][$y],
$temp2[$x - 1][$y + 1], $temp2[$x][$y + 1], $temp2[$x + 1][$y + 1] );
return $arr3x3;}

x,y must be bigger than 0 and smaller than array size.

I'm thinking now about putting the map into the database...
CREATE TABLE `map` (
id INT(11) NOT NULL auto_increment,
x INT(11) NOT NULL
y INT(11) NOT NULL
image VARCHAR(20),
description TEXT);

But the creation of such map is quite horrible, however it's quite convenient because I can strore event and description...

Hard to decide what's the best solution.
Thanks Jatar for interest

Michal

jatar_k

5:17 pm on Sep 7, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



ah ok, I didn't understand the relative positioning of the centre point, I figured it wasn't a simple question and couldn't quite get what you meant.

are those the only two options you are going to use or are you planning on processing for all 9 possible centre points?

your way is fine but I am wondering about passing x and y to the function, how does the other script find out x and y, user selected?

if not, given there 9 center points you could pass 1 to 9 to the script and have predetermined array structures to return.

mcibor

9:58 pm on Sep 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I wish to process map (the input array) up to 50x50 or more. It will be a kind of a labirynth.
So relative positioning is essential.

I think I will finish up with storing the data in db -

$sql = "SELECT * FROM table WHERE x IN ('". $x - 1 .",". $x .",". $x + 1 .")
AND y IN ('". $y - 1 .",". $y .",". $y + 1 .") LIMIT 9;

I'm going to pass the actual position in SESSION.

If you have any other ideas don't hesitate to write!

Michal Cibor

PS. I think this will be the first visualisation of an www game :) Sth like first Might and Magic :D
Cya

mcibor

11:09 am on Sep 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Jatar wrote:

but I am wondering about passing x and y to the function, how does the other script find out x and y, user selected?

The current value I'm planning to store in SESSION. The user specifies only the movement: North, South, West or East. Depending on what is chosen
$x = $_SESSION['x']; if($move == "East") $x = $x + 1; $_SESSION['x'] = $x; //...etc.

I'm thinking more and more on the db solution - it's more flexible.
Feel free to comment.
Best regards

Michal Cibor

jatar_k

5:23 pm on Sep 8, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I think the db route is the best thought, just because reading it will be much faster and you could store a huge map and could straight select your values as opposed to chunking up files all the time. You can also offload part of the processing to mysql and not have php doing it all.

I don't see anything wrong with your reasoning

there is some thought itching at the back of my mind though, I hate that, not sure if it will come out or not