Jump to content

Archived

This topic is now archived and is closed to further replies.

lore_lanu

[SOLVED] PHP maze type thing...

Recommended Posts

Hello,

 

So I've been wondering how to make a PHP maze as of late. But it's not one of those ones where you print out a maze on a piece of paper and try to draw a path from one end to another. (which is what I've been getting from my Google searches)

 

What I'm talking about is a sort of maze where the user is presented with, say 5, door  images. When they click one, it goes to another set of door images and they have to kepp clicking on doors until they find the exit.

 

Please keep these features in mind: when the user closes the window, I want them to stay in the same part of the maze when they come back (they will have to be logged into their account), and there will be dead ends,where the user is redirected to the beginning.

 

Could anyone tell me how I could go about making this? Or is this not possible using PHP?

 

Thanks in advance!

Share this post


Link to post
Share on other sites

Its possible, as I wrote a basic one some years back,

 

The problem I had was I wanted to start at a random pace and get to a random place, I also wanted the maze to be different each time and I wanted a map.

Mine had only 4 doors, North, South, East and West,

it was integrated into another system but I could possible find the maze generator (which I modified from one I found one the web, can't remember where)

 

Knowing where the use is positioned was done via sessions, if they logged out and in, they had to re-enter a new maze,

 

If your Maze is going to be the same routes each time then you could just create and array with possible directions and a html file name to load

Share this post


Link to post
Share on other sites

Thanks for your reply.

 

I'll try the html file array thing. But is there a more efficient way to keep the maze the same everytime? If I were to make a big maze, then the amount of individual html files would take up a lot of space.

 

Also, how could I go about keeping the user at the same place in the maze after they logged out and logged back in?

Share this post


Link to post
Share on other sites

Thanks for your reply.

 

I'll try the html file array thing. But is there a more efficient way to keep the maze the same everytime? If I were to make a big maze, then the amount of individual html files would take up a lot of space.

Not really, (depends what you class as a lot), the reason for the HTML files is just for graphic design, if you think of a 5x5 maze that's 25 rooms,

0  1  2  3  4

5  6  7  8  9 

10 11 12 13 14

15 16 17 18 19

20 21 22 23 24

 

Now just say you that's an array

$pos = 0; //in room 0
$maze = array("SE","EW","SEW","W", "S", "N","S","X"); //Rooms 0 to 7 (because I'm lazy)

I start in room 0 and need to get to 7

using the logic above

i include X.html to show the user some pretty stuff, but the choices can be PHP driven, infact you could cheat and have rooms with X doors and just show that.

 

heres the basic logic i used

I can go

[0 ] South or East,

now I'm in room 0.html  I go East

 

[1] East or West

now I'm in room 1.html I go East

 

[2] South or East or West

now I'm in room 2.html I go East

 

[3] West

now I'm in room 3.html I go West

 

[2] South or East or West

now I'm in room 2.html I go West

 

[1] East or West

now I'm in room 1.html I go West

 

[0 ] South or East,

now I'm in room 0.html I go South

 

[5] North

now I'm in room 5.html I go North

 

[0 ] South or East,

now I'm in room 0.html I go East

 

[1] East or West

now I'm in room 1.html I go East

 

[2] South or East or West

now I'm in room 2.html I go South

 

[7] Exit

Done :)

 

Also, how could I go about keeping the user at the same place in the maze after they logged out and logged back in?

Just store their position in the database linked to their account

Share this post


Link to post
Share on other sites

More complete example

$maze = array(
  [0]=> "S",
  [1]=> "EX",
  [2]=> "EW",
  [3]=> "EW",
  [4]=> "SW",
  [5]=> "NE",
  [6]=> "EW",
  [7]=> "SW",
  [8]=> "SE",
  [9]=> "NW",
  [10]=> "SE",
  [11]=> "SW",
  [12]=> "NS",
  [13]=> "NSE",
  [14]=> "SW",
  [15]=> "NS",
  [16]=> "N",
  [17]=> "NS",
  [18]=> "N",
  [19]=> "NS",
  [20]=> "NE",
  [21]=> "EW",
  [22]=> "NEW",
  [23]=> "EW",
  [24]=> "NW");

 

[attachment deleted by admin]

Share this post


Link to post
Share on other sites

Ah, okay, I see, thanks for clarifying!

 

So lets say I'm using 4 doors, I could just use four door images(north door, south door, east door, west door) and use if statements to display them depending on the room?

 

For example, if I was using the maze you created above:

if ($maze == 22) {
    echo $north;
    echo $east;
    echo $west
}

 

But then, that wouldn't really utilize the array. I haven't had much experience with them; could you explain to me how I could use that to display the different areas?

 

Thanks!

Share this post


Link to post
Share on other sites

You could shorten it to

$x = $_SESSION['pos']; //users position
if(strpos($maze[$x],"N")) echo "<img src=\"north.jpg\">";
if(strpos($maze[$x],"S")) echo "<img src=\"south.jpg\">";
if(strpos($maze[$x],"E")) echo "<img src=\"east.jpg\">";
if(strpos($maze[$x],"W")) echo "<img src=\"west.jpg\">";

 

EDIT: of course this would mean all the doors look the same..

 

to add some flavour you could add a check to see if the door is unique ie

$x = $_SESSION['pos']; //users position
if(strpos($maze[$x],"N")) echo (file_exists("$x_north.jpg"))?"<img src=\"".$x."_north.jpg\">":"<img src=\"north.jpg\">";
//same as above for South East West

 

so for room 22 north door your create a file called

22_north.jpg

Share this post


Link to post
Share on other sites

Okay, so let me test my knowledge of php here...

 

The code that you just wrote checks the array values to see if N, S, E, and/or W is there. If it is it echos the door image?

 

I think I am starting to understand. I'll leave this unsolved for now, though, just in case I run in to any more problems.

 

Thanks for your help!

 

Share this post


Link to post
Share on other sites

Yes,

here the longer version

 

if(strpos($maze[$x],"N")) echo (file_exists($x."_north.jpg"))?"<img src=\"".$x."_north.jpg\">":"<img src=\"north.jpg\">";
// is the same as

if(strpos($maze[$x],"N"))//if N is found
{
//check to see if the file "X_north.jpg" exists (X being the position)
if(file_exists($x."_north.jpg"))
{
	//display image
	echo "<img src=\"".$x."_north.jpg\">";
}else{
	//display image
	echo "<img src=\"north.jpg\">";
}
}

 

PS their a bug in the previous version

file_exists("$x_north.jpg")

should be

file_exists($x."_north.jpg")

Share this post


Link to post
Share on other sites

Hmm, I'm not sure I understand the extended code.

 

//display image
echo "<img src=\"".$x."_north.jpg\">";

 

what would this do? does it mean I have to create an image for each position and direction?

 

Also, what would be the target of the doors?

// what would go in instead of the '#'?
<a href="#"><img src="north.jpg" /></a> 

 

Share this post


Link to post
Share on other sites

if your at room 22 and their a north door

and the 22_north.jpg file exist it will display it if that file doesn't exist it displays north.jpg

this allows you to add some extra doors with minimal code updates,

 

as for the target that depends on how your going to code it,

if you look at the 5x5

if you think of a 5x5 maze that's 25 rooms,

0  1  2  3  4

5  6  7  8  9 

10 11 12 13 14

15 16 17 18 19

20 21 22 23 24

then the all you need to do is workout what room is in that direction

 

So

West is X-1

East is X+1

North is X-5

South is X+5

if your passing this via Get then you would something like

<?php
$X = $_GET['X'];
$maze = array(/*maze details here*/);

if(strpos($maze[$x],"N"))//if N is found
{
echo "<a href=\"?X=".($X-5)."\">";
if(file_exists($x."_north.jpg"))
{
	//display image
		echo "<img src=\"".$x."_north.jpg\">";
}else{
	//display image
	echo "<img src=\"north.jpg\">";
}
echo "</a>";
}

if(strpos($maze[$x],"S"))//if S is found
{
echo "<a href=\"?X=".($X+5)."\">";
if(file_exists($x."_south.jpg"))
{
	//display image
		echo "<img src=\"".$x."_south.jpg\">";
}else{
	//display image
	echo "<img src=\"_south.jpg\">";
}
echo "</a>";
}

if(strpos($maze[$x],"E"))//if E is found
{
echo "<a href=\"?X=".($X+5)."\">";
if(file_exists($x."_east.jpg"))
{
	//display image
		echo "<img src=\"".$x."_east.jpg\">";
}else{
	//display image
	echo "<img src=\"_east.jpg\">";
}
echo "</a>";
}

if(strpos($maze[$x],"W"))//if W is found
{
echo "<a href=\"?X=".($X+5)."\">";
if(file_exists($x."_west.jpg"))
{
	//display image
		echo "<img src=\"".$x."_west.jpg\">";
}else{
	//display image
	echo "<img src=\"_west.jpg\">";
}
echo "</a>";
}
?>

 

please note this is all untested and I haven't written any real designs for this..

Share this post


Link to post
Share on other sites

Ah, okay, I see now. I just have to make sure that no information appears in the url.

 

Of course, I'll be doing this on a much larger scale, so we'll see how it goes. :)

 

