Advertisement
Guest User

Untitled

a guest
Sep 18th, 2014
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.79 KB | None | 0 0
  1. <form action="" method="GET">
  2.     <input type="text" name="x" placeholder="ilosc kolumn">
  3.     <input type="text" name="y" placeholder="ilosc wierszy">
  4.     <input type="submit" value="Generuj">
  5. </form>
  6. <?php
  7.  
  8. $tabx = (isset($_GET['x']) && (int)$_GET['x'] ) ? (int)$_GET['x'] : 6;
  9. $taby = (isset($_GET['y']) && (int)$_GET['y'] ) ? (int)$_GET['y'] : 6;
  10.  
  11.  
  12. function print_tab($tab) {
  13.  
  14.     foreach ($tab as $trow) {
  15.         foreach ($trow as $tcell) {
  16.             echo '<div style="width:65px;text-align:center;margin:0px;padding:14px 3px;float:left;border:1px solid #ddd;">';
  17.                 echo $tcell;
  18.             echo '</div>';
  19.         }
  20.         echo '<div style="clear:both"></div>';
  21.     }
  22.     echo "<br>";
  23.     echo "<br>";
  24.  
  25. }
  26.  
  27. function generate_tab($x,$y){
  28.  
  29.     $top = true;
  30.     $right = true;
  31.  
  32.     $array = array();
  33.     $up = true;
  34.     $right = true;
  35.     $val = 1;
  36.    
  37.     $cursor_x = -1;
  38.     $cursor_y = 0;
  39.     $half =false;
  40.     $count = $x * $y;
  41.     while ($count>0) {
  42.  
  43.         if ($cursor_x == $x-1 && $cursor_y == 0) {
  44.             $up = !$up;
  45.             $right = !$right;
  46.             $cursor_x++;
  47.             $half = true;
  48.         }
  49.  
  50.         $cursor_y += $up ? -1 : 1;
  51.         $cursor_x += $right ? 1 : -1;
  52.        
  53.  
  54.         if ($cursor_y < 0) {
  55.             $cursor_y++;
  56.             $up = !$up;
  57.             $right = !$right;
  58.         }
  59.         if ($cursor_x < 0) {
  60.             $cursor_x++;
  61.             $up = !$up;
  62.             $right = !$right;
  63.         }
  64.         if ($cursor_y >= $y) {
  65.             $cursor_y--;
  66.             $up = !$up;
  67.             $right = !$right;
  68.  
  69.             if ($half) {
  70.                 $cursor_x++;
  71.                 $cursor_x++;
  72.             }
  73.         }
  74.         if ($cursor_x >= $x) {
  75.             $cursor_x--;
  76.             $up = !$up;
  77.             $right = !$right;
  78.             if ($half) {
  79.                 $cursor_y++;
  80.                 $cursor_y++;
  81.             }
  82.         }
  83.         if (!isset($array[$cursor_y][$cursor_x])) {
  84.         $array[$cursor_y][$cursor_x] = $val;
  85.            
  86.         } else {
  87.         $array[$cursor_y][$cursor_x] .= ' '.$val;
  88.         }
  89.  
  90.        
  91.  
  92.         $val++;
  93.         $count--;
  94.     }
  95.  
  96.  
  97.     return $array;
  98. }
  99.  
  100. $tab = generate_tab($tabx,$taby);
  101.  
  102. print_tab($tab);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement