From 815f99c0cc31333e8767d49eec6e739d991e7e4e Mon Sep 17 00:00:00 2001 From: Gnieark Date: Sat, 19 Dec 2015 12:51:08 +0100 Subject: [PATCH 1/7] hit sunk win --- src/arenas/Battleship/act.php | 40 +++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/src/arenas/Battleship/act.php b/src/arenas/Battleship/act.php index d0c7141..d9413ea 100644 --- a/src/arenas/Battleship/act.php +++ b/src/arenas/Battleship/act.php @@ -255,11 +255,42 @@ 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]); + 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 +300,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, From 5093978301d5d4091d49802c5180fbadfb92ca51 Mon Sep 17 00:00:00 2001 From: Gnieark Date: Sat, 19 Dec 2015 12:54:55 +0100 Subject: [PATCH 2/7] fix --- src/arenas/Battleship/act.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/arenas/Battleship/act.php b/src/arenas/Battleship/act.php index d9413ea..2a75436 100644 --- a/src/arenas/Battleship/act.php +++ b/src/arenas/Battleship/act.php @@ -282,7 +282,7 @@ switch ($_POST['act']){ $result="hit and sunk"; //remove the ship unset($_SESSION['ships'][$opponent][$shipIndex]); - var_dump($_SESSION['ships'][$opponent]); + //var_dump($_SESSION['ships'][$opponent]); //win the game? if(count($_SESSION['ships'][$opponent]) == 0){ $result="hit sunk and win"; From 88d2106aa05f198846a7c59ed5d07b0936a431bd Mon Sep 17 00:00:00 2001 From: Gnieark Date: Sat, 19 Dec 2015 13:47:02 +0100 Subject: [PATCH 3/7] fix --- src/arenas/Battleship/act.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/arenas/Battleship/act.php b/src/arenas/Battleship/act.php index 2a75436..1a7fb03 100644 --- a/src/arenas/Battleship/act.php +++ b/src/arenas/Battleship/act.php @@ -282,6 +282,7 @@ switch ($_POST['act']){ $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){ From eed0b2ff06eba19ed5dcf845ba8f48cbce033cc3 Mon Sep 17 00:00:00 2001 From: Gnieark Date: Sat, 19 Dec 2015 13:57:39 +0100 Subject: [PATCH 4/7] verif arena --- html/StupidIABattleship.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/html/StupidIABattleship.php b/html/StupidIABattleship.php index 85d17d4..f9c5c4d 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)); + + // 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{ From db21d2f0c1769d76871f68304a75fdaa0209323f Mon Sep 17 00:00:00 2001 From: Gnieark Date: Sat, 19 Dec 2015 13:59:39 +0100 Subject: [PATCH 5/7] verif arena --- html/StupidIABattleship.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/html/StupidIABattleship.php b/html/StupidIABattleship.php index f9c5c4d..c28ee9d 100644 --- a/html/StupidIABattleship.php +++ b/html/StupidIABattleship.php @@ -208,7 +208,7 @@ switch($_POST['act']){ break; case "fight": //for debog arena - file_put_contents(__DIR__."log.txt",print_r($_POST)); + file_put_contents(__DIR__."log.txt",var_export($_POST)); // if((is_int(settype($_POST['width'],"integer"))) && (is_int(settype($_POST['height'],"integer")))){ From d1b14a1eeb5b1a78b1800a48b67a5316240c6f0d Mon Sep 17 00:00:00 2001 From: Gnieark Date: Sat, 19 Dec 2015 14:02:26 +0100 Subject: [PATCH 6/7] verif arena --- html/StupidIABattleship.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/html/StupidIABattleship.php b/html/StupidIABattleship.php index c28ee9d..cee936d 100644 --- a/html/StupidIABattleship.php +++ b/html/StupidIABattleship.php @@ -208,7 +208,7 @@ switch($_POST['act']){ break; case "fight": //for debog arena - file_put_contents(__DIR__."log.txt",var_export($_POST)); + file_put_contents(__DIR__."log.txt",print_r($_POST,true)); // if((is_int(settype($_POST['width'],"integer"))) && (is_int(settype($_POST['height'],"integer")))){ From 79ab9d0b71c8893e705d96b1769eeae125facdfb Mon Sep 17 00:00:00 2001 From: Gnieark Date: Sat, 19 Dec 2015 14:04:07 +0100 Subject: [PATCH 7/7] verif arena --- html/StupidIABattleship.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/html/StupidIABattleship.php b/html/StupidIABattleship.php index cee936d..8452d22 100644 --- a/html/StupidIABattleship.php +++ b/html/StupidIABattleship.php @@ -208,7 +208,7 @@ switch($_POST['act']){ break; case "fight": //for debog arena - file_put_contents(__DIR__."log.txt",print_r($_POST,true)); + file_put_contents(__DIR__."/log.txt",print_r($_POST,true)); // if((is_int(settype($_POST['width'],"integer"))) && (is_int(settype($_POST['height'],"integer")))){