Author Topic: [SOLVED] PHP maze type thing...  (Read 2663 times)

0 Members and 1 Guest are viewing this topic.

Offline lore_lanuTopic starter

  • Irregular
  • Posts: 44
    • View Profile
Re: PHP maze type thing...
« Reply #15 on: July 06, 2009, 11:15:22 PM »
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.

Code: [Select]
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..

Offline MadTechie

  • Guru
  • Freak!
  • *
  • Posts: 9,374
  • Gender: Male
  • I try to F1
    • View Profile
Re: PHP maze type thing...
« Reply #16 on: July 06, 2009, 11:41:53 PM »
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(
    
=> array(301292831),
    
=> array(01),
/*more stuff*/
    
29 => array(4432),
);

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
« Last Edit: July 06, 2009, 11:45:45 PM by MadTechie »
Computers are good at following instructions, but not at reading your mind.
The quality of a response, is usually directly related to the quality of the question. ©2009 mjdamato
I dunno about that.  A regular expression has a 0% chance of touching my penis.
the code is professionally made up but not working
Remember to Click Solved, how to ask questions - the smart way

Offline lore_lanuTopic starter

  • Irregular
  • Posts: 44
    • View Profile
Re: PHP maze type thing...
« Reply #17 on: July 08, 2009, 11:47:28 AM »
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!

Offline MadTechie

  • Guru
  • Freak!
  • *
  • Posts: 9,374
  • Gender: Male
  • I try to F1
    • View Profile
Re: PHP maze type thing...
« Reply #18 on: July 08, 2009, 12:25:41 PM »
If you look at the last code I posted, it has the loop your use,

here's a revised version

Code: [Select]
<?php
session_start
();
$rooms = array(
    
=> array(301292831),
    
=> array(01),
/*more stuff*/
    
29 => array(4432)
);
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>";
}
?>
 

[/quote]
Computers are good at following instructions, but not at reading your mind.
The quality of a response, is usually directly related to the quality of the question. ©2009 mjdamato
I dunno about that.  A regular expression has a 0% chance of touching my penis.
the code is professionally made up but not working
Remember to Click Solved, how to ask questions - the smart way

Offline lore_lanuTopic starter

  • Irregular
  • Posts: 44
    • View Profile
Re: PHP maze type thing...
« Reply #19 on: July 08, 2009, 04:22:51 PM »
lets say that I had this array.

Code: [Select]
$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!

Offline MadTechie

  • Guru
  • Freak!
  • *
  • Posts: 9,374
  • Gender: Male
  • I try to F1
    • View Profile
Re: PHP maze type thing...
« Reply #20 on: July 08, 2009, 04:37:11 PM »
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
Computers are good at following instructions, but not at reading your mind.
The quality of a response, is usually directly related to the quality of the question. ©2009 mjdamato
I dunno about that.  A regular expression has a 0% chance of touching my penis.
the code is professionally made up but not working
Remember to Click Solved, how to ask questions - the smart way

Offline lore_lanuTopic starter

  • Irregular
  • Posts: 44
    • View Profile
Re: PHP maze type thing...
« Reply #21 on: July 08, 2009, 06:32:37 PM »
Okay, so I tried the code that you submitted.

Code: [Select]
$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..

Offline MadTechie

  • Guru
  • Freak!
  • *
  • Posts: 9,374
  • Gender: Male
  • I try to F1
    • View Profile
Re: PHP maze type thing...
« Reply #22 on: July 08, 2009, 06:40:52 PM »
thats because
Code: [Select]
foreach ($rooms[$position] as $key => $value) {should be
Code: [Select]
foreach ($rooms[$_SESSION['position']] as $key => $value) {
Computers are good at following instructions, but not at reading your mind.
The quality of a response, is usually directly related to the quality of the question. ©2009 mjdamato
I dunno about that.  A regular expression has a 0% chance of touching my penis.
the code is professionally made up but not working
Remember to Click Solved, how to ask questions - the smart way

Offline lore_lanuTopic starter

  • Irregular
  • Posts: 44
    • View Profile
Re: PHP maze type thing...
« Reply #23 on: July 08, 2009, 06:44:31 PM »
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...

Offline MadTechie

  • Guru
  • Freak!
  • *
  • Posts: 9,374
  • Gender: Male
  • I try to F1
    • View Profile
Re: PHP maze type thing...
« Reply #24 on: July 08, 2009, 06:47:45 PM »
Erm... that doesn't help me much!
Computers are good at following instructions, but not at reading your mind.
The quality of a response, is usually directly related to the quality of the question. ©2009 mjdamato
I dunno about that.  A regular expression has a 0% chance of touching my penis.
the code is professionally made up but not working
Remember to Click Solved, how to ask questions - the smart way

Offline lore_lanuTopic starter

  • Irregular
  • Posts: 44
    • View Profile
Re: PHP maze type thing...
« Reply #25 on: July 08, 2009, 06:51:23 PM »
Code: [Select]
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\">$roomid</a><br>";
}

Okay so that's what I have posted under my array. Anytime I click on one of the room ids, it should change to a different set of room ids, right? Whenever I do click, however, the url updates (ie ?number=1) but the numbers stay the same.

Offline lore_lanuTopic starter

  • Irregular
  • Posts: 44
    • View Profile
Re: PHP maze type thing...
« Reply #26 on: July 08, 2009, 07:13:15 PM »
I did a little testing and it seems that

Code: [Select]
print $rooms[$_SESSION['pos']][$_GET['number']];

Outputs the right code but since that is the value of $_SESSION['pos'] the code
Code: [Select]
foreach ($rooms[$_SESSION['pos']] as $K=>$roomid) {

turns out equaling
Code: [Select]
foreach ($rooms[$rooms[$_SESSION['pos']][$_GET['number']]] as $K=>$roomid) {

Or something along those lines. I think that is causing problems..
« Last Edit: July 08, 2009, 07:15:00 PM by lore_lanu »

Offline MadTechie

  • Guru
  • Freak!
  • *
  • Posts: 9,374
  • Gender: Male
  • I try to F1
    • View Profile
Re: PHP maze type thing...
« Reply #27 on: July 08, 2009, 07:21:08 PM »
depending on how your code starts the maze.. you may want to change
Code: [Select]
if(empty($_SESSION['pos']))to
Code: [Select]
if(!isset($_SESSION['pos']))if your not setting before hand!

I also this has moved a little out side the ranges of "design"
Computers are good at following instructions, but not at reading your mind.
The quality of a response, is usually directly related to the quality of the question. ©2009 mjdamato
I dunno about that.  A regular expression has a 0% chance of touching my penis.
the code is professionally made up but not working
Remember to Click Solved, how to ask questions - the smart way

Offline lore_lanuTopic starter

  • Irregular
  • Posts: 44
    • View Profile
Re: PHP maze type thing...
« Reply #28 on: July 08, 2009, 07:35:31 PM »
I agree completely, but I think we are done anyways. :) Thanks a ton for your help! You rock!

Topic Solved.