Advertisement
Guest User

Untitled

a guest
Aug 31st, 2012
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.45 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html dir="rtl">
  3. <head>
  4.     <title>סודוקו</title>
  5.     <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  6.     <meta name="viewport" content="height=device-height, width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=2.0, user-scalable=no, target-densitydpi=device-dpi" />
  7.     <script src="../jquery.js"></script>
  8.    
  9. <style type='text/css'>
  10. * {
  11.     padding: 0;
  12.     margin: 0;
  13. }
  14.  
  15. table {
  16.     direction: ltr;
  17.     margin: 50px auto;
  18.     border: 2px solid #000000;
  19.     display: none;
  20. }
  21.  
  22. td {
  23.     border: 1px solid #000000;
  24.     padding: 15px;
  25. }
  26.  
  27. td.blank {
  28.     background-color: #FF0000;
  29. }
  30. </style>
  31.  
  32. <script>
  33. var counter;
  34. $(document).ready(function(){
  35.     $soid = 0;
  36.  
  37.     $("#next").click(function(){
  38.         $(".sudoku_board").hide();
  39.         $("#" + ++$soid).show();
  40.     });
  41.    
  42.     $("#perv").click(function(){
  43.         $(".sudoku_board").hide();
  44.         $("#" + --$soid).show();
  45.     });
  46.  
  47.     //$soid = 50;
  48.     counter = setInterval("ShowNext()",10);
  49. });
  50.  
  51. function ShowNext(){
  52.     $(".sudoku_board").hide();
  53.     $("#" + ++$soid).show();        //  on load
  54.     if(!document.getElementById($soid)){
  55.         clearInterval(counter);
  56.         $soid--;
  57.         $("#" + $soid).show();
  58.     }
  59. }
  60. </script>
  61. </head>
  62. <body>
  63.  
  64. <?php
  65.  
  66. $sid = 0;       //  DEBUG, sudoku id
  67. $killme = 0;
  68.  
  69. $sudoku = Array(    1=> Array( 1=> 0,   2=> 0,  3=> 0,  4=> 0,  5=> 0,  6=> 0,  7=> 0,  8=> 0,  9=> 0),
  70.                     2=> Array( 1=> 0,   2=> 0,  3=> 0,  4=> 0,  5=> 0,  6=> 0,  7=> 0,  8=> 0,  9=> 0),
  71.                     3=> Array( 1=> 0,   2=> 0,  3=> 0,  4=> 0,  5=> 0,  6=> 0,  7=> 0,  8=> 0,  9=> 0),
  72.                     4=> Array( 1=> 0,   2=> 0,  3=> 0,  4=> 0,  5=> 0,  6=> 0,  7=> 0,  8=> 0,  9=> 0),
  73.                     5=> Array( 1=> 0,   2=> 0,  3=> 0,  4=> 0,  5=> 0,  6=> 0,  7=> 0,  8=> 0,  9=> 0),
  74.                     6=> Array( 1=> 0,   2=> 0,  3=> 0,  4=> 0,  5=> 0,  6=> 0,  7=> 0,  8=> 0,  9=> 0),
  75.                     7=> Array( 1=> 0,   2=> 0,  3=> 0,  4=> 0,  5=> 0,  6=> 0,  7=> 0,  8=> 0,  9=> 0),
  76.                     8=> Array( 1=> 0,   2=> 0,  3=> 0,  4=> 0,  5=> 0,  6=> 0,  7=> 0,  8=> 0,  9=> 0),
  77.                     9=> Array( 1=> 0,   2=> 0,  3=> 0,  4=> 0,  5=> 0,  6=> 0,  7=> 0,  8=> 0,  9=> 0)
  78. );
  79.  
  80. CreateNumbers(1,1);
  81. function CreateNumbers($row,$col){
  82.     global $sudoku,$sid,$killme;
  83.    
  84.     $killme++;      //  no more infinite loops!
  85.    
  86.     if($row > 9 || $row < 1){
  87.         return false;
  88.     }
  89.    
  90.     if($col > 9 || $col < 1){
  91.         return false;
  92.     }
  93.    
  94.     if($sid > 1000 || $killme > 1000){
  95.         return;
  96.     }
  97.    
  98.     for($putin = 1; $putin <= 9; $putin++){
  99.         if(CheckNumber($row,$col,$putin)){
  100.             $sudoku[$row][$col] = $putin;
  101.             PrintSudoku($row,$col);
  102.             CreateNumbers($row+1,$col);
  103.             CreateNumbers($row,$col+1);
  104.         }
  105.     }
  106. }
  107.  
  108. // Good location for number?
  109. function CheckNumber($row,$col,$new_num){
  110.     if(GoodRow($row,$col,$new_num)){                    //  row ok?
  111.         if(GoodCol($row,$col,$new_num)){                    //  col ok?
  112.             if(GoodBlock($row,$col,$new_num)){      //  block(3x3) ok?
  113.                 return true;
  114.             }
  115.         }
  116.     }
  117.  
  118.     return false;
  119. }
  120.  
  121. // Check a row and decide if the number is ok
  122. function GoodRow($row,$col,$num){
  123.     global $sudoku;
  124.     for($i = 1; $i <= 9; $i++){
  125.         if($sudoku[$row][$i] == $num){
  126.             return false;
  127.         }
  128.     }
  129.     return true;
  130. }
  131.  
  132. // Check a colume and decide if the number is ok
  133. function GoodCol($row,$col,$num){
  134.     global $sudoku;
  135.     for($i = 1; $i <= 9; $i++){
  136.         if($sudoku[$i][$col] == $num){
  137.             return false;
  138.         }
  139.     }
  140.     return true;
  141. }
  142.  
  143. // Check a block(3x3) and decide if the number is ok
  144. function GoodBlock($row,$col,$num){
  145.     global $sudoku;
  146.     $same_row = (int)(($row-1) / 3);
  147.     $same_col = (int)(($col-1) / 3);
  148.  
  149.     $start_row = (int)(($same_row*3)+1);
  150.     $stop_row = (int)(($same_row+1)*3);
  151.    
  152.     $start_col = (int)(($same_col*3)+1);
  153.     $stop_col = (int)(($same_col+1)*3);
  154.    
  155.     for($r = $start_row; $r <= $stop_row; $r++){
  156.         for($c = $start_col; $c <= $stop_col; $c++){
  157.             if($r == $row && $c == $col){
  158.                 continue;
  159.             }
  160.             //echo $sudoku[$r][$c]."<br />";
  161.             if($sudoku[$r][$c] == $num){
  162.                 return false;
  163.             }
  164.         }
  165.     }
  166.    
  167.     return true;
  168. }
  169.  
  170. function PrintSudoku($bold_row=0,$bold_col=0){
  171.     global $sudoku,$sid;
  172.     $sid++;
  173.    
  174.     echo "<table cellspacing='0' class='sudoku_board' id='{$sid}'>\r\n";
  175.         foreach($sudoku as $row_id => $col){
  176.             echo "<tr>\r\n";
  177.             foreach($col as $col_id => $value){
  178.                    
  179.                 $blank = ($value == 0)?"class='blank'":"";
  180.                
  181.                 if($row_id == $bold_row && $col_id == $bold_col){
  182.                     echo "<td style='background-color: #FFFF00;' {$blank}>{$value}</td>";
  183.                 }
  184.                 else {
  185.                     echo "<td {$blank}>{$value}</td>";
  186.                 }
  187.             }
  188.             echo "\r\n</tr>\r\n";
  189.         }
  190.     echo "</table>\r\n\r\n";
  191. }
  192. ?>
  193.  
  194. <?php PrintSudoku(); ?>
  195.  
  196. <button id='next'>הבא</button>
  197. <button id='perv'>הקודם</button>
  198.  
  199. </body>
  200. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement