我需要编写一个简单的"五子棋"游戏。我已成功检测到了行胜利和列胜利,如下所示:
对于行:
$same=0;
for($i=1;$i<=$size;$i++) for($j=1;$j<=$size;$j++){ /wins in a row
if((@$_SESSION["pos"][$i][$j] == @$_SESSION["pos"][$i][$j+1]) && @$_SESSION["pos"][$i][$j]!=0 ) ++$same; else $same=0;
if($same==4){
if($_SESSION["pos"][$i][$j]==1) $winner="First"; //will be read from DB
if($_SESSION["pos"][$i][$j]==2) $winner="Second"; //will be read from DB
print $winner.' player WON!<br />';
}
}
对于列,情况相同,但是方向相反,所以很容易。现在我的问题是,如何检测对角线胜利?我尝试搜索了一下,但是没有找到任何我可以使用(或理解)的东西。我只能使用PHP。
What is what:
$same = same symbol counter
$_SESSION["pos"][$i][$j] = the 2D array of coordinates of the board (starting from 1,1 not 0,0)
indexes are the coordinates, values can be 0 (empty space) or 1 (symbol1) or 2 (symbol2)
$size = size of the board (always N x N)
您不需要给我一个完整的代码,只需给我一些东西来理解如何实现它。