Share Pastebin
Guest
Public paste!

Pascal Honore

By: a guest | Jan 26th, 2010 | Syntax: PHP | Size: 5.38 KB | Hits: 89 | Expires: Never
Copy text to clipboard
  1. <?php
  2.  
  3. function nslog($object){
  4.         if(is_string($object) or is_numeric($object)){
  5.                 echo $object."\n";
  6.         }else{
  7.                 echo serialize($object)."\n";
  8.         }
  9. }
  10. class Cell {
  11.         private $value = null;
  12.         public function __construct(){
  13.                
  14.         }
  15.         public function setValue($number){
  16.                 $this->value = $number;
  17.         }
  18.         public function value(){
  19.                 return $this->value;
  20.         }
  21.         public function resolveAttempt($board){
  22.                 if(is_null($this->value)){
  23.                         $possiblevalues = array();
  24.                         foreach(range(1,9) as $xvalue){
  25.                                 $this->value = $xvalue;
  26.                                 if($board->selfTest()){
  27.                                         $possiblevalues[] = $xvalue;
  28.                                 }
  29.                         }
  30.                         if(count($possiblevalues)==1){
  31.                                 $this->value = $possiblevalues[0];
  32.                         }else{
  33.                                 $this->value = null;
  34.                         }
  35.                 }
  36.         }
  37. }
  38. class Board {
  39.  
  40.         private $lines = array();
  41.  
  42.         public function __construct(){
  43.                 foreach(range(1,9) as $linenumber){
  44.                         $line = array();
  45.                         foreach(range(1,9) as $rownumber){
  46.                                 $line[] = new Cell();
  47.                         }
  48.                         $this->lines[] = $line;
  49.                 }
  50.         }
  51.         public function lines(){
  52.                 return $this->lines;
  53.         }
  54.         public function columns(){
  55.                 $answer = array();
  56.                 foreach(range(1,9) as $columnnumber){
  57.                         $column = array();
  58.                         foreach(range(1,9) as $linenumber){
  59.                                 $column[] = $this->lines[$linenumber-1][$columnnumber-1];
  60.                         }
  61.                         $answer[] = $column;
  62.                 }
  63.                 return $answer;
  64.         }
  65.  
  66.                 // $yindice = 0,1,2
  67.         // $xindice = 0,1,2
  68.         public function zone($yindice,$xindice){
  69.                 $answer = array();
  70.                 foreach(range(($yindice*3)+1,($yindice*3)+3) as $linenumber){
  71.                         foreach(range(($xindice*3)+1,($xindice*3)+3) as $columnnumber){
  72.                                 $answer[] = $this->lines[$linenumber-1][$columnnumber-1];
  73.                         }
  74.                 }
  75.                 return $answer;        
  76.         }
  77.         public function zones(){
  78.                 $answer = array();
  79.                 foreach(range(0,2) as $yindice){
  80.                         foreach(range(0,2) as $xindice){
  81.                                 $answer[] = $this->zone($yindice,$xindice);
  82.                         }
  83.                 }
  84.                 return $answer;
  85.         }
  86.         public function allcells(){
  87.                 $answer = array();
  88.                 foreach(range(1,9) as $columnnumber){
  89.                         foreach(range(1,9) as $linenumber){
  90.                                 $answer[] = $this->lines[$linenumber-1][$columnnumber-1];
  91.                         }
  92.                 }      
  93.                 return $answer;
  94.         }
  95.  
  96.         // line_number = 1,...,9 ; row_number = 1,...,9
  97.         public function setValueForCell($line_number,$row_number,$value){
  98.                 $cell = $this->lines[$line_number-1][$row_number-1];
  99.                 $cell->setValue($value);
  100.         }
  101.        
  102.         public function toString($separator){
  103.                 $string = '';
  104.                 foreach($this->lines() as $line){
  105.                         foreach($line as $cell){
  106.                                 if($cell->value()){
  107.                                         $string .= $cell->value();
  108.                                 }else{
  109.                                         $string .= $separator;
  110.                                 }
  111.                         }
  112.                         $string .= "\n";
  113.                 }
  114.                 return $string;
  115.         }
  116.  
  117.         public function selfTest(){
  118.                 if(!$this->selfTest_Lines()){
  119.                         return false;
  120.                 }
  121.                 if(!$this->selfTest_Columns()){
  122.                         return false;
  123.                 }
  124.                 if(!$this->selfTest_Zones()){
  125.                         return false;
  126.                 }
  127.                 return true;
  128.         }
  129.         public function selfTest_Lines(){
  130.                 // We need to establish that elements in lines occur at least once.
  131.                 foreach($this->lines() as $collection){
  132.                         if(!$this->selfTest_Collection($collection)){
  133.                                 return false;
  134.                         }
  135.                 }
  136.                 return true;
  137.         }
  138.         public function selfTest_Columns(){
  139.                 // We need to establish that elements in columns occur at least once.
  140.                 foreach($this->columns() as $collection){
  141.                         if(!$this->selfTest_Collection($collection)){
  142.                                 return false;
  143.                         }
  144.                 }
  145.                 return true;
  146.         }
  147.         public function selfTest_Zones(){
  148.                 // We need to establish that elements in square zones occur at least once.
  149.                 foreach($this->zones() as $collection){
  150.                         if(!$this->selfTest_Collection($collection)){
  151.                                 return false;
  152.                         }
  153.                 }
  154.                 return true;
  155.         }
  156.         public function selfTest_Collection($collection){
  157.                 $already_found = array();
  158.                 foreach($collection as $cell){
  159.                         if($cell->value() and in_array($cell->value(),$already_found)){
  160.                                 return false;
  161.                         }else{
  162.                                 $already_found[] = $cell->value();
  163.                         }
  164.                 }
  165.                 return true;
  166.         }
  167.  
  168.         public function onePassResolutionAttempt(){
  169.                 foreach($this->allcells() as $cell){
  170.                         $cell->resolveAttempt($this);
  171.                 }
  172.         }
  173.         public function isComplete(){
  174.                 $count = 0;
  175.                 foreach($this->allcells() as $cell){
  176.                         if($cell->value()){
  177.                                 $count++;
  178.                         }
  179.                 }
  180.                 return ($count==81);
  181.         }
  182.  
  183. }
  184.  
  185. $board = new Board();
  186.  
  187. $board->setValueForCell(1,1,1);
  188. $board->setValueForCell(1,4,6);
  189. $board->setValueForCell(1,6,4);
  190. $board->setValueForCell(1,8,9);
  191. $board->setValueForCell(1,9,3);
  192.  
  193. $board->setValueForCell(2,2,3);
  194. $board->setValueForCell(2,4,8);
  195. $board->setValueForCell(2,5,9);
  196. $board->setValueForCell(2,8,2);
  197.  
  198. $board->setValueForCell(3,6,1);
  199. $board->setValueForCell(3,7,4);
  200. $board->setValueForCell(3,8,6);
  201.  
  202. $board->setValueForCell(4,3,3);
  203. $board->setValueForCell(4,6,2);
  204. $board->setValueForCell(4,7,7);
  205.  
  206. $board->setValueForCell(5,1,9);
  207. $board->setValueForCell(5,4,7);
  208. $board->setValueForCell(5,5,5);
  209. $board->setValueForCell(5,6,8);
  210. $board->setValueForCell(5,9,1);
  211.  
  212. $board->setValueForCell(6,3,5);
  213. $board->setValueForCell(6,4,1);
  214. $board->setValueForCell(6,7,2);
  215.  
  216. $board->setValueForCell(7,2,9);
  217. $board->setValueForCell(7,3,1);
  218. $board->setValueForCell(7,4,3);
  219.  
  220. $board->setValueForCell(8,2,7);
  221. $board->setValueForCell(8,5,1);
  222. $board->setValueForCell(8,6,5);
  223. $board->setValueForCell(8,8,8);
  224.  
  225. $board->setValueForCell(9,1,5);
  226. $board->setValueForCell(9,2,6);
  227. $board->setValueForCell(9,4,4);
  228. $board->setValueForCell(9,6,9);
  229. $board->setValueForCell(9,9,2);
  230.  
  231. echo $board->toString('.');
  232. while(!$board->isComplete()){
  233.         $board->onePassResolutionAttempt();
  234. }
  235. nslog('');
  236. echo $board->toString('.');