Ejercicio palabras mal escritas

 <style>
 .celdako,.celdaok{
 cursor: pointer;
 width:100px;
 height:50px;
 text-align: center;
 }
 .celdaok:active {
 background-color:lightgreen;
 
 }
 .celdako:active {
 background-color: red;
 }

 </style>

<?php

 function muestraTabla($tabla) {
 echo "<pre>";
 print_r($tabla);
 echo "</pre>";
 }

 $palabras = array(
 array('length', array('lenght', 'legnth', 'langth', 'lentgh', 'lenhgt')),
 array('locomotion', array('lomocotion', 'lotocomion', 'molocotion', 'locamotion', 'locomocion')),
 array('bedroom', array('vedroom', 'bedrum', 'berdoom', 'bedrun', 'bedroon', 'beddroom')),
 array('coin', array('coyn', 'cony', 'cain', 'con')),
 array('stable', array('estable', 'stoble', 'stabel', 'tsable')),
 array('crush', array('cruhs', 'clush', 'cruss', 'crusah')),
 array('fantasy', array('fantasi', 'fantascy', 'fatnasy', 'frantasy')),
 array('pillow', array('pellow', 'pillou', 'pilluw', 'pilow', 'pilou')),
 );

 function array_to_table($tabla, $buena,$ancho=2) {
 $res = "<table border=1>";
 for ($i = 0; $i < count($tabla); $i++) {
 if ($i % $ancho == 0) {
 $res .= "<tr>";
 }
 $res .= '<td class="' . ($tabla[$i] == $buena ? 'celdaok' : 'celdako') . '">' . $tabla[$i] . "</td>";
 if ($i % $ancho == $ancho-1) {
 $res .= "</tr>";
 }
 }
 $res .= "</table>";
 return $res;
 }

 //Seleccionar una palabra al azar rand (0,count($palabras)-1)

 $palabra = $palabras[rand(0, count($palabras) - 1)];
 // muestraTabla($palabra);
 //Coger la palabra buena y seleccionar tres palabras malas al azar
 $tablero[] = $palabra[0];
 // $malos = $palabra[1];

 //shuffle($malos);
 //$malos=array_slice($malos, 0, 8);
 $tablero = array_merge($tablero, $palabra[1]);
 while (count($tablero)<9){
 $a=$palabras[array_rand($palabras)];
 
 $palazar=$a[1][array_rand($a[1])];
 if (!in_array($palazar, $tablero)){
 $tablero[]=$palazar;
 }
 }
 // muestraTabla($tablero);
 //Mezclar
 shuffle($tablero);
 //Y mostrar
 echo array_to_table($tablero, $palabra[0],3);
 ?>

Publicado por

Avatar del usuario

Juan Pablo Fuentes

Formador de programación y bases de datos