Share this post


Link to post
Share on other sites

you could just pass N,S,E,W to the GET and hold the X in a session.

ie

switch($_GET['action'])
{
case "N":
$_SESSION['X']=$_SESSION['X']-5;
break;
case "S":
$_SESSION['X']=$_SESSION['X']+5;
break;
case "E":
$_SESSION['X']=$_SESSION['X']+1;
break;
case "W":
$_SESSION['X']=$_SESSION['X']-1;
break;
}
$X = $_SESSION['X'];

Share this post


Link to post
Share on other sites

Since the maze that I wanted wasn't in any sort of order (therefore I couldn't use X+1, X-1, etc), I decided to just list the door numbers for each position, and use a switch statement.

 

I also decided to link each door as an image input box, and submit the value of each box (written by foreach loop) via the $_POST method.

 

Example:

$room = $_POST['number'];

switch ($room) {
case 0: // START
	$doornum = array(30, 1, 29, 28, 31);
	foreach ($doornum as $aaa) {
  	echo "<input type=\"text\" name=\"$aaa\" src=\"door.png\"";
	}
	break;
}

Note: This applies to all doors, I only posted one. Also, I didn't posted the form code.

 

However, if the user were to view the source code, they would see the door numbers, and would be able to choose the id numbers they haven't visited yet.

 

