From f86bab3ddbfabb74c78077cf4f63a91e06cd812f Mon Sep 17 00:00:00 2001 From: gnieark Date: Wed, 15 Jun 2016 00:45:35 +0200 Subject: [PATCH] 2 --- connectfour2.php | 210 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 210 insertions(+) create mode 100644 connectfour2.php diff --git a/connectfour2.php b/connectfour2.php new file mode 100644 index 0000000..6dcba5b --- /dev/null +++ b/connectfour2.php @@ -0,0 +1,210 @@ + "_" ) && ($params['board'][$y][$x] <> $params['you'] )){ + $opponent= $params['board'][$y][$x]; + } + + //tester si la case est jouable (s'il y a un support en dessous) + if (($params['board'][$y][$x] == "_" ) + AND (($y==0) OR ($params['board'][$y - 1][$x] <> "_")) + ){ + //la case est jouable, je la marque par un "+" + $params['board'][$y][$x] = "+"; + } + } + } + if((!isset($opponent)) && ($params['you'] == "X")){ + $opponent="O"; + }elseif(!isset($opponent)){ + $opponent="X"; + } + + //transformer la grille en lignes horizontales, verticales et diagonales + + + + //verticales + for($x = 0; $x <7; $x ++){ + $colStr=""; + for($y = 0; $y <6; $y ++){ + $colStr.= $params['board'][$y][$x]; + } + if(can_win($colStr,$params['you']) !== false){ + echo '{"play":'.$x.'}'; + die; + } + if (can_loose($colStr,$opponent) !== false){ + $colForNoLose = $x; + } + } + + //horizontales + for($y = 0; $y <6; $y ++){ + $lnStr=""; + for($x = 0; $x <7; $x ++){ + $lnStr.= $params['board'][$y][$x]; + } + if(can_win($lnStr,$params['you']) !== false){ + echo '{"play":'.can_win($lnStr,$params['you']).'}'; + die; + } + if (can_loose($lnStr,$opponent) !== false){ + $colForNoLose = can_loose($lnStr,$opponent); + } + + } + + + //tester seulement les diagonales >= 4 cases + + for ($k = 0; $k < 4; $k ++){ + + //diagonale / + $diagStr=""; + for($x=$k , $y=0; isset($params['board'][$y][$x]); $x++, $y++){ + $diagStr.=$params['board'][$y][$x]; + } + if(can_win($diagStr,$params['you']) !== false){ + echo '{"play":'.$x.'}'; + die; + } + if (can_loose($diagStr,$opponent) !== false){ + $colForNoLose = $x; + } + + //diagonale \ + $diagStr=""; + for($x=$k , $y=5; isset($params['board'][$y][$x]); $x++, $y--){ + $diagStr.=$params['board'][$y][$x]; + } + if(can_win($diagStr,$params['you']) !== false){ + echo '{"play":'.$x.'}'; + die; + } + if (can_loose($diagStr,$opponent) !== false){ + $colForNoLose = $x; + } + + + } + for ($k = 0; $k < 3; $k ++){ + //diagonale / + $diagStr=""; + for($x = 0, $y = $k ; isset($params['board'][$y][$x]); $x++, $y++){ + $diagStr.=$params['board'][$y][$x]; + } + if(can_win($diagStr,$params['you']) !== false){ + echo '{"play":'.$x.'}'; + die; + } + if (can_loose($diagStr,$opponent) !== false){ + $colForNoLose = $x; + } + } + for ($k = 3 ; $k < 6 ; $k++){ + + //diagonales \ + $diagStr=""; + for($x=0 , $y=$k isset($params['board'][$y][$x]); $x++, $y--){ + $diagStr.=$params['board'][$y][$x]; + } + if(can_win($diagStr,$params['you']) !== false){ + echo '{"play":'.$x.'}'; + die; + } + if (can_loose($diagStr,$opponent) !== false){ + $colForNoLose = $x; + } + + } + + //si j'arrive là, je ne gagne pas à ce tour + if(isset($colForNoLose)){ + echo '{"play":'.$colForNoLose.'}'; + die; + } + + //still there? random + + $colAvailable=array(); + //dont play on full colomns + for($i=0;$i<7;$i++){ + if(($grid[5][$i] == "+") OR ($grid[5][$i] == "_")){ + $colAvailable[]=$i; + } + } + + shuffle($colAvailable); + echo '{"play":'.$colAvailable[0].'}'; + + + //echo '{"play":"'.better_col($params['board'],$params['you'],$opponent,0).'"}'; + break; + default: + break; +}