diff --git a/html/StupidIABattleship.php b/html/StupidIABattleship.php index 85d17d4..8452d22 100644 --- a/html/StupidIABattleship.php +++ b/html/StupidIABattleship.php @@ -207,6 +207,10 @@ switch($_POST['act']){ echo json_encode($shipsCoords); break; case "fight": + //for debog arena + file_put_contents(__DIR__."/log.txt",print_r($_POST,true)); + + // if((is_int(settype($_POST['width'],"integer"))) && (is_int(settype($_POST['height'],"integer")))){ echo rand(0,$_POST['width'] -1).",".rand(0,$_POST['height'] -1); // }else{ diff --git a/src/arenas/Battleship/act.php b/src/arenas/Battleship/act.php index d0c7141..1a7fb03 100644 --- a/src/arenas/Battleship/act.php +++ b/src/arenas/Battleship/act.php @@ -255,11 +255,43 @@ switch ($_POST['act']){ die; } - //do this shot hit a boat + //put previous strikes in a simple array; + $previousStrikes=array(); + foreach( $_SESSION['strikes'][$currentPlayer] as $strikes){ + $previousStrikes[]=$strikes['target']; + } + + //do this strike hit a boat? + $continue=1; $result=''; - foreach($_SESSION['ships'][$opponent] as $ennemyBoat){ + + for( $shipIndex = 0; $shipIndex < count( $_SESSION['ships'][$opponent]); $shipIndex ++){ + $ennemyBoat = $_SESSION['ships'][$opponent][$shipIndex]; + if(in_array($x.",".$y, $ennemyBoat)){ $result='hit'; + //sunk? + $sunk=true; + foreach($ennemyBoat as $boatCase){ + if((!in_array($boatCase,$previousStrikes)) && ($boatCase <> $x.",".$y)) { + $sunk=false; + break; + } + } + if($sunk){ + $result="hit and sunk"; + //remove the ship + unset($_SESSION['ships'][$opponent][$shipIndex]); + $_SESSION['ships'][$opponent] = array_values($_SESSION['ships'][$opponent]); + //var_dump($_SESSION['ships'][$opponent]); + //win the game? + if(count($_SESSION['ships'][$opponent]) == 0){ + $result="hit sunk and win"; + $continue=0; + save_battle('Battleship',$_SESSION['bot1']['name'],$_SESSION['bot2']['name'],$currentPlayer); + } + } + break; } } @@ -269,11 +301,6 @@ switch ($_POST['act']){ 'target' => $x.",".$y, 'result' => $result ); - if(count( $_SESSION['strikes'][$currentPlayer]) < 10){ - $continue=1; - }else{ - $continue=0; - } echo json_encode(array( 'opponent'=> $opponent, 'target' => $x.",".$y,