NIVEL 2 - A (mensaje 3)
La clase Algoritmo:
Código PHP:
<?php
class Algoritmo {
private $arrOrd;
public function Algoritmo() {
$this->arrOrd = array("2" => 2, "3" => 3, "4" => 4, "5" => 5, "6" => 6, "7" => 7, "8" => 8, "9" => 9, "10" => 10, "j" => 11, "q" => 12, "k" => 13, "a" => 14);
}
public function isEscaleraReal($carts) {
$result = array();
$isEscaleraColor = $this->isEscaleraColor($carts);
if ($isEscaleraColor['bool']) {
$result['color'] = $isEscaleraColor['color'];
if ($isEscaleraColor['min'] == 10 && $isEscaleraColor['max'] == 14) {
$result['bool'] = true;
} else {
$result['bool'] = false;
}
} else {
$result['bool'] = false;
}
return $result;
}
public function isEscaleraColor($carts) {
$result = array();
$isEscalera = $this->isEscalera($carts);
$isColor = $this->isColor($carts);
$result['min'] = $isEscalera['min'];
$result['max'] = $isEscalera['max'];
if ($isEscalera['bool'] && $isColor['bool']) {
$result['color'] = $isColor['color'];
$result['bool'] = true;
} else {
$result['bool'] = false;
}
return $result;
}
public function isPoker($carts) {
$result = array();
$cartsAgrupadas = $this->agruparCartas($carts);
//$exist = array_search('2', $cartsAgrupadas);
$exist = array_keys($cartsAgrupadas, '4');
if (count($exist) == 0) {
$result['bool'] = false;
} else {
$result['bool'] = true;
$result['val'] = $this->arrOrd[$exist[0]];
}
return $result;
}
public function isFull($carts) {
$result = array();
$exist1 = $this->isTrio($carts);
$exist2 = $this->isParejaODoble($carts);
if ($exist2['bool'] == true && $exist2['modo'] == "normal" && $exist1['bool']) {
$result['bool'] = true;
$result['val'] = array(array("trio", $exist1['val']), array("pareja", $exist2['val']));
} else {
$result['bool'] = false;
}
return $result;
}
public function isColor($carts) {
$result = array();
$color = true;
for ($i = 0; $i < (count($carts) - 1) && $color; $i++) {
if ($carts[$i]->getPalo() != $carts[$i + 1]->getPalo()) {
$color = false;
}
}
if ($color) {
$result['color'] = $carts[0]->getPalo();
$result['val'] = $this->arrOrd[$carts[count($carts) - 1]->getValor()];
$result['bool'] = true;
} else {
$result['bool'] = false;
}
return $result;
}
public function isEscalera($carts) {
$result = array();
$result['max'] = $this->arrOrd[$carts[count($carts) - 1]->getValor()];
$result['min'] = $this->arrOrd[$carts[0]->getValor()]; //para calcular si es escalera real
$escalera = true;
for ($i = 0; $i < (count($carts) - 1) && $escalera; $i++) {
if (($this->arrOrd[$carts[$i]->getValor()] + 1) != $this->arrOrd[$carts[$i + 1]->getValor()]) {
if (($this->arrOrd[$carts[$i + 1]->getValor()] == 14) && ($this->arrOrd[$carts[$i]->getValor()] == 5)) {
$escalera = true;
} else {
$escalera = false;
}
}
}
if ($escalera) {
$result['bool'] = true;
if ($result['max'] == 14 && $result['min'] == 2) {
$result['min'] = $result['max'];
$result['max'] = 5;
}
} else {
if ($result['max'] == 5 && $result['min'] == 14) {
$result['bool'] = true;
} else {
$result['bool'] = false;
}
}
return $result;
}
public function isTrio($carts) {
$result = array();
$cartsAgrupadas = $this->agruparCartas($carts);
//$exist = array_search('2', $cartsAgrupadas);
$exist = array_keys($cartsAgrupadas, '3');
if (count($exist) == 0) {
$result['bool'] = false;
} else {
$result['bool'] = true;
$result['val'] = $this->arrOrd[$exist[0]];
}
return $result;
}
public function isParejaODoble($carts) {
$result = array();
$cartsAgrupadas = $this->agruparCartas($carts);
$exist = array_keys($cartsAgrupadas, '2');
if (count($exist) == 2) {
$result['bool'] = true;
$result['val'] = array($this->arrOrd[$exist[0]], $this->arrOrd[$exist[1]]);
$result['modo'] = "dobles";
$aux = $this->cartaAlta($carts, $result['val']);
$result['max'] = $aux['val'];
} elseif (count($exist) == 1) {
$result['bool'] = true;
$result['val'] = $this->arrOrd[$exist[0]];
$result['modo'] = "normal";
$aux = $this->cartaAlta($carts, $result['val']);
$result['max'] = $aux['val'];
} else {
$result['bool'] = false;
}
return $result;
}
public function cartaAlta($carts, $excluir = null) {
$result = array();
$masAlta = null;
foreach ($carts as $val) {
if ($excluir != null) {
if (is_array($excluir)) {
if ($this->arrOrd[$val->getValor()] != $excluir[0] && $this->arrOrd[$val->getValor()] != $excluir[1]) {
if ($masAlta == null) {
$masAlta = $val->getValor();
} else {
if ($this->arrOrd[$val->getValor()] > $this->arrOrd[$masAlta]) {
$poner = true;
foreach($excluir as $exc) {
if($val->getValor() == $exc) {
$poner = false;
}
}
if($poner) {
$masAlta = $val->getValor();
}
}
}
}
} else {
if ($this->arrOrd[$val->getValor()] != $excluir) {
if ($masAlta == null) {
$masAlta = $val->getValor();
} else {
if ($this->arrOrd[$val->getValor()] > $this->arrOrd[$masAlta]) {
$masAlta = $val->getValor();
}
}
}
}
} else {
if ($masAlta == null) {
$masAlta = $val->getValor();
} else {
if ($this->arrOrd[$val->getValor()] > $this->arrOrd[$masAlta]) {
$masAlta = $val->getValor();
}
}
}
}
$result['val'] = $masAlta;
return $result;
}
private function agruparCartas($carts) {
$result = array();
$result['2'] = 0;
$result['3'] = 0;
$result['4'] = 0;
$result['5'] = 0;
$result['6'] = 0;
$result['7'] = 0;
$result['8'] = 0;
$result['9'] = 0;
$result['10'] = 0;
$result['j'] = 0;
$result['q'] = 0;
$result['k'] = 0;
$result['a'] = 0;
for ($i = 0; $i < count($carts); $i++) {
switch ($carts[$i]->getValor()) {
case "2":
$result['2'] += 1;
break;
case "3":
$result['3'] += 1;
break;
case "4":
$result['4'] += 1;
break;
case "5":
$result['5'] += 1;
break;
case "6":
$result['6'] += 1;
break;
case "7":
$result['7'] += 1;
break;
case "8":
$result['8'] += 1;
break;
case "9":
$result['9'] += 1;
break;
case "10":
$result['10'] += 1;
break;
case "j":
$result['j'] += 1;
break;
case "q":
$result['q'] += 1;
break;
case "k":
$result['k'] += 1;
break;
case "a":
$result['a'] += 1;
break;
default:
break;
}
}
return $result;
}
public function ordenarCartas($cartas) {
$cards = array();
$resultado = array();
foreach ($cartas as $key => $value) {
$cards[$key] = $this->arrOrd[$value->getValor()];
}
asort($cards);
foreach ($cards as $cla => $val) {
$resultado[] = $cartas[$cla];
}
return $resultado;
}
}
?>