Skip to content

Instantly share code, notes, and snippets.

@tarnfeld
Created January 4, 2011 23:48
Show Gist options
  • Save tarnfeld/765670 to your computer and use it in GitHub Desktop.
Save tarnfeld/765670 to your computer and use it in GitHub Desktop.
<?php
/* $board = "
+---+---+---+---+---+---+---+
| | | | | | | |
+---+---+---+---+---+---+---+
| | | R | | | | |
+---+---+---+---+---+---+---+
| R | | R | W | | | R |
+---+---+---+---+---+---+---+
| R | | W | W | | | W |
+---+---+---+---+---+---+---+
| W | W | R | R | | | R |
+---+---+---+---+---+---+---+
| W | W | R | R | W | W | R |
+---+---+---+---+---+---+---+
| W | W | R | W | R | R | W |
+---+---+---+---+---+---+---+"; */
$board = "
+---+---+---+---+---+---+---+
| | | | | | | |
+---+---+---+---+---+---+---+
| | | | | | | |
+---+---+---+---+---+---+---+
| | | | | | | |
+---+---+---+---+---+---+---+
| R | | | W | R | | R |
+---+---+---+---+---+---+---+
| W | | W | R | W | R | W |
+---+---+---+---+---+---+---+
| W | W | R | R | W | W | R |
+---+---+---+---+---+---+---+
| W | R | W | W | R | R | W |
+---+---+---+---+---+---+---+";
$lines = explode("\n", $board);
$count = count($lines);
for($x = 0; $x < $count; $x++)
{
$lines[$x] = str_replace(array("\t", "\r", "\n", " "), "", $lines[$x]);
if(substr($lines[$x], 0, 1) != '|') {
unset($lines[$x]);
continue;
}
$lines[$x] = explode("|", $lines[$x]);
unset($lines[$x][0]);
array_pop($lines[$x]);
}
foreach($lines as $i => $r) {
foreach($r as $j => $c) {
$does_streak_exist = false;
if($c != "") {
$streak_exists = array(
array(0, 1),
array(1, 0),
array(1, -1),
array(1, 1),
);
foreach($streak_exists as $streak) {
$x = $streak[0];
$y = $streak[1];
$s = array(1, 2, 3, 4);
foreach($s as $k) {
$l = (($i + ($y * $k)) > 0);
$m = (($j + ($x * $k)) > 0);
$q = false;
if(isset($lines[$i + ($y * $k)])) {
$q = $lines[$i + ($y * $k)];
}
$p = ($c == (is_array($q) && $q[(string) $j + ($x * $k)]));
if($l && $m && $p)
{
$does_streak_exist = true;
}
}
}
if($does_streak_exist) {
if($c == "R") {
$c = "W";
} else if($x == "W") {
$x = "R";
}
echo "\nStreaked Letter: " . $c . "\n\n";
exit;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment