commit
eb50904327
|
@ -1,5 +1,28 @@
|
||||||
<?php
|
<?php
|
||||||
/*
|
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;
|
||||||
|
if( $shipsArea > $gridHeight * $gridWidth / 2){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
//longest ship
|
||||||
|
for($i=6; $i > 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){
|
function place_ship_on_map($x1,$y1,$x2,$y2,$map){
|
||||||
if ((($x1 <> $x2) && ($y1 <> $y2))
|
if ((($x1 <> $x2) && ($y1 <> $y2))
|
||||||
OR (!isset($map[$y1][$x1]))
|
OR (!isset($map[$y1][$x1]))
|
||||||
|
@ -68,13 +91,20 @@ switch($_POST['act']){
|
||||||
if(!preg_match('/^[0-9]+-(1|2)$/',$match_id)){
|
if(!preg_match('/^[0-9]+-(1|2)$/',$match_id)){
|
||||||
echo "parametre incorrect"; die;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
a:
|
||||||
$map=array();
|
$map=array();
|
||||||
//construire une grille
|
//construire une grille
|
||||||
for($i=0; $i < $width; $i++){
|
for($i=0; $i < $width; $i++){
|
||||||
for($j=0; $j < $height; $j++){
|
for($j=0; $j < $height; $j++){
|
||||||
$map[$j][$i]=0;
|
$map[$j][$i]=0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$shipsCoords=array();
|
$shipsCoords=array();
|
||||||
|
|
||||||
|
@ -84,96 +114,96 @@ switch($_POST['act']){
|
||||||
$dynVar='ship'.$shipWidth;
|
$dynVar='ship'.$shipWidth;
|
||||||
$shipCount=$$dynVar; // #trollface
|
$shipCount=$$dynVar; // #trollface
|
||||||
for( $sh = 0; $sh < $shipCount; $sh++){ //loop for all boats witch size is $shipWidth
|
for( $sh = 0; $sh < $shipCount; $sh++){ //loop for all boats witch size is $shipWidth
|
||||||
$directions=array();
|
//find free cases
|
||||||
while( count($directions) == 0){
|
$freeCases=array();
|
||||||
do{
|
for($y=0; $y < $height; $y++){
|
||||||
$xtest=rand(0,$width -1);
|
for($x=0; $x < $width; $x++){
|
||||||
$ytest=rand(0,$height -1);
|
if($map[$y][$x] == 0){
|
||||||
}while($map[$ytest][$xtest] == 1);
|
$directions=array();
|
||||||
|
//test top
|
||||||
//Y a t'il la place pour le bateau vers le haut?
|
$top=true;
|
||||||
|
for($i = $y; $i > $y - $shipWidth; $i--){
|
||||||
//haut
|
if((!isset($map[$i][$x])) OR ($map[$i][$x]==1)){
|
||||||
$top=true;
|
$top=false;
|
||||||
for($i = $ytest; $i >= $ytest - $shipWidth + 1; $i--){
|
$break;
|
||||||
if ((!isset($map[$i][$xtest]))
|
}
|
||||||
OR ($map[$i][$xtest] == 1)){
|
}
|
||||||
$top=false;
|
if($top){
|
||||||
break;
|
$directions[]='top';
|
||||||
}
|
}
|
||||||
}
|
//test Bottom
|
||||||
|
$bottom=true;
|
||||||
//vers le bas
|
for($i = $y; $i < $y + $shipWidth; $i++){
|
||||||
$bottom=true;
|
if(((!isset($map[$i][$x])) OR $map[$i][$x]==1)){
|
||||||
for($i = $ytest; $i < $ytest + $shipWidth -1; $i++){
|
$bottom=false;
|
||||||
if ((!isset($map[$i][$xtest]))
|
$break;
|
||||||
OR ($map[$i][$xtest] == 1)){
|
}
|
||||||
$bottom=false;
|
}
|
||||||
break;
|
if($bottom){
|
||||||
}
|
$directions[]='bottom';
|
||||||
}
|
}
|
||||||
|
//test left
|
||||||
//droite
|
$left=true;
|
||||||
$right=true;
|
for($i = $x; $i > $x - $shipWidth; $i--){
|
||||||
for($i = $xtest; $i < $xtest + $shipWidth -1; $i++){
|
if((!isset($map[$y][$i])) OR ($map[$y][$i]==1)){
|
||||||
if((!isset($map[$ytest][$i]))
|
$left=false;
|
||||||
OR($map[$ytest][$i] == 1)){
|
$break;
|
||||||
$right= false;
|
}
|
||||||
break;
|
}
|
||||||
}
|
if($left){
|
||||||
}
|
$directions[]='left';
|
||||||
|
}
|
||||||
//gauche
|
//test right
|
||||||
$left=true;
|
$right=true;
|
||||||
for($i = $xtest; $i >= $xtest - $shipWidth + 1; $i--){
|
for($i = $x; $i < $x + $shipWidth; $i++){
|
||||||
if((!isset($map[$ytest][$i]))
|
if((!isset($map[$y][$i])) OR ($map[$y][$i]==1)){
|
||||||
OR($map[$ytest][$i] == 1)){
|
$right=false;
|
||||||
$left= false;
|
$break;
|
||||||
break;
|
}
|
||||||
}
|
}
|
||||||
}
|
if($right){
|
||||||
|
$directions[]='right';
|
||||||
|
}
|
||||||
|
|
||||||
$directions=array();
|
if(count($directions)>0){
|
||||||
if($top){
|
$freeCases[]=array($x,$y,$directions);
|
||||||
$directions[]='top';
|
}
|
||||||
}
|
|
||||||
if($bottom){
|
}
|
||||||
$directions[]='bottom';
|
}
|
||||||
}
|
}
|
||||||
if($left){
|
|
||||||
$directions[]='left';
|
if(count($freeCases) == 0){
|
||||||
}
|
//can't place the ship
|
||||||
if($right){
|
goto a; //#facepalm
|
||||||
$directions[]='right';
|
}
|
||||||
}
|
shuffle($freeCases); //choose start case for this ship
|
||||||
}
|
shuffle($freeCases[0][2]); //choose random direction
|
||||||
|
$x=$freeCases[0][0];
|
||||||
shuffle($directions);
|
$y=$freeCases[0][1];
|
||||||
switch($directions[0]){
|
switch($freeCases[0][2][0]){
|
||||||
case 'top':
|
case 'top':
|
||||||
$shipsCoords[]=$xtest.",".$ytest."-".$xtest.",".($ytest - $shipWidth + 1);
|
$shipsCoords[]=$x.",".$y."-".$x.",".($y - $shipWidth + 1);
|
||||||
$map= place_ship_on_map($xtest,$ytest,$xtest,$ytest - $shipWidth + 1,$map);
|
$map= place_ship_on_map($x,$y,$x,$y - $shipWidth + 1,$map);
|
||||||
break;
|
break;
|
||||||
case 'bottom':
|
case 'bottom':
|
||||||
$shipsCoords[]=$xtest.",".$ytest."-".$xtest.",".($ytest + $shipWidth - 1);
|
$shipsCoords[]=$x.",".$y."-".$x.",".($y + $shipWidth - 1);
|
||||||
$map= place_ship_on_map($xtest,$ytest,$xtest,$ytest + $shipWidth -1 ,$map);
|
$map= place_ship_on_map($x,$y,$x,$y + $shipWidth -1 ,$map);
|
||||||
break;
|
break;
|
||||||
case 'left':
|
case 'left':
|
||||||
$shipsCoords[]=$xtest.",".$ytest."-".($xtest - $shipWidth + 1).",".$ytest;
|
$shipsCoords[]=$x.",".$y."-".($x - $shipWidth + 1).",".$y;
|
||||||
$map= place_ship_on_map($xtest,$ytest,$xtest - $shipWidth + 1 ,$ytest,$map);
|
$map= place_ship_on_map($x,$y,$x - $shipWidth + 1 ,$y,$map);
|
||||||
break;
|
break;
|
||||||
case 'right':
|
case 'right':
|
||||||
$shipsCoords[]=$xtest.",".$ytest."-".($xtest + $shipWidth - 1 ).",".$ytest;
|
$shipsCoords[]=$x.",".$y."-".($x + $shipWidth - 1 ).",".$y;
|
||||||
$map= place_ship_on_map($xtest,$ytest,$xtest + $shipWidth -1 ,$ytest,$map);
|
$map= place_ship_on_map($x,$y,$x + $shipWidth -1 ,$y,$map);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
print_r($map);
|
|
||||||
|
//print_r($map);
|
||||||
echo json_encode($shipsCoords);
|
echo json_encode($shipsCoords);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -57,8 +57,8 @@ switch ($_POST['act']){
|
||||||
error (500,"missing parameter 2");
|
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");
|
error (404,"grid is too little for these ships");
|
||||||
}
|
}
|
||||||
|
|
||||||
//vars checked, lets init the initGame
|
//vars checked, lets init the initGame
|
||||||
|
@ -119,14 +119,14 @@ switch ($_POST['act']){
|
||||||
list($xStart,$yStart)=explode(",",$startCoord);
|
list($xStart,$yStart)=explode(",",$startCoord);
|
||||||
list($xEnd,$yEnd)=explode(",",$endCoord);
|
list($xEnd,$yEnd)=explode(",",$endCoord);
|
||||||
if($xStart == $xEnd){
|
if($xStart == $xEnd){
|
||||||
$long=abs($yStart - $yEnd +1);
|
$long=abs($yStart - $yEnd) +1;
|
||||||
}else{
|
}else{
|
||||||
$long=abs($xStart - $xEnd +1);
|
$long=abs($xStart - $xEnd) +1;
|
||||||
}
|
}
|
||||||
$nbBoatsIwant[$long]-=1;
|
$nbBoatsIwant[$long]-=1;
|
||||||
$grid[$player]=place_ship_on_map($xStart,$yStart,$xEnd,$yEnd,$grid[$player]);
|
$grid[$player]=place_ship_on_map($xStart,$yStart,$xEnd,$yEnd,$grid[$player]);
|
||||||
if(!$grid[$player]){
|
if(!$grid[$player]){
|
||||||
echo $currentBot['name']." n'a pas placé correctement ses bateaux. Certains se chevauchent. Il perd<pre>".$anwserPlayer." ".$xStart.$yStart.$xEnd.$yEnd."</pre>";
|
echo $currentBot['name']." n'a pas placé correctement ses bateaux. Certains se chevauchent. Il perd.";
|
||||||
if($player==1){
|
if($player==1){
|
||||||
save_battle('Battleship',$bot1['name'],$bot2['name'],2);
|
save_battle('Battleship',$bot1['name'],$bot2['name'],2);
|
||||||
}else{
|
}else{
|
||||||
|
@ -137,7 +137,7 @@ switch ($_POST['act']){
|
||||||
}
|
}
|
||||||
foreach($nbBoatsIwant as $nb){
|
foreach($nbBoatsIwant as $nb){
|
||||||
if($nb <> 0){
|
if($nb <> 0){
|
||||||
echo $currentBot['name']." n'a pas placé correctement ses bateaux. Il perd. sa réponse: <pre>".$anwserPlayer."</pre>";
|
echo $currentBot['name']." n'a pas placé le bon nombre de bateaux. Il perd.";
|
||||||
if($player==1){
|
if($player==1){
|
||||||
save_battle('Battleship',$bot1['name'],$bot2['name'],2);
|
save_battle('Battleship',$bot1['name'],$bot2['name'],2);
|
||||||
}else{
|
}else{
|
||||||
|
@ -148,7 +148,7 @@ switch ($_POST['act']){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$_SESSION['grids']=$grid;
|
//$_SESSION['grids']=$grid;
|
||||||
echo json_encode($grid); die;
|
echo json_encode($grid); die;
|
||||||
|
|
||||||
die;
|
die;
|
||||||
|
|
|
@ -2,8 +2,8 @@
|
||||||
function is_it_possible_to_place_ships_on_grid($gridWidth,$gridHeight,$nbShipsSize1,$nbShipsSize2,$nbShipsSize3,$nbShipsSize4,$nbShipsSize5,$nbShipsSize6){
|
function is_it_possible_to_place_ships_on_grid($gridWidth,$gridHeight,$nbShipsSize1,$nbShipsSize2,$nbShipsSize3,$nbShipsSize4,$nbShipsSize5,$nbShipsSize6){
|
||||||
//return false or true
|
//return false or true
|
||||||
//not a perfect solution
|
//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 < $gridHeight * $gridwidth / 2){
|
if( $shipsArea > intval($gridHeight * $gridWidth / 2)){
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
//longest ship
|
//longest ship
|
||||||
|
|
|
@ -82,31 +82,31 @@ function battleship(bot1,bot2,gridWidth,gridHeight,nbShip1,nbShip2,nbShip3,nbShi
|
||||||
|
|
||||||
var xhr = Ajx();
|
var xhr = Ajx();
|
||||||
xhr.onreadystatechange = function(){if(xhr.readyState == 4){
|
xhr.onreadystatechange = function(){if(xhr.readyState == 4){
|
||||||
if(xhr.status == 200) {
|
if(xhr.status == 200) {
|
||||||
//debug
|
//debug
|
||||||
//alert(xhr.responseText);
|
//alert(xhr.responseText);
|
||||||
try{
|
try{
|
||||||
var grids = JSON.parse(xhr.responseText);
|
var grids = JSON.parse(xhr.responseText);
|
||||||
for( var player=1; player <= 2 ; player ++){
|
}catch(e){
|
||||||
var p=createElem("p");
|
document.getElementById('logs').innerHTML = xhr.responseText;
|
||||||
p.innerHTML='Reponse joueurs:' + xhr.responseText;
|
return;
|
||||||
document.getElementById('logs').appendChild(p);
|
}
|
||||||
|
|
||||||
for (var y=0; y < grids[player].length ; y++){
|
for( var player=1; player <= 2 ; player ++){
|
||||||
for (var x=0; x < grids[player][y].length ; x++){
|
for (var y=0; y < grids[player].length ; y++){
|
||||||
if (grids[player][y][x] == 1){
|
for (var x=0; x < grids[player][y].length ; x++){
|
||||||
document.getElementById( 'bot' + player + '-' + y + '-' + x).className="shipOn";
|
if (grids[player][y][x] == 1){
|
||||||
}
|
document.getElementById( 'bot' + player + '-' + y + '-' + x).className="shipOn";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
var p=createElem("p");
|
||||||
catch(e){
|
p.innerHTML='players placed theirs ships';
|
||||||
document.getElementById('logs').innerHTML = xhr.responseText;
|
document.getElementById('logs').appendChild(p);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}};
|
}};
|
||||||
xhr.open("POST", '/Battleship', true);
|
xhr.open("POST", '/Battleship', true);
|
||||||
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
||||||
|
|
Loading…
Reference in New Issue
Block a user