How can I hide the door numbers and have 1, 2, 3, 4, 5 etc posted instead?

 

Share this post


Link to post
Share on other sites

don't confuse obscurity with security, just because you hide the number, anyone who looks at the html could just enter sequential numbers until they get a hit,

 

Personally I could try to keep the maze built into an array and work form their

somthing like this

<?php
session_start();
$rooms = array(
    0 => array(30, 1, 29, 28, 31),
    1 => array(0, 1)
);

$_SESSION['pos'] = $rooms[$_SESSION['pos']][$_POST['number']];
foreach ($rooms[$_SESSION['pos']] as $K=>$aaa) {
     echo "<input type=\"text\" name=\"$K\" src=\"door.png\"";
}
?>

 

 

Share this post


Link to post
Share on other sites

So, in attempt to not have to do my code over again, I am going to try to hide the numbers. If not I'll try the code you posted

 

Here is part of my code now. As you can see, I stored the door numbers in a hidden field. However, if a user looks at the source code, they could manipulate it/ or see which doors lead where.

 

switch ($room) {
case 0: // START
	$doornum = array(30, 1, 29, 28, 31);
echo "<table width=\"850px\"><tr>";
	foreach ($doornum as $doornumber) {
		echo "<td width=\"120px\"><form method=\"post\" action=\"maze\"><input type=\"hidden\" name=\"nextdoor\" value=\"" . $doornumber . "\"><img src=\"i/door.png\" width=\"180px\" height=\"180px\"><br><input type=\"submit\" value=\"Explore this cavern\" name=\"submit\"></form></td>";
	} unset($doornumber);
echo "</tr></table>";
	break;
case 1:
	$doornum = array(3, 2, 7, 29, 0, 30);
echo "<table width=\"850px\"><tr>";
	foreach ($doornum as $doornumber) {
		echo "<td width=\"120px\"><form method=\"post\" action=\"maze\"><input type=\"hidden\" name=\"nextdoor\" value=\"" . $doornumber . "\"><img src=\"i/door.png\" width=\"180px\" height=\"180px\"><br><input type=\"submit\" value=\"Explore this cavern\" name=\"submit\"></form></td>";
	} unset($doornumber);
echo "</tr></table>";
	break;

 

How could I store the door numbers without showing them in the source codes? Will I have to use sessions, and wouldn't that get kind of messy(er)? I'm afraid I don't understand the code you submitted above..

Share this post


Link to post
Share on other sites

okay the code I submitted.. get the current position and their selection  (room number 0 - X) i then check the array and get the new room number and set that to my position

 

so for this example i am going use this maze array

$rooms = array(
    0 => array(30, 1, 29, 28, 31),
    1 => array(0, 1),
/*more stuff*/
    29 => array(44, 32),
);

I start at 0

my new doors are $rooms[0]

and i have 5 options

0, 1, 2, 3, 4

 

keep in mind that in room 0

0=30

1=1

2=29

etc

(the user only selects a number 0, 1, 2, 3 or 4)

 

the user select 2

the code get the maze array and looks up $rooms[0][2]

this returns my new position which is 29

my new doors are $rooms[29]

and i have 2 options

0, 1

 

keep in mind that in room 29

0=44

1=32

(the user only selects a number 0 or 1)

etc etc

 

now i never reveal any room numbers just the index number always 0 to X

and i then lookup the next room by getting what door (array key) was selected and what room i was in

Share this post


Link to post
Share on other sites

Okay, it makes sense. Thanks for the explanation! So I set up my arrays as you did in your last post (by the way I have 71 rooms...)

 

But how would I set up my foreach loop? The loop would have to count how many doors in the room numbers array and I only know how to get it to display the index array..

 

Thanks!

Share this post


Link to post
Share on other sites

If you look at the last code I posted, it has the loop your use,

 

here's a revised version

 

<?php
session_start();
$rooms = array(
    0 => array(30, 1, 29, 28, 31),
    1 => array(0, 1),
/*more stuff*/
    29 => array(44, 32)
);
if(empty($_SESSION['pos']))
{
  $_SESSION['pos'] = 0;
}else{
  $_SESSION['pos'] = $rooms[$_SESSION['pos']][$_GET['number']];
}
if(!isset($rooms[$_SESSION['pos']]))
{
echo "Room error!";//attempting to bypass rooms will cause this
exit;
}
foreach ($rooms[$_SESSION['pos']] as $K=>$roomid) {
     //$roomid isn't used but its the room your going into
     //you may use this later !!
     echo "<a href=\"?number=$K\"><img src=\"door.png\" boarder=\"0\"></a>";
}
?>

 

 

Share this post


Link to post
Share on other sites

lets say that I had this array.

 

$room = array(
0 => array(1 => 30, 1, 29, 28, 31),
1 => array(1 => 3, 2, 7, 29, 0, 30),
2 => array(1 => 4, 5, 7, 1, 3),
3 => array(1 => 0, 0, 2, 1, 36),
4 => array(1 => 0, 0, 43, 5, 2),
5 => array(1 => 4, 43, 44, 0, 6, 2),
6 => array(1 => 5, 0, 7),
7 => array(1 => 2, 6, 0, 0, 8, 1),
8 => array(1 => 7, 9, 0, 14, 10, 29),
9 => array(1 => 8, 0, 0, 11, 13),
10 => array(1 => 29, 8, 14, 15),

);

 

How could I get a foreach loop to print out the values of a certain index number?

 

For example if $currentroom = 6 then the loop would be 5<br>0<br>7<br>.

 

I'm wanting to use the $_POST method to send my results as the $_GET method would diplay results in the url. Also, how could I store the room numbers in a variable? (ie 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

 

I'm sorry this is taking so long!

Share this post


Link to post
Share on other sites

How could I get a foreach loop to print out the values of a certain index number?

 

For example if $currentroom = 6 then the loop would be 5<br>0<br>7<br>.

See my previous post,

 

I'm wanting to use the $_POST method to send my results as the $_GET method would diplay results in the url.

I don't see a problem with using get as it will only show 0 to X (x being the door, not the room)

 

Also, how could I store the room numbers in a variable? (ie 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

 

I'm sorry this is taking so long!

 

From the foreach loop its $roomid variable

Share this post


Link to post
Share on other sites

Okay, so I tried the code that you submitted.

 

$rooms = array(
0 => array(1 => 30, 1, 29, 28, 31),
1 => array(1 => 3, 2, 7, 29, 0, 30),
2 => array(1 => 4, 5, 7, 1, 3),
3 => array(1 => 0, 0, 2, 1, 36),
4 => array(1 => 0, 0, 43, 5, 2),
5 => array(1 => 4, 43, 44, 0, 6, 2),
6 => array(1 => 5, 0, 7),
7 => array(1 => 2, 6, 0, 0, 8, 1),
8 => array(1 => 7, 9, 0, 14, 10, 29),
9 => array(1 => 8, 0, 0, 11, 13),
10 => array(1 => 29, 8, 14, 15),
             ...
);

if(empty($_SESSION['position'])) {
$_SESSION['position'] = 0;
} else {
$_SESSION['position'] = $rooms[$_SESSION['position']][$_GET['cavern']];
}

foreach ($rooms[$position] as $key => $value) {
     echo "<a href='?cavern=" . $key . "'><img src='i/door.png' width='130px' height='170px'></a>";
}

 

But it doesn't seem to be working correctly. When I try it, I'm not sure it changes rooms because there are always 5 doors. And I can't reach the end. I can't figure it out because the code looks like it should update $_SESSION['position'], but I don't think it is..

 

Share this post


Link to post
Share on other sites

thats because

foreach ($rooms[$position] as $key => $value) {

should be

foreach ($rooms[$_SESSION['position']] as $key => $value) {

Share this post


Link to post
Share on other sites

Oh, sorry, about that. My code was actually was the one that was posted, I was just testing something. But, unfortunately, it still is'nt updating correctly...

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.