Created
January 21, 2014 14:16
-
-
Save alxy/8540888 to your computer and use it in GitHub Desktop.
4-Gewinnt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Playground implements Serializable | |
{ | |
public $rows = array(); | |
public function __construct() { | |
} | |
public function init() { | |
for($i=1; $i<=7; $i++) { | |
$this->rows[] = new Row; | |
} | |
} | |
public function isWon($key) { | |
$field = end($this->rows[$key]->fields); | |
$fieldKey = array_keys($this->rows[$key]->fields, $field)[0]; | |
$vertical = $this->getValues($this->rows[$key]->fields); | |
$horizontal = $this->getValues(array_map(function($row) { | |
return array_intersect_key($row, array($fieldKey => $field))[0]; | |
}, $this->rows)); | |
$diagonalUp = $this->getValues($this->getDiagonalUp($this->rows, $key)); | |
$diagonalDown = $this->getValues($this->getDiagonalDown($this->rows, $key)); | |
$possibilities = array($vertical, $horizontal, $diagonalUp, $diagonalDown); | |
foreach ($possibilities as $possibility) { | |
$count = $this->count($possibility); | |
if ($this->fourInARow($count)) { | |
return true; | |
} | |
} | |
return false; | |
} | |
private function getDiagonalUp($rows, $rowKey) { | |
$diagonal = array(); | |
foreach ($rows as $key => $row) { | |
$difference = $key - $rowKey; | |
$newKey = $key + $difference; | |
$value = (isset($row[$newKey])) ? $row[$newKey] : null; | |
$diagonal[] = $value | |
} | |
return $diagonal; | |
} | |
private function getDiagonalDown($rows, $rowKey) { | |
$diagonal = array(); | |
foreach ($rows as $key => $row) { | |
$difference = $key - $rowKey; | |
$newKey = $key - $difference; | |
$value = (isset($row[$newKey])) ? $row[$newKey] : null; | |
$diagonal[] = $value | |
} | |
return $diagonal; | |
} | |
private function count($array) { | |
$counts = array(); | |
$count = 0; | |
$last = ""; | |
foreach($array as $key => $value) { | |
if($value != $last) { | |
array_push($counts, array($count, $last)); | |
$count = 0; | |
$last = $value; | |
} | |
$count++; | |
} | |
array_shift($counts); | |
array_push($counts, array($count, $value)); | |
return $counts; | |
} | |
private function fourInARow($counts) { | |
foreach ($counts as $count) { | |
if ($count[0] >= 4) { | |
return true; | |
} | |
} | |
return false; | |
} | |
private function getValues($array) { | |
return array_map(function($field) { | |
return $field->value; | |
}, $array); | |
} | |
public function serialize() { | |
return serialize($this->rows); | |
} | |
public function unserialize($data) { | |
$this->rows = unserialize($rows); | |
} | |
} | |
class Row implements Serializable | |
{ | |
public $fields = array(); | |
public function __construct() { | |
} | |
public function addField(Field $field) { | |
if ($this->isValid()) { | |
$this->fields[] = $field; | |
} | |
} | |
protected function isValid() { | |
return count($this->fields) < 6; | |
} | |
public function serialize() { | |
return serialize($this->fields); | |
} | |
public function unserialize($data) { | |
$this->fields = unserialize($fields); | |
} | |
} | |
class Field implements Serializable | |
{ | |
public $value; | |
public function __construct($value) { | |
$this->value = $value; | |
} | |
public function serialize() { | |
return serialize($this->value); | |
} | |
public function unserialize($data) { | |
$this->value = unserialize($value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment