Detect Game Win State in AS3
I'm trying to come up with a clean function that will perform a check on a 2D Array to verify if a win has occurred in a ConnectFour game. I'm trying to work through the logic but my brain is swiss cheese right now. Here's what I have for checking for a horizontal win. It seems to me like there should be a way to streamline this block to work in all possible directions.
private function checkForHorizontalWin(column:uint, row:uint, grid:Array):Boolean
{
var player:uint = grid[column][row];
var counter:uint = 1;
for(var i:uint = Math.min(0, uint(column-1)); i>=0; i--)
{
if(!grid[i][row] || grid[i][row] != player)
{
break;
}
counter++;
}
for(var j:uint = column+1; j<_columns; j++)
{
if(!grid[j][row] || grid[j]开发者_运维百科[row] != player)
{
break;
}
counter++;
}
if(counter >=4)
{
return true;
}
else
{
return false;
}
}
I actually ended up doing something pretty simple...Converted the grid array to a string then look for 4 in a row...Only tricky part was getting the correct array for the diagonal possibilities but I got that worked out too.
精彩评论