From 656c8fe29173bca6c436bfcb77d1a092cae8f66f Mon Sep 17 00:00:00 2001 From: Gnieark Date: Thu, 17 Dec 2015 18:45:57 +0100 Subject: [PATCH 01/17] re-open stupid IA for debug --- html/StupidIABattleship.php | 40 +++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/html/StupidIABattleship.php b/html/StupidIABattleship.php index 66b75e1..39d2f65 100644 --- a/html/StupidIABattleship.php +++ b/html/StupidIABattleship.php @@ -1,5 +1,28 @@ 0; $i--){ + $var='nbShipsSize'.$i; + if($$var > 0){ + $longestShip=$$var; + break; + } + } + if( (!isset($longestShip)) + OR(($longestShip > $gridWidth) && ($longestShip > $gridHeight)) + ){ + return false; + } + return true; +} + + function place_ship_on_map($x1,$y1,$x2,$y2,$map){ if ((($x1 <> $x2) && ($y1 <> $y2)) OR (!isset($map[$y1][$x1])) @@ -68,13 +91,20 @@ switch($_POST['act']){ if(!preg_match('/^[0-9]+-(1|2)$/',$match_id)){ echo "parametre incorrect"; die; } + + if(!is_it_possible_to_place_ships_on_grid($width,$height,$ship1,$ship2,$ship3,$ship4,$ship5,$ship6)){ + echo "I don't want play this game"; + die; + } + + $map=array(); //construire une grille for($i=0; $i < $width; $i++){ for($j=0; $j < $height; $j++){ $map[$j][$i]=0; } - } + } $shipsCoords=array(); @@ -132,9 +162,7 @@ switch($_POST['act']){ break; } } - - - + $directions=array(); if($top){ $directions[]='top'; @@ -173,7 +201,7 @@ switch($_POST['act']){ } } - print_r($map); + //print_r($map); echo json_encode($shipsCoords); break; default: From 786ed15e1594188eb7b75f5bf4bf361d9384d875 Mon Sep 17 00:00:00 2001 From: Gnieark Date: Thu, 17 Dec 2015 18:50:13 +0100 Subject: [PATCH 02/17] fix php parse error --- 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 6a62660..964463d 100644 --- a/src/arenas/Battleship/act.php +++ b/src/arenas/Battleship/act.php @@ -57,7 +57,7 @@ switch ($_POST['act']){ error (500,"missing parameter 2"); } - if(!is_it_possible_to_place_ships_on_grid($postValues['gridWidth'],$postValues['gridHeight'],$postValues['nbShip1'],$posValues['nbship2'],$postValues['nbship3'],$postValues['nbship4'],$postValues['nbship5'],$postvalues['nbship6'])){ + if(!is_it_possible_to_place_ships_on_grid($postValues['gridWidth'],$postValues['gridHeight'],$postValues['nbShip1'],$postValues['nbship2'],$postValues['nbship3'],$postValues['nbship4'],$postValues['nbship5'],$postvalues['nbship6'])){ error (404,"grid is too little for these sips"); } From 7903c64f1948da17b9d2465251a9d83091255c63 Mon Sep 17 00:00:00 2001 From: Gnieark Date: Thu, 17 Dec 2015 18:51:06 +0100 Subject: [PATCH 03/17] fix php parse error --- 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 964463d..1b43585 100644 --- a/src/arenas/Battleship/act.php +++ b/src/arenas/Battleship/act.php @@ -57,7 +57,7 @@ switch ($_POST['act']){ error (500,"missing parameter 2"); } - if(!is_it_possible_to_place_ships_on_grid($postValues['gridWidth'],$postValues['gridHeight'],$postValues['nbShip1'],$postValues['nbship2'],$postValues['nbship3'],$postValues['nbship4'],$postValues['nbship5'],$postvalues['nbship6'])){ + if(!is_it_possible_to_place_ships_on_grid($postValues['gridWidth'],$postValues['gridHeight'],$postValues['nbShip1'],$postValues['nbShip2'],$postValues['nbShip3'],$postValues['nbShip4'],$postValues['nbShip5'],$postvalues['nbShip6'])){ error (404,"grid is too little for these sips"); } From 7bfa057499df56dc6ea5a7288b5bcf53066314c2 Mon Sep 17 00:00:00 2001 From: Gnieark Date: Thu, 17 Dec 2015 18:51:35 +0100 Subject: [PATCH 04/17] fix php parse error --- 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 1b43585..bb6f27d 100644 --- a/src/arenas/Battleship/act.php +++ b/src/arenas/Battleship/act.php @@ -57,7 +57,7 @@ switch ($_POST['act']){ error (500,"missing parameter 2"); } - if(!is_it_possible_to_place_ships_on_grid($postValues['gridWidth'],$postValues['gridHeight'],$postValues['nbShip1'],$postValues['nbShip2'],$postValues['nbShip3'],$postValues['nbShip4'],$postValues['nbShip5'],$postvalues['nbShip6'])){ + if(!is_it_possible_to_place_ships_on_grid($postValues['gridWidth'],$postValues['gridHeight'],$postValues['nbShip1'],$postValues['nbShip2'],$postValues['nbShip3'],$postValues['nbShip4'],$postValues['nbShip5'],$postValues['nbShip6'])){ error (404,"grid is too little for these sips"); } From 55b4e7bb8572d6cf752e7905c9df61caf95152b2 Mon Sep 17 00:00:00 2001 From: Gnieark Date: Thu, 17 Dec 2015 18:56:40 +0100 Subject: [PATCH 05/17] fix php parse error --- html/StupidIABattleship.php | 2 +- src/arenas/Battleship/functions.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/html/StupidIABattleship.php b/html/StupidIABattleship.php index 39d2f65..bb1a55c 100644 --- a/html/StupidIABattleship.php +++ b/html/StupidIABattleship.php @@ -3,7 +3,7 @@ function is_it_possible_to_place_ships_on_grid($gridWidth,$gridHeight,$nbShipsSi //return false or true //not a perfect solution $shipsArea=$nbShipsSize1 + 2 * $nbShipsSize2 + 3 * $nbShipsSize3 + 4 * $nbShipsSize4 + 5 * $nbShipsSize5 + 6 * $nbShipsSize6; - if( $shipsArea < $gridHeight * $gridwidth / 2){ + if( $shipsArea < $gridHeight * $gridWidth / 2){ return false; } //longest ship diff --git a/src/arenas/Battleship/functions.php b/src/arenas/Battleship/functions.php index 9079a2e..f41cb26 100644 --- a/src/arenas/Battleship/functions.php +++ b/src/arenas/Battleship/functions.php @@ -3,7 +3,7 @@ function is_it_possible_to_place_ships_on_grid($gridWidth,$gridHeight,$nbShipsSi //return false or true //not a perfect solution $shipsArea=$nbShipsSize1 + 2 * $nbShipsSize2 + 3 * $nbShipsSize3 + 4 * $nbShipsSize4 + 5 * $nbShipsSize5 + 6 * $nbShipsSize6; - if( $shipsArea < $gridHeight * $gridwidth / 2){ + if( $shipsArea < $gridHeight * $gridWidth / 2){ return false; } //longest ship From 6d2df49dbf6f6d96f05e35faa68f0a36bd0ff9b5 Mon Sep 17 00:00:00 2001 From: Gnieark Date: Thu, 17 Dec 2015 18:58:48 +0100 Subject: [PATCH 06/17] fix php int float comparaison --- src/arenas/Battleship/act.php | 2 +- src/arenas/Battleship/functions.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/arenas/Battleship/act.php b/src/arenas/Battleship/act.php index bb6f27d..9ed44b2 100644 --- a/src/arenas/Battleship/act.php +++ b/src/arenas/Battleship/act.php @@ -58,7 +58,7 @@ switch ($_POST['act']){ } if(!is_it_possible_to_place_ships_on_grid($postValues['gridWidth'],$postValues['gridHeight'],$postValues['nbShip1'],$postValues['nbShip2'],$postValues['nbShip3'],$postValues['nbShip4'],$postValues['nbShip5'],$postValues['nbShip6'])){ - error (404,"grid is too little for these sips"); + error (404,"grid is too little for these ships"); } //vars checked, lets init the initGame diff --git a/src/arenas/Battleship/functions.php b/src/arenas/Battleship/functions.php index f41cb26..1500630 100644 --- a/src/arenas/Battleship/functions.php +++ b/src/arenas/Battleship/functions.php @@ -3,7 +3,7 @@ function is_it_possible_to_place_ships_on_grid($gridWidth,$gridHeight,$nbShipsSi //return false or true //not a perfect solution $shipsArea=$nbShipsSize1 + 2 * $nbShipsSize2 + 3 * $nbShipsSize3 + 4 * $nbShipsSize4 + 5 * $nbShipsSize5 + 6 * $nbShipsSize6; - if( $shipsArea < $gridHeight * $gridWidth / 2){ + if( $shipsArea < intval($gridHeight * $gridWidth / 2)){ return false; } //longest ship From c23476f9bef5fe771f3b57b6c50858be03ddc529 Mon Sep 17 00:00:00 2001 From: Gnieark Date: Thu, 17 Dec 2015 19:03:10 +0100 Subject: [PATCH 07/17] fix php int float comparaison --- src/arenas/Battleship/functions.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/arenas/Battleship/functions.php b/src/arenas/Battleship/functions.php index 1500630..d3f6bc0 100644 --- a/src/arenas/Battleship/functions.php +++ b/src/arenas/Battleship/functions.php @@ -4,6 +4,7 @@ function is_it_possible_to_place_ships_on_grid($gridWidth,$gridHeight,$nbShipsSi //not a perfect solution $shipsArea=$nbShipsSize1 + 2 * $nbShipsSize2 + 3 * $nbShipsSize3 + 4 * $nbShipsSize4 + 5 * $nbShipsSize5 + 6 * $nbShipsSize6; if( $shipsArea < intval($gridHeight * $gridWidth / 2)){ + echo "there"; return false; } //longest ship From 36eae642a2d417fc0b8fc343dd00c7c93ba6eced Mon Sep 17 00:00:00 2001 From: Gnieark Date: Thu, 17 Dec 2015 19:04:51 +0100 Subject: [PATCH 08/17] fix php int float comparaison --- src/arenas/Battleship/functions.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/arenas/Battleship/functions.php b/src/arenas/Battleship/functions.php index d3f6bc0..c8b244e 100644 --- a/src/arenas/Battleship/functions.php +++ b/src/arenas/Battleship/functions.php @@ -2,9 +2,8 @@ function is_it_possible_to_place_ships_on_grid($gridWidth,$gridHeight,$nbShipsSize1,$nbShipsSize2,$nbShipsSize3,$nbShipsSize4,$nbShipsSize5,$nbShipsSize6){ //return false or true //not a perfect solution - $shipsArea=$nbShipsSize1 + 2 * $nbShipsSize2 + 3 * $nbShipsSize3 + 4 * $nbShipsSize4 + 5 * $nbShipsSize5 + 6 * $nbShipsSize6; + $shipsArea=$nbShipsSize1 + (2 * $nbShipsSize2) + (3 * $nbShipsSize3) + (4 * $nbShipsSize4) + (5 * $nbShipsSize5) + (6 * $nbShipsSize6); if( $shipsArea < intval($gridHeight * $gridWidth / 2)){ - echo "there"; return false; } //longest ship From e0044e5146f63de35a6ec5f739055466cc423bf0 Mon Sep 17 00:00:00 2001 From: Gnieark Date: Thu, 17 Dec 2015 19:09:23 +0100 Subject: [PATCH 09/17] for debog --- src/arenas/Battleship/functions.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/arenas/Battleship/functions.php b/src/arenas/Battleship/functions.php index c8b244e..5c9caf5 100644 --- a/src/arenas/Battleship/functions.php +++ b/src/arenas/Battleship/functions.php @@ -4,6 +4,7 @@ function is_it_possible_to_place_ships_on_grid($gridWidth,$gridHeight,$nbShipsSi //not a perfect solution $shipsArea=$nbShipsSize1 + (2 * $nbShipsSize2) + (3 * $nbShipsSize3) + (4 * $nbShipsSize4) + (5 * $nbShipsSize5) + (6 * $nbShipsSize6); if( $shipsArea < intval($gridHeight * $gridWidth / 2)){ + echo $shipArea." ". intval($gridHeight * $gridWidth / 2); return false; } //longest ship From a78e2ebf2b8439fc1d6b18aef93ba2451aa0a68b Mon Sep 17 00:00:00 2001 From: Gnieark Date: Thu, 17 Dec 2015 19:10:00 +0100 Subject: [PATCH 10/17] for debog the debog --- src/arenas/Battleship/functions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/arenas/Battleship/functions.php b/src/arenas/Battleship/functions.php index 5c9caf5..91afa98 100644 --- a/src/arenas/Battleship/functions.php +++ b/src/arenas/Battleship/functions.php @@ -4,7 +4,7 @@ function is_it_possible_to_place_ships_on_grid($gridWidth,$gridHeight,$nbShipsSi //not a perfect solution $shipsArea=$nbShipsSize1 + (2 * $nbShipsSize2) + (3 * $nbShipsSize3) + (4 * $nbShipsSize4) + (5 * $nbShipsSize5) + (6 * $nbShipsSize6); if( $shipsArea < intval($gridHeight * $gridWidth / 2)){ - echo $shipArea." ". intval($gridHeight * $gridWidth / 2); + echo $shipsArea." ". intval($gridHeight * $gridWidth / 2); return false; } //longest ship From 999ccd9948394f1ef2e253eed400e34e20cc47c0 Mon Sep 17 00:00:00 2001 From: Gnieark Date: Thu, 17 Dec 2015 19:11:07 +0100 Subject: [PATCH 11/17] erf --- src/arenas/Battleship/functions.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/arenas/Battleship/functions.php b/src/arenas/Battleship/functions.php index 91afa98..ae7677c 100644 --- a/src/arenas/Battleship/functions.php +++ b/src/arenas/Battleship/functions.php @@ -3,8 +3,7 @@ function is_it_possible_to_place_ships_on_grid($gridWidth,$gridHeight,$nbShipsSi //return false or true //not a perfect solution $shipsArea=$nbShipsSize1 + (2 * $nbShipsSize2) + (3 * $nbShipsSize3) + (4 * $nbShipsSize4) + (5 * $nbShipsSize5) + (6 * $nbShipsSize6); - if( $shipsArea < intval($gridHeight * $gridWidth / 2)){ - echo $shipsArea." ". intval($gridHeight * $gridWidth / 2); + if( $shipsArea > intval($gridHeight * $gridWidth / 2)){ return false; } //longest ship From aaddbf7d252fe595501a512c0d9224c2c22d9bf4 Mon Sep 17 00:00:00 2001 From: Gnieark Date: Thu, 17 Dec 2015 19:11:36 +0100 Subject: [PATCH 12/17] erf --- html/StupidIABattleship.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/html/StupidIABattleship.php b/html/StupidIABattleship.php index bb1a55c..45f3640 100644 --- a/html/StupidIABattleship.php +++ b/html/StupidIABattleship.php @@ -3,7 +3,7 @@ function is_it_possible_to_place_ships_on_grid($gridWidth,$gridHeight,$nbShipsSi //return false or true //not a perfect solution $shipsArea=$nbShipsSize1 + 2 * $nbShipsSize2 + 3 * $nbShipsSize3 + 4 * $nbShipsSize4 + 5 * $nbShipsSize5 + 6 * $nbShipsSize6; - if( $shipsArea < $gridHeight * $gridWidth / 2){ + if( $shipsArea > $gridHeight * $gridWidth / 2){ return false; } //longest ship From efb974deeef602372fdea2e58de279ce848b074b Mon Sep 17 00:00:00 2001 From: Gnieark Date: Thu, 17 Dec 2015 22:00:56 +0100 Subject: [PATCH 13/17] rewrite stupid IA --- html/StupidIABattleship.php | 154 ++++++++++++++++++------------------ 1 file changed, 78 insertions(+), 76 deletions(-) diff --git a/html/StupidIABattleship.php b/html/StupidIABattleship.php index 45f3640..fdbba96 100644 --- a/html/StupidIABattleship.php +++ b/html/StupidIABattleship.php @@ -97,7 +97,7 @@ switch($_POST['act']){ die; } - + a: $map=array(); //construire une grille for($i=0; $i < $width; $i++){ @@ -114,93 +114,95 @@ switch($_POST['act']){ $dynVar='ship'.$shipWidth; $shipCount=$$dynVar; // #trollface for( $sh = 0; $sh < $shipCount; $sh++){ //loop for all boats witch size is $shipWidth - $directions=array(); - while( count($directions) == 0){ - do{ - $xtest=rand(0,$width -1); - $ytest=rand(0,$height -1); - }while($map[$ytest][$xtest] == 1); - - //Y a t'il la place pour le bateau vers le haut? - - //haut - $top=true; - for($i = $ytest; $i >= $ytest - $shipWidth + 1; $i--){ - if ((!isset($map[$i][$xtest])) - OR ($map[$i][$xtest] == 1)){ - $top=false; - break; - } - } - - //vers le bas - $bottom=true; - for($i = $ytest; $i < $ytest + $shipWidth -1; $i++){ - if ((!isset($map[$i][$xtest])) - OR ($map[$i][$xtest] == 1)){ - $bottom=false; - break; - } - } - - //droite - $right=true; - for($i = $xtest; $i < $xtest + $shipWidth -1; $i++){ - if((!isset($map[$ytest][$i])) - OR($map[$ytest][$i] == 1)){ - $right= false; - break; - } - } - - //gauche - $left=true; - for($i = $xtest; $i >= $xtest - $shipWidth + 1; $i--){ - if((!isset($map[$ytest][$i])) - OR($map[$ytest][$i] == 1)){ - $left= false; - break; - } - } - - $directions=array(); - if($top){ - $directions[]='top'; - } - if($bottom){ - $directions[]='bottom'; - } - if($left){ - $directions[]='left'; - } - if($right){ - $directions[]='right'; - } - } - - shuffle($directions); - switch($directions[0]){ + //find free cases + $freeCases=array(); + for($y=0; $y < $height; $y++){ + for($x=0; $x < $width; $x++){ + if($map[$y][$x] == 0){ + $directions=array(); + //test top + $top=true; + for($i = $y; $i > $y - $shipWidth; $i--){ + if((!isset($map[$i][$x])) OR ($map[$i][$x]==1)){ + $top=false; + $break; + } + } + if($top){ + $directions[]='top'; + } + //test Bottom + $bottom=true; + for($i = $y; $i < $y + $shipWidth; $i++){ + if(((!isset($map[$i][$x])) OR $map[$i][$x]==1)){ + $bottom=false; + $break; + } + } + if($bottom){ + $directions[]='bottom'; + } + //test left + $left=true; + for($i = $x; $i > $x - $shipWidth; $i--){ + if((!isset($map[$y][$i])) OR ($map[$y][$i]==1)){ + $left=false; + $break; + } + } + if($left){ + $directions[]='left'; + } + //test right + $right=true; + for($i = $x; $i < $x + $shipWidth; $i++){ + if((!isset($map[$y][$i])) OR ($map[$y][$i]==1)){ + $right=false; + $break; + } + } + if($right){ + $directions[]='right'; + } + + if(count($directions)>0){ + $freeCases[]=array($x,$y,$directions); + } + + } + } + } + + if(count($freeCases) == 0){ + //can't place the ship + goto a; //#facepalm + } + shuffle($freeCases); //choose start case for this ship + shuffle($freeCases[0][2]); //choose random direction + $x=$freeCases[0][0]; + $y=$freeCases[0][1]; + switch($freeCases[0][2][0]){ case 'top': - $shipsCoords[]=$xtest.",".$ytest."-".$xtest.",".($ytest - $shipWidth + 1); - $map= place_ship_on_map($xtest,$ytest,$xtest,$ytest - $shipWidth + 1,$map); + $shipsCoords[]=$x.",".$y."-".$x.",".($y - $shipWidth + 1); + $map= place_ship_on_map($x,$y,$x,$y - $shipWidth + 1,$map); break; case 'bottom': - $shipsCoords[]=$xtest.",".$ytest."-".$xtest.",".($ytest + $shipWidth - 1); - $map= place_ship_on_map($xtest,$ytest,$xtest,$ytest + $shipWidth -1 ,$map); + $shipsCoords[]=$x.",".$y."-".$x.",".($y + $shipWidth - 1); + $map= place_ship_on_map($x,$y,$x,$y + $shipWidth -1 ,$map); break; case 'left': - $shipsCoords[]=$xtest.",".$ytest."-".($xtest - $shipWidth + 1).",".$ytest; - $map= place_ship_on_map($xtest,$ytest,$xtest - $shipWidth + 1 ,$ytest,$map); + $shipsCoords[]=$x.",".$y."-".($x - $shipWidth + 1).",".$y; + $map= place_ship_on_map($x,$y,$x - $shipWidth + 1 ,$y,$map); break; case 'right': - $shipsCoords[]=$xtest.",".$ytest."-".($xtest + $shipWidth - 1 ).",".$ytest; - $map= place_ship_on_map($xtest,$ytest,$xtest + $shipWidth -1 ,$ytest,$map); + $shipsCoords[]=$x.",".$y."-".($x + $shipWidth - 1 ).",".$y; + $map= place_ship_on_map($x,$y,$x + $shipWidth -1 ,$y,$map); break; } - } } + //print_r($map); echo json_encode($shipsCoords); break; From 5cf7dd261589045cf82e3c8edc2298f14e3ed484 Mon Sep 17 00:00:00 2001 From: gnieark Date: Thu, 17 Dec 2015 22:03:19 +0100 Subject: [PATCH 14/17] place ships function --- src/arenas/Battleship/act.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/arenas/Battleship/act.php b/src/arenas/Battleship/act.php index 9ed44b2..704ef52 100644 --- a/src/arenas/Battleship/act.php +++ b/src/arenas/Battleship/act.php @@ -119,9 +119,9 @@ switch ($_POST['act']){ list($xStart,$yStart)=explode(",",$startCoord); list($xEnd,$yEnd)=explode(",",$endCoord); if($xStart == $xEnd){ - $long=abs($yStart - $yEnd +1); + $long=abs($yStart - $yEnd) +1; }else{ - $long=abs($xStart - $xEnd +1); + $long=abs($xStart - $xEnd) +1; } $nbBoatsIwant[$long]-=1; $grid[$player]=place_ship_on_map($xStart,$yStart,$xEnd,$yEnd,$grid[$player]); From c4fa2974cface8fb8a52ce03090c068c1009a438 Mon Sep 17 00:00:00 2001 From: gnieark Date: Thu, 17 Dec 2015 22:27:17 +0100 Subject: [PATCH 15/17] js reduce try catch --- src/arenas/Battleship/act.php | 6 ++--- src/arenas/Battleship/js.js | 48 ++++++++++++++++++----------------- 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/src/arenas/Battleship/act.php b/src/arenas/Battleship/act.php index 704ef52..49a266b 100644 --- a/src/arenas/Battleship/act.php +++ b/src/arenas/Battleship/act.php @@ -126,7 +126,7 @@ switch ($_POST['act']){ $nbBoatsIwant[$long]-=1; $grid[$player]=place_ship_on_map($xStart,$yStart,$xEnd,$yEnd,$grid[$player]); if(!$grid[$player]){ - echo $currentBot['name']." n'a pas placé correctement ses bateaux. Certains se chevauchent. Il perd
".$anwserPlayer." ".$xStart.$yStart.$xEnd.$yEnd."
"; + echo $currentBot['name']." n'a pas placé correctement ses bateaux. Certains se chevauchent. Il perd."; if($player==1){ save_battle('Battleship',$bot1['name'],$bot2['name'],2); }else{ @@ -137,7 +137,7 @@ switch ($_POST['act']){ } foreach($nbBoatsIwant as $nb){ if($nb <> 0){ - echo $currentBot['name']." n'a pas placé correctement ses bateaux. Il perd. sa réponse:
".$anwserPlayer."
"; + echo $currentBot['name']." n'a pas placé le bon nombre de bateaux. Il perd."; if($player==1){ save_battle('Battleship',$bot1['name'],$bot2['name'],2); }else{ @@ -148,7 +148,7 @@ switch ($_POST['act']){ } } - $_SESSION['grids']=$grid; + //$_SESSION['grids']=$grid; echo json_encode($grid); die; die; diff --git a/src/arenas/Battleship/js.js b/src/arenas/Battleship/js.js index dfaa90a..2c41adf 100644 --- a/src/arenas/Battleship/js.js +++ b/src/arenas/Battleship/js.js @@ -82,30 +82,32 @@ function battleship(bot1,bot2,gridWidth,gridHeight,nbShip1,nbShip2,nbShip3,nbShi var xhr = Ajx(); xhr.onreadystatechange = function(){if(xhr.readyState == 4){ - if(xhr.status == 200) { - //debug - //alert(xhr.responseText); - try{ - var grids = JSON.parse(xhr.responseText); - for( var player=1; player <= 2 ; player ++){ - var p=createElem("p"); - p.innerHTML='Reponse joueurs:' + xhr.responseText; - document.getElementById('logs').appendChild(p); + if(xhr.status == 200) { + //debug + //alert(xhr.responseText); + try{ + var grids = JSON.parse(xhr.responseText); + catch(e){ + document.getElementById('logs').innerHTML = xhr.responseText; + return; + } + + for( var player=1; player <= 2 ; player ++){ + var p=createElem("p"); + p.innerHTML='Reponse joueurs:' + xhr.responseText; + document.getElementById('logs').appendChild(p); - for (var y=0; y < grids[player].length ; y++){ - for (var x=0; x < grids[player][y].length ; x++){ - if (grids[player][y][x] == 1){ - document.getElementById( 'bot' + player + '-' + y + '-' + x).className="shipOn"; - } - } - } - } - - } - catch(e){ - document.getElementById('logs').innerHTML = xhr.responseText; - - } + for (var y=0; y < grids[player].length ; y++){ + for (var x=0; x < grids[player][y].length ; x++){ + if (grids[player][y][x] == 1){ + document.getElementById( 'bot' + player + '-' + y + '-' + x).className="shipOn"; + } + } + } + } + + } + } }}; xhr.open("POST", '/Battleship', true); From 828765d840415eddf199f7398111520dc10feae7 Mon Sep 17 00:00:00 2001 From: gnieark Date: Thu, 17 Dec 2015 22:28:51 +0100 Subject: [PATCH 16/17] js reduce try catch --- src/arenas/Battleship/js.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/arenas/Battleship/js.js b/src/arenas/Battleship/js.js index 2c41adf..4b27839 100644 --- a/src/arenas/Battleship/js.js +++ b/src/arenas/Battleship/js.js @@ -87,7 +87,7 @@ function battleship(bot1,bot2,gridWidth,gridHeight,nbShip1,nbShip2,nbShip3,nbShi //alert(xhr.responseText); try{ var grids = JSON.parse(xhr.responseText); - catch(e){ + }catch(e){ document.getElementById('logs').innerHTML = xhr.responseText; return; } @@ -108,7 +108,6 @@ function battleship(bot1,bot2,gridWidth,gridHeight,nbShip1,nbShip2,nbShip3,nbShi } - } }}; xhr.open("POST", '/Battleship', true); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); From 99bb80c3e23cdcf1338c86d0ce49f5263582ebe4 Mon Sep 17 00:00:00 2001 From: gnieark Date: Thu, 17 Dec 2015 22:31:08 +0100 Subject: [PATCH 17/17] log --- src/arenas/Battleship/js.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/arenas/Battleship/js.js b/src/arenas/Battleship/js.js index 4b27839..fd7d108 100644 --- a/src/arenas/Battleship/js.js +++ b/src/arenas/Battleship/js.js @@ -93,10 +93,6 @@ function battleship(bot1,bot2,gridWidth,gridHeight,nbShip1,nbShip2,nbShip3,nbShi } for( var player=1; player <= 2 ; player ++){ - var p=createElem("p"); - p.innerHTML='Reponse joueurs:' + xhr.responseText; - document.getElementById('logs').appendChild(p); - for (var y=0; y < grids[player].length ; y++){ for (var x=0; x < grids[player][y].length ; x++){ if (grids[player][y][x] == 1){ @@ -105,6 +101,9 @@ function battleship(bot1,bot2,gridWidth,gridHeight,nbShip1,nbShip2,nbShip3,nbShi } } } + var p=createElem("p"); + p.innerHTML='players placed theirs ships'; + document.getElementById('logs').appendChild(p); }