Gnieark 8 years ago
parent 7cb88a3a2a
commit a4a19b7440

@ -17,7 +17,90 @@ function score($board,$me,$colToPlay){
$newBoard[$y,$colToPlay] = $me;
//do I win?
$searchValue="";
for($i = 0 ; $i < 4; $i++){
$searchValue.=$me;
}
//horizontaly
$line="";
for ($i=0; $i < 7; $i++){
if ($newBoard[$y][$i] == ""){
$line.=" ";
}else{
$line.=$newBoard[$y][$i];
}
}
if(strpos($searchValue,$line) > -1){
return 42;
}
//verticaly
$line="";
for ($i=0; $i < 6; $i++){
if ($newBoard[$i][$colToPlay] == ""){
$line.=" ";
}else{
$line.=$newBoard[$i][$colToPlay];
}
}
if(strpos($searchValue,$line) > -1){
return 42;
}
//diagonal \
$line="";
$b = $y + $colToPlay;
if($b < 6){
$ix = 0;
$iy = $b;
}else{
$ix = $b - 5;
$iy = 5;
}
for($jx = $ix, $jy = $iy; ($jx < 7) && ($jy > -1); $jx++, $jy--){
if($newBoard[$jy][$jx] == ""){
$line.=" ";
}else{
$line.=$newBoard[$jy][$jx];
}
}
if(strpos($searchValue,$line) > -1){
return 42;
}
/*
//diagonal / affin function like y=x+b
b = parseInt(y - x);
if( b > -1){
//first point has x=0
kx = 0;
ky = b
}else{
//first point has y=0
ky = 0;
kx = -b;
}
var line="";
var lx , ly;
for (lx = kx , ly = ky ; (lx < 7) && (ly < 6) ; lx++ , ly++){
if( board[ly][lx] == ""){
line += " ";
}else{
line += board[ly][lx];
}
}
if (line.indexOf(searchValue) > -1){
wins(currentPlayer);
return;
}
*/

Loading…
Cancel
Save