tom2oo8 Posted September 4, 2009 Share Posted September 4, 2009 Ok im trying to create a small grid mini game where users can hunt for fish by clicking a square and hoping to hit the fish. The fish is only meant to move 1 left , 1 right , 1 up , 1 down per click so its possible. I used a grid script found off a diffrent forum and tried moding it to my needs, however, the first problem was it was making more fish than it was meant to, but i fixed that by making it check if a number is 1 then at the end of the if it added 1. However i soon encountered problems i couldnt solve as the fish wouldnt move only 1 space. (i think i might know why while posing this) Heres the code and i was woundering how i would fix the problem: <?php //game map test //set the grid size $grid_y = (int)10; $grid_x = (int)10; //x and y rows to display at once $display_rows = (int)10; //default display cordinate $x = (int)1; $y = (int)1; $param_x = $_REQUEST["xcord"]; $param_y = $_REQUEST["ycord"]; if (isset($param_x) && isset($param_y)) { //validate that the parameter is a legit point if (($param_x <= $grid_x) && ($param_x >= 1) && ($param_y <= $grid_y) &&($param_y >= 1)) { $x = (int)$param_x; $y = (int)$param_y; } } //the grid position desired will be set to display in the center of the viewable grid $display_half = round($display_rows / 2); $other_half = $display_rows - $display_half; //to display the target in the middle, you have to get the number of rows to display before and after it. //some simple math to take care of that. $start_x = ($x - $display_half) +1; $end_x = $x + $other_half; //if the $start_x variable is less than 1 the grid would be in the negatives. so set it to 1 if ($start_x < 1) { $start_x = 1; $end_x = $display_rows; } else //if $end_x is off the grid we have to compensate and add the remaining rows to the start. if ($end_x > $grid_x) { $extra = $end_x - $grid_x; $end_x = $grid_x; $start_x = $start_x - $extra; } //same applies for the y axis $start_y = ($y - $display_half) +1; $end_y = $y + $other_half; if ($start_y < 1) { $start_y = 1; $end_y = $display_rows; } else if ($end_y > $grid_y) { $extra = $end_y - $grid_y; $end_y = $grid_y; $start_y = $start_y - $extra; } //showing the current parameters echo "X $x - Y $y<br>"; ?> <!--grid table--> <table cellspacing="0" cellpadding="0" border="0"> <?php //these 2 for loops represent the y and x axis //using the data collected above the loops will properly display the grid for ($Ty = $start_y; $Ty <= $end_y; $Ty++) { //start new row echo '<tr>'; for ($Tx = $start_x; $Tx <= $end_x; $Tx++) { //show grid DisplayGrid($Tx,$Ty); } echo '</tr>'; } ?> </table> <?php function DisplayGrid($gridx,$gridy) { global $x, $y; $bgcolor = 'style="background-image: url(/water.gif) ; border: 0 0 0 0; cursor:pointer;cursor:hand"'; //highlight current select grid coordinate $link = 'onClick="document.location.href=\'grid.php?xcord=' . $gridx . '&ycord=' . $gridy.' \';" '; $i = 0; while($i <= 6) { $randx = rand(1,10); $randy = rand(1,10); if ($gridx == $randx && $gridy == $randy) { $bgcolor = 'style="background-image: url(/weed.gif) ; border: 0 0 0 0; "'; $link = 'null'; } $i++; } if(!$_SESSION['fishx']) { $fx = rand(1,10); $fy = rand(1,10); } else { $fx = $_SESSION['fishx'] ; $fy = $_SESSION['fishy'] ; } $random = rand(0,4); if($random == 1) { $fx = $fx + 1; } elseif($random == 2) { $fx = $fx - 1; } elseif($random == 3) { $fy = $fy + 1; } else { $fy = $fy - 1; } if ($gridx == $fx && $gridy == $fy) { $e = 1; if($e == 1) { $bgcolor = 'style="background-image: url(/fish.gif) ; border: 0 0 0 0; "'; $link = 'null'; $e = 2; } } echo "<td width=\"25\" height=\"25\" $link $bgcolor align=center valign=center></td>"; $_SESSION['fishx'] = $fx; $_SESSION['fishy'] = $fy; } if($_GET['page'] == 'reset') { session_destroy(); } ?> Link to comment https://forums.phpfreaks.com/topic/173168-solved-help-php-grid-game-just-doesnt-do-what-i-want-it-too/ Share on other sites More sharing options...
tom2oo8 Posted September 5, 2009 Author Share Posted September 5, 2009 Not sure if your allowed to bump on this forums but: Bump Link to comment https://forums.phpfreaks.com/topic/173168-solved-help-php-grid-game-just-doesnt-do-what-i-want-it-too/#findComment-912826 Share on other sites More sharing options...
SilveR316 Posted September 5, 2009 Share Posted September 5, 2009 Ok... there are a few things fundamentally wrong with your script. 1) At line 118 where you start your random move direction code. This piece of code gets called every time the DisplayGrid() function is called. This is probably why its moving more than 1 spot. If you var_dump($_SESSION) at the end of the script, you'll see that after every page reload the numbers increase/decrease a large amount (at one point it was at x:300 y: -600 and it wouldn't display a fish at all). 2) The way the grid is printed is a little weird. You can't determine the previous/next position of the fish inside the loop. The reason for this is because every time you loop through the rows, the position of the fish will change. If x and y don't match for the table cell its generating, the fish won't display. So... you need to predetermine the position of the fish before you start printing the table cells. Each time you loop through to print the cells, you need to check if the x and y position match the new position of your fish that you predetermined before you started the loop. If it is, you show the fish. If not, you show water or weeds. Link to comment https://forums.phpfreaks.com/topic/173168-solved-help-php-grid-game-just-doesnt-do-what-i-want-it-too/#findComment-912892 Share on other sites More sharing options...
tom2oo8 Posted September 5, 2009 Author Share Posted September 5, 2009 Ok... there are a few things fundamentally wrong with your script. 1) At line 118 where you start your random move direction code. This piece of code gets called every time the DisplayGrid() function is called. This is probably why its moving more than 1 spot. If you var_dump($_SESSION) at the end of the script, you'll see that after every page reload the numbers increase/decrease a large amount (at one point it was at x:300 y: -600 and it wouldn't display a fish at all). 2) The way the grid is printed is a little weird. You can't determine the previous/next position of the fish inside the loop. The reason for this is because every time you loop through the rows, the position of the fish will change. If x and y don't match for the table cell its generating, the fish won't display. So... you need to predetermine the position of the fish before you start printing the table cells. Each time you loop through to print the cells, you need to check if the x and y position match the new position of your fish that you predetermined before you started the loop. If it is, you show the fish. If not, you show water or weeds. Ah i see what you mean. But ive tried taking the code for the position out of the loop and ive place it at the top of the page, however that just seems to never display the fish at all? Link to comment https://forums.phpfreaks.com/topic/173168-solved-help-php-grid-game-just-doesnt-do-what-i-want-it-too/#findComment-912895 Share on other sites More sharing options...
SilveR316 Posted September 5, 2009 Share Posted September 5, 2009 After looking at it a bit more, you'll need to determine the positions of the weeds before generating the table cells. I had some free time on my hands, so I create a little example based on your code. It demonstrates how to predetermine the positions of the weeds, and the fish (and also move it 1 cell in some direction). Of course, there are a few things that can be added still. Things such as checking if the weed positions don't overwrite each other (since they are randomly generated coordinates without checking if those coordinates were already generated). You'll need to slightly modify the code to fit yours though. I didn't have the images, so I just used background colors. <?php session_start(); // grid size $grid_y = 10; $grid_x = 10; // default coordinates $x = 1; $y = 1; $param_x = isset($_REQUEST['xcord']) ? $_REQUEST['xcord'] : null; $param_y = isset($_REQUEST['ycord']) ? $_REQUEST['ycord'] : null; // vaidate the parameter coords are legit if ($param_x && $param_y) { if (($param_x <= $grid_x) && ($param_x >= 1) && ($param_y <= $grid_y) && ($param_y >= 1)) { $x = (int)$param_x; $y = (int)$param_y; } } // predetermine weed positions (6 of them) $weed_positions = array(); for ($i = 0; $i < 6; $i++) { $weed_y = rand(1,10); $weed_x = rand(1,10); $weed_positions[$weed_y][] = $weed_x; } // predetermine fish position if(isset($_SESSION['fishx']) && isset($_SESSION['fishy'])) { $fish_x = $_SESSION['fishx']; $fish_y = $_SESSION['fishy']; // since we got the fish position from the session, it means we had a previous fish position // so move it 1 cell in some direction switch (rand(1,4)) { case 1: if ($fish_x == 10) continue; // sanity check to make sure fish doesnt go outside of border $fish_x++; break; case 2: if ($fish_x == 1) continue; // sanity check to make sure fish doesnt go outside of border $fish_x--; break; case 3: if ($fish_y == 10) continue; // sanity check to make sure fish doesnt go outside of border $fish_y++; break; case 4: default: if ($fish_y == 1) // sanity check, but different since we already we already went through all other cases { $fish_y++; break; } $fish_y--; } } else { $fish_x = rand(1,10); $fish_y = rand(1,10); } // store fish position in session $_SESSION['fishx'] = $fish_x; $_SESSION['fishy'] = $fish_y; function DisplayGrid($x, $y) { global $weed_positions, $fish_x, $fish_y; //highlight current select grid coordinate $bgcolor = 'style="background-color: blue; border: 0 0 0 0; cursor: pointer; cursor:hand"'; $link = 'onClick="document.location.href=\'test.php?xcord=' . $x . '&ycord=' . $y.' \';" '; // check for weed if (isset($weed_positions[$y]) && in_array($x, $weed_positions[$y])) { $bgcolor = 'style="background-color: green ; border: 0 0 0 0; "'; $link = 'null'; } // check for fish if ($x == $fish_x && $y == $fish_y) { $bgcolor = 'style="background-color: red ; border: 0 0 0 0; "'; $link = 'null'; } echo "<td width=\"25\" height=\"25\" $link $bgcolor align=center valign=center></td>"; } ?> <table cellspacing="0" cellpadding="0" border="0"> <?php for ($iY = 1; $iY <= $grid_y; $iY++) { //start new row echo '<tr>'; for ($iX = 1; $iX <= $grid_x; $iX++) { //show grid DisplayGrid($iX,$iY); } echo '</tr>'; } ?> </table> Link to comment https://forums.phpfreaks.com/topic/173168-solved-help-php-grid-game-just-doesnt-do-what-i-want-it-too/#findComment-912901 Share on other sites More sharing options...
tom2oo8 Posted September 5, 2009 Author Share Posted September 5, 2009 After looking at it a bit more, you'll need to determine the positions of the weeds before generating the table cells. I had some free time on my hands, so I create a little example based on your code. It demonstrates how to predetermine the positions of the weeds, and the fish (and also move it 1 cell in some direction). Of course, there are a few things that can be added still. Things such as checking if the weed positions don't overwrite each other (since they are randomly generated coordinates without checking if those coordinates were already generated). You'll need to slightly modify the code to fit yours though. I didn't have the images, so I just used background colors. <?php session_start(); // grid size $grid_y = 10; $grid_x = 10; // default coordinates $x = 1; $y = 1; $param_x = isset($_REQUEST['xcord']) ? $_REQUEST['xcord'] : null; $param_y = isset($_REQUEST['ycord']) ? $_REQUEST['ycord'] : null; // vaidate the parameter coords are legit if ($param_x && $param_y) { if (($param_x <= $grid_x) && ($param_x >= 1) && ($param_y <= $grid_y) && ($param_y >= 1)) { $x = (int)$param_x; $y = (int)$param_y; } } // predetermine weed positions (6 of them) $weed_positions = array(); for ($i = 0; $i < 6; $i++) { $weed_y = rand(1,10); $weed_x = rand(1,10); $weed_positions[$weed_y][] = $weed_x; } // predetermine fish position if(isset($_SESSION['fishx']) && isset($_SESSION['fishy'])) { $fish_x = $_SESSION['fishx']; $fish_y = $_SESSION['fishy']; // since we got the fish position from the session, it means we had a previous fish position // so move it 1 cell in some direction switch (rand(1,4)) { case 1: if ($fish_x == 10) continue; // sanity check to make sure fish doesnt go outside of border $fish_x++; break; case 2: if ($fish_x == 1) continue; // sanity check to make sure fish doesnt go outside of border $fish_x--; break; case 3: if ($fish_y == 10) continue; // sanity check to make sure fish doesnt go outside of border $fish_y++; break; case 4: default: if ($fish_y == 1) // sanity check, but different since we already we already went through all other cases { $fish_y++; break; } $fish_y--; } } else { $fish_x = rand(1,10); $fish_y = rand(1,10); } // store fish position in session $_SESSION['fishx'] = $fish_x; $_SESSION['fishy'] = $fish_y; function DisplayGrid($x, $y) { global $weed_positions, $fish_x, $fish_y; //highlight current select grid coordinate $bgcolor = 'style="background-color: blue; border: 0 0 0 0; cursor: pointer; cursor:hand"'; $link = 'onClick="document.location.href=\'test.php?xcord=' . $x . '&ycord=' . $y.' \';" '; // check for weed if (isset($weed_positions[$y]) && in_array($x, $weed_positions[$y])) { $bgcolor = 'style="background-color: green ; border: 0 0 0 0; "'; $link = 'null'; } // check for fish if ($x == $fish_x && $y == $fish_y) { $bgcolor = 'style="background-color: red ; border: 0 0 0 0; "'; $link = 'null'; } echo "<td width=\"25\" height=\"25\" $link $bgcolor align=center valign=center></td>"; } ?> <table cellspacing="0" cellpadding="0" border="0"> <?php for ($iY = 1; $iY <= $grid_y; $iY++) { //start new row echo '<tr>'; for ($iX = 1; $iX <= $grid_x; $iX++) { //show grid DisplayGrid($iX,$iY); } echo '</tr>'; } ?> </table> Wow, thanks alot , i realy wasnt expecting someone to do it for me xD. I dont mind too much that the weed somtimes overlap as it adds more "random-ness" to the game. But a huge thanks to your help Link to comment https://forums.phpfreaks.com/topic/173168-solved-help-php-grid-game-just-doesnt-do-what-i-want-it-too/#findComment-912903 Share on other sites More sharing options...
SilveR316 Posted September 5, 2009 Share Posted September 5, 2009 I didn't do it for you, I just prefer to teach by example. I fully expect you to comb through every line of that code and learn why I did everything the way I did. Link to comment https://forums.phpfreaks.com/topic/173168-solved-help-php-grid-game-just-doesnt-do-what-i-want-it-too/#findComment-912904 Share on other sites More sharing options...
tom2oo8 Posted September 5, 2009 Author Share Posted September 5, 2009 I didn't do it for you, I just prefer to teach by example. I fully expect you to comb through every line of that code and learn why I did everything the way I did. I have already read all your code Now all i gotta do is addin the database entrys and walla im done Link to comment https://forums.phpfreaks.com/topic/173168-solved-help-php-grid-game-just-doesnt-do-what-i-want-it-too/#findComment-912914 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.