<?php // Clase Abstract pieza abstract class Pieza { protected $filaActual = 0; protected $columnaActual = 0; protected $color = "red"; // Metodos Abstractos a ser implementados en las subclasses public abstract function MovimientoValido($fila, $columna); public abstract function FormaPieza(); public function Mover($fila, $columna) { // utilizando metodo abstracto que se implementara en las subclasses if($this->MovimientoValido($fila, $columna)) { $this->filaActual = $fila; $this->columnaActual = $columna; return true; } else { return false; } } public function Mostrar() { // utilizando metodo abstracto que se implementara en las subclasses echo $this->FormaPieza(); } } // Clase PiezaCirculo -> esta solo puede moverse verticamente de a un lugar class PiezaCirculo extends Pieza { //Constructor public function PiezaCirculo($color, $fila, $columna) { $this->color = $color; $this->filaActual = $fila; $this->columnaActual = $columna; } public function MovimientoValido($fila, $columna) { if($this->columnaActual != $columna) { return false; } if($this->filaActual - 1 != $fila && $this->filaActual + 1 != $fila) { return false; } return true; } public function FormaPieza() { return "O"; } } // Clase PiezaCruz -> esta solo puede moverse horizontalmente de a un lugar class PiezaCruz extends Pieza { //Constructor public function PiezaCruz($color, $fila, $columna) { $this->color = $color; $this->filaActual = $fila; $this->columnaActual = $columna; } public function MovimientoValido($fila, $columna) { if($this->filaActual != $fila) { return false; } if($this->columnaActual - 1 != $columna && $this->columnaActual + 1 != $columna) { return false; } return true; } public function FormaPieza() { return "X"; } } // Tablero que contiene las piezas class Tablero { private $casillas; // Constructor public function Tablero() { // Array de 3 x 3 con las Piezas $this->casillas = array ( array(new PiezaCruz("red", 0, 0), new PiezaCirculo("red", 0, 1), new PiezaCruz("red", 0, 2), new PiezaCirculo("red", 0, 3)), array(null ,null , null, null), array(null ,null , null, null), array(new PiezaCruz("green", 3, 0),new PiezaCirculo("green", 3, 1), new PiezaCruz("green", 3, 2), new PiezaCirculo("green", 3, 3)), ); $this->DibujarTablero(); } public function MoverPieza($fila, $columna, $filaDest, $columnaDest) { // TODO: Validar fila, columna filaDest, columanDest (que esten dentro dle tablero) $pieza = $this->casillas[$fila][$columna]; // utilizacion del metodo mover qwue llama al metodo abstractro(polimorfismo) if($pieza->Mover($filaDest, $columnaDest)) { // Muevo lña pieza en el tablero $this->casillas[$fila][$columna] = null; $this->casillas[$filaDest][$columnaDest] = $pieza; } else { echo "Movimiento no Permitido para la Pieza\n"; } $this->DibujarTablero(); } private function DibujarTablero() { echo "-----------------\n"; for ($i = 0; $i < 4; $i++) { echo" | "; for ($j = 0; $j < 4; $j++) { $pieza = $this->casillas[$i][$j]; if($pieza != null) { $pieza->Mostrar(); echo" | "; } else { echo " | "; } } echo "\n"; } echo "-----------------\n"; } } $tablero = new Tablero(); // Movimiento valido (circulo verticalmente) $tablero->MoverPieza(0, 1, 1, 1); // Movimiento valido (circulo verticalmente) $tablero->MoverPieza(3, 3, 2, 3); // Movimiento valido (cruz horizontalmente) $tablero->MoverPieza(0, 0, 0, 1); // Movimiento invalido (cruz verticalmente) $tablero->MoverPieza(3, 0, 2, 0); ?>
Write, Run & Share PHP code online using OneCompiler's PHP online compiler for free. It's one of the robust, feature-rich online compilers for PHP language, running on the latest version 7. Getting started with the OneCompiler's PHP compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as PHP
and start coding.
OneCompiler's PHP online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample PHP program which takes name as input and prints hello message with your name.
<?php
fscanf(STDIN, "%s\n", $name);
echo "Hello ".$name.".\n";
?>
PHP(Hypertext Preprocessor) is widely used server sripting language by Rasmus Lerdorf in the year 1994.
In PHP, there is no need to explicitly declare variables to reserve memory space. When you assign a value to a variable, declaration happens automatically. Variables are case-sensitive in PHP.
$variable_name = value;
If, If-else, Nested-Ifs are used when you want to perform a certain set of operations based on conditional expressions.
if(conditional-expression){
//code
}
if(conditional-expression){
//code if condition is true
} else {
//code if condition is false
}
if(condition-expression1) {
//code if above condition is true
} elseif(condition-expression2){
//code if above condition is true
}
elseif(condition-expression3) {
//code if above condition is true
}
...
else {
//code if all the conditions are false
}
Switch is used to execute one set of statement from multiple conditions.
switch(conditional-expression) {
case value1:
// code if the above value is matched
break; // optional
case value2:
// code if the above value is matched
break; // optional
...
default:
// code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
// code
}
// you can use any of the below syntax
foreach ($array as $element-value) {
//code
}
foreach ($array as $key => $element-value) {
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while(condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.
function function_name(parameters) {
//code
}
function_name (parameters)