From 7cb88a3a2a67c6cd722031342279b4286780d584 Mon Sep 17 00:00:00 2001 From: Gnieark Date: Mon, 13 Jun 2016 11:17:52 +0200 Subject: [PATCH] . --- connectfour.php | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/connectfour.php b/connectfour.php index a4a1b23..1f2af09 100644 --- a/connectfour.php +++ b/connectfour.php @@ -7,6 +7,39 @@ header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Ac * stupid IA for battle ship * choose by random a free column */ + +function score($board,$me,$colToPlay){ + + $newBoard = $board; + //add cell + for($y = 0; $board[$y][$colToPlay] <> ""; $y++){ + } + $newBoard[$y,$colToPlay] = $me; + + //do I win? + + + + + +} + +function better_col($board,$me){ + $betterScore= -1000; + $betterCol= -1; + for( $i = 0; $i < 7; $i++){ + if($board[5][$i] == ""){ + $sc = score($board,$me,$i) + if( score($board,$me,$i) > $betterScore){ + $betterScore = $sc; + $betterCol = $i; + } + } + } + + return $i; +} + $in=file_get_contents('php://input'); $params=json_decode($in, TRUE); switch($params['action']){ @@ -14,9 +47,7 @@ switch($params['action']){ echo '{"name":"Gnieark"}'; break; case "play-turn": - - $grid=$params['board']; - + echo '{"play":"'.better_col($params['board'],$params['you']).'"}'; break; default: break;