Merge pull request #26 from gnieark/dev

Dev
This commit is contained in:
Gnieark 2015-12-19 14:27:17 +01:00
commit 80e245b563
2 changed files with 38 additions and 7 deletions

View File

@ -207,6 +207,10 @@ switch($_POST['act']){
echo json_encode($shipsCoords); echo json_encode($shipsCoords);
break; break;
case "fight": 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")))){ // if((is_int(settype($_POST['width'],"integer"))) && (is_int(settype($_POST['height'],"integer")))){
echo rand(0,$_POST['width'] -1).",".rand(0,$_POST['height'] -1); echo rand(0,$_POST['width'] -1).",".rand(0,$_POST['height'] -1);
// }else{ // }else{

View File

@ -255,11 +255,43 @@ switch ($_POST['act']){
die; 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=''; $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)){ if(in_array($x.",".$y, $ennemyBoat)){
$result='hit'; $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; break;
} }
} }
@ -269,11 +301,6 @@ switch ($_POST['act']){
'target' => $x.",".$y, 'target' => $x.",".$y,
'result' => $result 'result' => $result
); );
if(count( $_SESSION['strikes'][$currentPlayer]) < 10){
$continue=1;
}else{
$continue=0;
}
echo json_encode(array( echo json_encode(array(
'opponent'=> $opponent, 'opponent'=> $opponent,
'target' => $x.",".$y, 'target' => $x.",".$y,