hawk enterprises portfolio
hawk enterprises portfolio
hawk enterprises portfolio
hawk enterprises portfolio
hawk enterprises portfolio
hawk enterprises portfolio
hawk enterprises portfolio
hawk enterprises portfolio
hawk enterprises portfolio
hawk enterprises portfolio
hawk enterprises portfolio
hawk enterprises portfolio
hawk enterprises portfolio
hawk enterprises portfolio
 
counter
Select Country/Language FrancaisDeutschEspanolItalianoPortugeseJapaneseKoreanChineseArabicRussianEnglish
Current Projects
portland paranormal.com
xxk search
battlenow
bighawk casino
Hawk Enterprises News

PHP Pentago, Determine Winner, Win State algorithm

Tags: , , by Hawk on 05.11.08 4:36 am

Pentago is a great strategy game and certainly fun to play. Basically it’s played on a 6×6 grid that is made up of 4 3×3 quadrants that can rotate once after each turn. The idea behind the game is to get five marbles in a row. Building an interface should be fun but for those just interested in the algorithm to determine a win state for Pentago in php.

<?php
// php pentago by http://www.hawkenterprises.org

$board = array(0=>array(1,2,2,2,2,1),
1=>array(1,1,2,1,2,1),
2=>array(2,2,1,2,1,1),
3=>array(2,1,2,2,1,2),
4=>array(1,2,1,1,1,2),
5=>array(2,1,1,2,0,1));

function match_check($board,$nocheck = false){
$winner = false;
// horizontal check
for($y=0;$y<6;$y++){
$line_score2 = 0; // reset each new line
$line_score1 = 0; // reset each new line
for($x=0;$x<6;$x++){
if($board[$y][$x] == 2) $line_score2++;
if($board[$y][$x] == 1) $line_score1++;
}
if($line_score2 >= 5) {
// we now need to be more dilegent (zeros can only appear in the the first and last position)
$zerofound = false;
for($i=1;$i<4;$i++){
if($board[$y][$i] == 0) $zerofound = true;
if($board[$y][$i] == 1) $zerofound = true;
}
if($zerofound == false) {
$winner = true;
break;
}
}
if($line_score1 >= 5) {
// we now need to be more dilegent (zeros can only appear in the the first and last position)
$zerofound = false;
for($i=1;$i<4;$i++){
if($board[$y][$i] == 0) $zerofound = true;
if($board[$y][$i] == 2) $zerofound = true;
}
if($zerofound == false) {
$winner = true;
break;
}
}

}
if($winner == false){
// vertical check

for($x=0;$x<6;$x++){
$line_score2 = 0; // reset each new line
$line_score1 = 0; // reset each new line
for($y=0;$y<6;$y++){
if($board[$y][$x] == 2) $line_score2++;
if($board[$y][$x] == 1) $line_score1++;
}
if($line_score2 >= 5) {
// we now need to be more dilegent (zeros can only appear in the the first and last position)
$zerofound = false;
for($i=1;$i<4;$i++){
if($board[$i][$x] == 0) $zerofound = true; // only different by where the $i and x is from above
if($board[$i][$x] == 1) $zerofound = true;
}
if($zerofound == false) {
$winner = true;
break;
}
}

if($line_score1 >= 5) {
// we now need to be more dilegent (zeros can only appear in the the first and last position)
$zerofound = false;
for($i=1;$i<4;$i++){
if($board[$i][$x] == 0) $zerofound = true; // only different by where the $i and x is from above
if($board[$i][$x] == 2) $zerofound = true;
}
if($zerofound == false) {
$winner = true;
break;
}
}
}
}
if($winner == false && $nocheck == false){
// transform board
for($y=0;$y<6;$y++){
for($x=0;$x<6;$x++){
$cloneboard[$y][$x] = 0;
}
}
$cloneboard2 = $cloneboard;
for($y = 0; $y<6;$y++){
if($y == 0) $move = 2;
if($y == 1) $move = 1;
if($y == 2) $move = 0;
if($y == 3) $move = -1;
if($y == 4) $move = -2;
if($y == 5) $move = -3;
for($x=0;$x<6;$x++){
$cloneboard[$y][$x + $move] = $board[$y][$x];
}
}

$who_wins = match_check($cloneboard,true);
if($who_wins == 0){
for($y = 0; $y<6;$y++){
if($y == 0) $move = 2;
if($y == 1) $move = 1;
if($y == 2) $move = 0;
if($y == 3) $move = -1;
if($y == 4) $move = -2;
if($y == 5) $move = -3;
for($x=0;$x<6;$x++){
$cloneboard2[$y][$x - $move] = $board[$y][$x];
}

}
return match_check($cloneboard2,true);
}
return $who_wins;
}
if($winner and $line_score1 >= 5) return 1;
if($winner and $line_score2 >= 5) return 2;
return 0;

}
var_dump(match_check($board));
?>

This code might be a little confusing, however what it basically does is a matrix rotation of the grid one way and then the other.   This makes it so all the cross lines become straight lines.  You have to rotate one for one direction and one for the other.

Obviously there are only 8 cross positions that could win and the optimal solution would involve rotating only the inner grid.    There could also be some loops probably combined but over all it runs amazingly fast.

Perhaps there is an open source php pentago package in the future.

No Comments yet »

RSS feed for comments on this post. TrackBack URI

Leave a comment

You must be logged in to post a comment.