* @copyright 2025 Bartolomé Sintes Marco * @license http://www.gnu.org/licenses/agpl.txt AGPL 3 or later * @version 2025-02-10 * @link http://www.mclibre.org * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ ?> Cartas pares iguales consecutivas. Matrices (3). Sin formularios. Ejercicios. PHP. Bartolomé Sintes Marco. www.mclibre.org

Cartas pares iguales consecutivas

Las $n cartas\n"; print "\n"; print "

\n"; foreach ($cartas as $carta) { print " \"$carta\n"; } print "

\n"; print "\n"; // Recorremos la matriz de cartas y si el valor de la carta es par, eliminamos el valor for ($i = 0; $i < $n; $i++) { if ($cartas[$i] % 2) { unset($cartas[$i]); } } // Mostramos las imágenes de las cartas (las eliminadas ya no se mostrarán) print "

Sin cartas impares

\n"; print "\n"; print "

\n"; foreach ($cartas as $carta) { print " \"$carta\n"; } print "

\n"; print "\n"; // Reindexamos la matriz de cartas para poderla recorrer con un bucle for // y que los índices sean consecutivos $cartas = array_values($cartas); // Recorremos la matriz de cartas (hasta el penúltimo valor) // comparando cada valor con el siguiente y si coinciden // damos el valor true a la variable testigo $consecutivas $consecutivas = false; for ($i = 0; $i < count($cartas) - 1; $i++) { if ($cartas[$i] == $cartas[$i + 1]) { $consecutivas = true; } } // Según el valor de consecutivas mostramos un mensaje distinto print $consecutivas ? "

Hay cartas iguales consecutivas

\n" : "

No hay cartas iguales consecutivas

\n"; print "\n"; ?>