Jump to content

PHP Number grid puzzler!


Mutley

Recommended Posts

Hi guys, having a problem with my script. Feel free to upload it to understand, below is the full file.

 

The problem: I want numbers on the grid to be unique but move X / Y like they do now, by 1 row and column at a time. At the moment, it has the same number repeated on each row, what I would like it to do, is start from -15 for example at the top left, then count upto 15 to the bottom right.

 

<style type="text/css">

td{

border:1px solid #000;

padding:10px;

}

.navigation td{

text-align:center;

}

</style>

 

<?php

$x = $_GET['x']; // Horizontal

$y = $_GET['y']; // Vertical

$x_limit = 15; // Horizontal Navigation Limit

$y_limit = 15; // Vertiacal Navigation Limit

 

if($x > $x_limit || $y > $y_limit || $x < -$x_limit || $y < -$y_limit){

$x = 0;

$y = 0;

}

 

$n=($x-($y*10)); // Cell ID (Number)

 

$h=array('20', '52', '123', '155', '104'); // Numbers to Highlight

 

echo '<table style="border:1px">';

$r=0; // Row

while($r<10){

$c=0; // Column

 

echo '<tr>';

while($c<10){

if(in_array($n, $h)){ echo '<td style="background:#CCC">';}else{ echo '<td>';} echo $n++.'</td>';

$c++; // Next Column

}

echo '</tr>';

$r++; // Next Row

}

echo '</table>';

?>

 

<br /><br />

<table class="navigation">

<tr>

<td> </td><td><?php if($y<$y_limit){ echo '<a href="?x='.$x.'&y='.($y+1).'">Up</a><br />';}?></td><td> </td>

</tr><tr>

<td><?php if($x>-$x_limit){ echo '<a href="?x='.($x-1).'&y='.$y.'">Left</a><br />';}?></td>

<td><?php echo '<a href="?x=0&y=0">Reset</a><br />';?></td>

<td><?php if($x<$x_limit){ echo '<a href="?x='.($x+1).'&y='.$y.'">Right</a><br />';}?></td>

</tr><tr>

<td> </td><td><?php if($y>-$y_limit){ echo '<a href="?x='.$x.'&y='.($y-1).'">Down</a><br />';}?></td><td> </td>

</tr>

</table>

 

 

Hope you guys understand! Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/164013-php-number-grid-puzzler/
Share on other sites

I'll try to explain, as I've had no replies. :(

 

At the moment, it does this:

 

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,25,26,27,28,29,30

 

Which would be fine... if that was the limits of the square but instead, it goes much further, so you get numbers repeating themselves:

 

0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20

11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30

21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40

 

What I would like it to do is make each number unique and in a FIXED position, so when you navigate around, it stays at that location but doesn't end up duplicating later on.

 

Thanks in advance.

Archived

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

×
×
  • 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.