Forum Moderators: coopster
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
0 1 1
0 0 1
0 3 2
1 2 0
2 1 0
0 0 0
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
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.
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
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
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