Ver Mensaje Individual
  #13 (permalink)  
Antiguo 27/01/2010, 09:09
Avatar de dggluz
dggluz
 
Fecha de Ingreso: abril-2009
Ubicación: Buenos Aires, Argentina
Mensajes: 525
Antigüedad: 15 años
Puntos: 50
Respuesta: [APORTE] Generador de sudokus sencillo y explicado

Resto del código del ejemplo completo:
Código PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Sudoku</title>
    <style>
        .sudoku
        {
            border-style:solid;
            border-width:2px;
            border-color:#000000;
            border-collapse:collapse;
        }
        .sudoku TD
        {
            border-style:solid;
            border-width:1px;
            border-color:#000000;
            width:1.5em;
            height:1.5em;
            text-align:center;
        }
    </style>
    <script type="text/javascript">
        function toogle(id)
        {
            var elm=document.getElementById(id);
            if(elm.style.display=='none')
            {
                elm.style.display='block';
            }
            else
            {
                elm.style.display='none';
            }
        }
    </script>
</head>
<body>
    <?php
        $sudokuCompleto
=completarMatrizSudoku();
        if(isset(
$_GET['dificultad']) && ((int)$_GET['dificultad'])<=28 && ((int)$_GET['dificultad'])>=1)
        {
            
$dificultad=$_GET['dificultad'];
        }
        else
        {
            
$dificultad=NULL;
        }
        
$sudoku=borrarCasilleros($sudokuCompleto$dificultad);
    
?>
    <br />
    <table class="sudoku">
        <?php
            
foreach($sudoku as $idx=>$valor)
            {
                if(
$idx&#37;9==1)
                
{
                    echo 
"<tr>";
                }
                echo 
"<td style=\"";
                if(
$idx%3==0)
                {
                    echo 
"border-right-width:2px;";
                }
                if(
ceil($idx/9)%3==0)
                {
                    echo 
"border-bottom-width:2px;";
                }
                echo 
"\">";
                echo 
$valor."</td>";
                if(
$idx%9==0)
                {
                    echo 
"</tr>";
                }
            }
        
?>
    </table>
    <br />
    <form action="<?php echo $_SERVER['SCRIPT_NAME'];?>" method="get">
        Dificultad (de 1 a 28):&nbsp;<input type="text" size="2" name="dificultad" maxlength="2" />&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" value="&iexcl;Generar!" />
    </form>
    <br />
    <input type="button" onclick="toogle('resultado');" value="Mostrar/Ocultar resultado" />
    <br />
    <div id="resultado" style="display:none;">
        <br />
        <table class="sudoku">
            <?php
                
foreach($sudokuCompleto as $idx=>$valor)
                {
                    if(
$idx%9==1)
                    {
                        echo 
"<tr>";
                    }
                    echo 
"<td style=\"";
                    if(
$idx%3==0)
                    {
                        echo 
"border-right-width:2px;";
                    }
                    if(
ceil($idx/9)%3==0)
                    {
                        echo 
"border-bottom-width:2px;";
                    }
                    echo 
"\">";
                    echo 
$valor."</td>";
                    if(
$idx%9==0)
                    {
                        echo 
"</tr>";
                    }
                }
            
?>
        </table>
    </div>
</body>
</html>
¡Suerte!