Advertisement
cdsatrian

class diamond square number

Dec 13th, 2012
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.42 KB | None | 0 0
  1. <?php
  2. class diamond
  3. {
  4.   public $rst=array();
  5.   public $n;
  6.  
  7.   public function __construct($n=3)
  8.   {
  9.     $this->set_n($n);
  10.   }
  11.   public final function set_n($n){
  12.     $this->n=$n;
  13.   }
  14.   protected final function generate()
  15.   {
  16.     for($i=0;$i<$this->n*2;$i++){
  17.       $this->rst[$i]=array();
  18.       if($i<$this->n){
  19.         for($j=0;$j<=$i;$j++){
  20.           $this->rst[$i][]=($this->n*$i+1)-($this->n-1)*$j;
  21.         }
  22.       }else{
  23.         for($j=2*$this->n-$i-1;$j>0;$j--){
  24.           $this->rst[$i][]=($j+$i+1)*$this->n-($this->n*$this->n+$j)+1;
  25.         }
  26.       }
  27.     }
  28.    
  29.   }
  30.   public function show()
  31.   {
  32.     $this->generate();
  33.     $space=strlen(($this->n*$this->n))+1;
  34.     for($i=0;$i<$this->n*2;$i++)
  35.     {
  36.       $s=count($this->rst[$i]);
  37.       $rst='';
  38.       for($j=0;$j<$s;$j++)
  39.       {
  40.         $rst.=str_pad($this->rst[$i][$j], $space, " ", STR_PAD_BOTH);
  41.       }
  42.       echo str_pad($rst, $this->n*$space, " ", STR_PAD_BOTH)."\n";
  43.     }
  44.   }
  45. }
  46.  
  47. class diamond_table extends diamond
  48. {
  49.   public function __contruct($n=3)
  50.   {
  51.     parent::__contruct($n);
  52.   }
  53.   public function show()
  54.   {
  55.     $this->generate();
  56.     $space=strlen(($this->n*$this->n))+1;
  57.     $side=$this->n*2-1;
  58.     $rst='<table>';
  59.     for($i=0;$i<$side;$i++)
  60.     {
  61.       $s=count($this->rst[$i]);
  62.       $rst.='<tr>';
  63.       $c=0;
  64.       for($j=0;$j<$side;$j++)
  65.       {
  66.         $rst.='<td>';
  67.         if($j==round($side/2)-$s+2*$c && $c<$s){
  68.            $rst.=$this->rst[$i][$c];
  69.            $c++;
  70.         }else{
  71.           $rst.='&nbsp;';
  72.         }
  73.         $rst.='</td>';
  74.       }
  75.       $rst.="</tr>\n";
  76.     }
  77.     $rst.='</table>';
  78.     echo $rst;
  79.   }
  80. }
  81.  
  82. $number=isset($_GET['number'])?$_GET['number']:3;
  83. ?>
  84. <!DOCTYPE html>
  85. <html>
  86. <head>
  87.   <title>Diamond Square Number</title>
  88.   <style>
  89.     html,body {margin:0px;padding:0px;font-family:Arial,verdana,sans-serif;font-size:10px;color:#003;}
  90.     td {text-align:center;width:10px;}
  91.   </style>
  92. </head>
  93. <body>
  94.   <nav>
  95.     <form id='frm'>
  96.     Input number [1-30] <input type="text" name="number" value="<?php echo $number;?>" />
  97.     <input type="button" value="process" onClick="frm.submit();" />
  98.     </form>
  99.   </nav>
  100.   <div id="content">
  101.     <div id="tablebox">
  102. <?php
  103. $d=new diamond_table($number);
  104. $d->show();
  105. ?>
  106.     </div>
  107.     <div id="plainbox">
  108.       <pre>  
  109. <?php
  110. $d=new diamond($number);
  111. $d->show();
  112. ?>
  113.       </pre>
  114.     </div>
  115.   </div>
  116. </body>
  117. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement