Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <form action="" method="GET">
- <input type="text" name="x" placeholder="ilosc kolumn">
- <input type="text" name="y" placeholder="ilosc wierszy">
- <input type="submit" value="Generuj">
- </form>
- <?php
- $tabx = (isset($_GET['x']) && (int)$_GET['x'] ) ? (int)$_GET['x'] : 6;
- $taby = (isset($_GET['y']) && (int)$_GET['y'] ) ? (int)$_GET['y'] : 6;
- function print_tab($tab) {
- foreach ($tab as $trow) {
- foreach ($trow as $tcell) {
- echo '<div style="width:65px;text-align:center;margin:0px;padding:14px 3px;float:left;border:1px solid #ddd;">';
- echo $tcell;
- echo '</div>';
- }
- echo '<div style="clear:both"></div>';
- }
- echo "<br>";
- echo "<br>";
- }
- function generate_tab($x,$y){
- $top = true;
- $right = true;
- $array = array();
- $up = true;
- $right = true;
- $val = 1;
- $cursor_x = -1;
- $cursor_y = 0;
- $half =false;
- $count = $x * $y;
- while ($count>0) {
- if ($cursor_x == $x-1 && $cursor_y == 0) {
- $up = !$up;
- $right = !$right;
- $cursor_x++;
- $half = true;
- }
- $cursor_y += $up ? -1 : 1;
- $cursor_x += $right ? 1 : -1;
- if ($cursor_y < 0) {
- $cursor_y++;
- $up = !$up;
- $right = !$right;
- }
- if ($cursor_x < 0) {
- $cursor_x++;
- $up = !$up;
- $right = !$right;
- }
- if ($cursor_y >= $y) {
- $cursor_y--;
- $up = !$up;
- $right = !$right;
- if ($half) {
- $cursor_x++;
- $cursor_x++;
- }
- }
- if ($cursor_x >= $x) {
- $cursor_x--;
- $up = !$up;
- $right = !$right;
- if ($half) {
- $cursor_y++;
- $cursor_y++;
- }
- }
- if (!isset($array[$cursor_y][$cursor_x])) {
- $array[$cursor_y][$cursor_x] = $val;
- } else {
- $array[$cursor_y][$cursor_x] .= ' '.$val;
- }
- $val++;
- $count--;
- }
- return $array;
- }
- $tab = generate_tab($tabx,$taby);
- print_tab($tab);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement