From a4a19b7440ec5d58211f81ee47539bcb6c1d9d4d Mon Sep 17 00:00:00 2001 From: Gnieark Date: Mon, 13 Jun 2016 12:13:33 +0200 Subject: [PATCH] . --- connectfour.php | 83 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/connectfour.php b/connectfour.php index 1f2af09..b9bc18c 100644 --- a/connectfour.php +++ b/connectfour.php @@ -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; + } + + */