function scanDiagonalsGravelyForWin() { $a_tmp_column = array(); $a_winning_set = array(); $start_rows = $this->rows - matchLength(); $end_rows = $this->rows - 1; $start_cols = 0; $end_cols = $this->columns - 1; $max_cols = matchLength() - 1; $y = $start_cols; $x = $start_rows; $i_max = $this->rows * $this->columns; $outer_x = $x; $outer_y = $y; $a_coords = array(); $i = 0; $j = 0; while ($outer_y <= matchLength()) { while ($x <= $end_rows && $x >= 0 && $outer_y <= $max_cols && $y <= $end_cols) { if ($j == 0) { $s_start = "start_y = {$y} start_x = {$x}<br/>\n"; $i_start_x = $x; $i_start_y = $y; } $s_end = "end_y = {$y} end_x = {$x}<br/>\n"; $i_end_x = $x; $i_end_y = $y; $a_tmp_column[] = $this->layout[$x][$y]; $a_coords[] = "{$x}:{$y}"; $y++; $x++; $j++; } $j = 0; $a_winning_set = findWinner($a_tmp_column, $i, $this->x, $this->o, 'gravely'); // find first value filled in a_tmp_column if (is_array($a_winning_set)) { // TODO: should I use i_pad to increment? hell ya $p_limit = count($a_tmp_column); for ($p = 0; $p < $p_limit; $p++) { if (strlen($a_tmp_column[$p]) > 1) { $i_pad = $p; break; } } $i_diff_x = $i_end_x - $i_start_x; $i_diff_y = $i_end_y - $i_start_y; $i_need_x = $i_diff_x - matchLength(); $i_need_y = $i_diff_y - matchLength(); if ($i_need_x >= 0) { $i_start_x += $i_pad; } if ($i_need_y >= 0) { $i_start_y += $i_pad; } if (isset($a_winning_set['x'])) { $a_winning_set['x'][0] = $i_start_y; $a_winning_set['x'][1] = $i_start_x; } if (isset($a_winning_set['o'])) { $a_winning_set['o'][0] = $i_start_y; $a_winning_set['o'][1] = $i_start_x; } $this->isGameOver = TRUE; $a_winning_set['how'] = 'gravely'; $this->a_winning_spot = $a_winning_set; } $a_coords = array(); // clear everything out again if (count($a_tmp_column) > 0) { $i++; } $a_tmp_column = array(); if ($outer_x >= 0) { $outer_x--; } else { $outer_x = 0; $outer_y++; } $x = $outer_x; $y = $outer_y; } }
function findWinner($a_set, $i_index, $x = '', $o = '') { $match_length = matchLength(); $set_size = count($a_set); $i_tries = $set_size - $match_length + 1; $start_point = 0; $end_point = $set_size - $i_tries; if (strlen($x) <= 0) { $x = 'x'; } if (strlen($o) <= 0) { $o = 'o'; } // loop through places to try // turn this into a while loop $x_count = 0; $o_count = 0; $i = 0; $j = 0; $i_sent = 1; $i_sent2 = 1; while ($i_sent2 > 0) { while ($i_sent > 0) { if (isset($a_set[$j])) { if (strcmp($a_set[$j], $x) == 0) { $x_count++; } } if ($x_count == $match_length) { $i_sent = 0; $i_sent2 = 0; return array('x' => array($i_index, $i, $j)); } if (isset($a_set[$j])) { if (strcmp($a_set[$j], $o) == 0) { $o_count++; } } if ($o_count == $match_length) { $i_sent = 0; $i_sent2 = 0; return array('o' => array($i_index, $i, $j)); } if ($j >= $end_point) { $i_sent = 0; } $j++; } // end while loop $start_point++; $end_point++; $j = $start_point; $x_count = 0; $o_count = 0; $i_sent = 1; if ($i >= $i_tries) { $i_sent2 = 0; } $i++; } // end while loop // no winner return FALSE; }