botsArena/html/StupidIABattleship.php

183 lines
5.3 KiB
PHP
Raw Normal View History

2015-12-12 10:33:33 +01:00
<?php
/*
2015-12-14 08:04:32 +01:00
function place_ship_on_map($x1,$y1,$x2,$y2,$map){
2015-12-16 00:12:48 +01:00
if ((($x1 <> $x2) && ($y1 <> $y2))
OR (!isset($map[$y1][$x1]))
OR (!isset($map[$y2][$x2]))){
return false;
2015-12-14 16:46:27 +01:00
}
2015-12-16 00:12:48 +01:00
2015-12-14 16:46:27 +01:00
if($x1 == $x2){
//horizontal ship
if($y1 <= $y2 ){
$start=$y1;
$end=$y2;
}else{
$start=$y2;
$end=$y1;
}
for($i = $start; $i <= $end; $i++){
2015-12-16 00:12:48 +01:00
if($map[$i][$x1]==0){
$map[$i][$x1]=1;
}else{
return false;
}
2015-12-14 16:46:27 +01:00
}
return $map;
}
if($y1 == $y2){
//vertical ship
if( $x1 <= $x2){
$start=$x1;
$end=$x2;
}else{
$start=$x2;
$end=$x1;
}
for( $i = $start; $i <= $end; $i++){
2015-12-16 00:12:48 +01:00
if( $map[$y1][$i] == 0){
$map[$y1][$i]=1;
}else{
return false;
}
2015-12-14 16:46:27 +01:00
}
return $map;
}
2015-12-14 08:04:32 +01:00
}
2015-12-12 13:29:37 +01:00
switch($_POST['act']){
case "init":
2015-12-12 19:10:15 +01:00
$wantedVars=array(
'match_id' => false, //false-> string ; true -> integer
'opponent' => false,
'width' => true,
'height' => true,
'ship1' => true,
'ship2' => true,
'ship3' => true,
'ship4' => true,
'ship5' => true,
'ship6' => true
);
foreach($wantedVars as $key => $shouldBeInteger){
if(($shouldBeInteger) && (!is_numeric($_POST[$key]))){
echo "var is not numeric"; die;
}
$$key=$_POST[$key];
}
2015-12-14 16:51:18 +01:00
if(!preg_match('/^[0-9]+-(1|2)$/',$match_id)){
2015-12-12 20:00:41 +01:00
echo "parametre incorrect"; die;
}
2015-12-16 00:12:48 +01:00
$map=array();
2015-12-12 20:00:41 +01:00
//construire une grille
for($i=0; $i < $width; $i++){
for($j=0; $j < $height; $j++){
2015-12-16 00:06:35 +01:00
$map[$j][$i]=0;
2015-12-12 20:00:41 +01:00
}
}
2015-12-12 19:10:15 +01:00
2015-12-14 08:04:32 +01:00
$shipsCoords=array();
2015-12-12 20:00:41 +01:00
//pour toutes les tailles de bateau
2015-12-15 22:43:06 +01:00
for($shipWidth = 6; $shipWidth > 0; $shipWidth--){
2015-12-12 20:00:41 +01:00
//nombre de bateau à placer de cette taille
2015-12-14 16:48:27 +01:00
$dynVar='ship'.$shipWidth;
$shipCount=$$dynVar; // #trollface
2015-12-15 22:40:46 +01:00
for( $sh = 0; $sh < $shipCount; $sh++){ //loop for all boats witch size is $shipWidth
2015-12-14 08:04:32 +01:00
$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?
2015-12-15 23:27:44 +01:00
//haut
$top=true;
for($i = $ytest; $i >= $ytest - $shipWidth + 1; $i--){
2015-12-15 23:29:01 +01:00
if ((!isset($map[$i][$xtest]))
2015-12-15 23:27:44 +01:00
OR ($map[$i][$xtest] == 1)){
$top=false;
break;
2015-12-14 08:04:32 +01:00
}
}
//vers le bas
2015-12-15 23:27:44 +01:00
$bottom=true;
2015-12-16 00:06:35 +01:00
for($i = $ytest; $i < $ytest + $shipWidth -1; $i++){
2015-12-15 23:29:01 +01:00
if ((!isset($map[$i][$xtest]))
2015-12-15 23:27:44 +01:00
OR ($map[$i][$xtest] == 1)){
$bottom=false;
break;
2015-12-14 08:04:32 +01:00
}
}
//droite
2015-12-15 23:27:44 +01:00
$right=true;
for($i = $xtest; $i < $xtest + $shipWidth -1; $i++){
if((!isset($map[$ytest][$i]))
OR($map[$ytest][$i] == 1)){
$right= false;
break;
2015-12-14 08:04:32 +01:00
}
}
//gauche
2015-12-15 23:27:44 +01:00
$left=true;
for($i = $xtest; $i >= $xtest - $shipWidth + 1; $i--){
if((!isset($map[$ytest][$i]))
OR($map[$ytest][$i] == 1)){
$left= false;
break;
}
2015-12-14 08:04:32 +01:00
}
2015-12-15 23:27:44 +01:00
2015-12-14 08:04:32 +01:00
$directions=array();
if($top){
$directions[]='top';
}
if($bottom){
$directions[]='bottom';
}
if($left){
$directions[]='left';
}
if($right){
$directions[]='right';
}
}
shuffle($directions);
switch($directions[0]){
case 'top':
2015-12-15 22:40:46 +01:00
$shipsCoords[]=$xtest.",".$ytest."-".$xtest.",".($ytest - $shipWidth + 1);
$map= place_ship_on_map($xtest,$ytest,$xtest,$ytest - $shipWidth + 1,$map);
2015-12-14 08:04:32 +01:00
break;
case 'bottom':
2015-12-15 22:40:46 +01:00
$shipsCoords[]=$xtest.",".$ytest."-".$xtest.",".($ytest + $shipWidth - 1);
$map= place_ship_on_map($xtest,$ytest,$xtest,$ytest + $shipWidth -1 ,$map);
2015-12-14 08:04:32 +01:00
break;
case 'left':
2015-12-15 22:40:46 +01:00
$shipsCoords[]=$xtest.",".$ytest."-".($xtest - $shipWidth + 1).",".$ytest;
$map= place_ship_on_map($xtest,$ytest,$xtest - $shipWidth + 1 ,$ytest,$map);
2015-12-14 08:04:32 +01:00
break;
case 'right':
2015-12-15 22:40:46 +01:00
$shipsCoords[]=$xtest.",".$ytest."-".($xtest + $shipWidth - 1 ).",".$ytest;
$map= place_ship_on_map($xtest,$ytest,$xtest + $shipWidth -1 ,$ytest,$map);
2015-12-14 08:04:32 +01:00
break;
}
2015-12-12 20:00:41 +01:00
}
}
2015-12-16 00:12:48 +01:00
print_r($map);
2015-12-14 16:55:48 +01:00
echo json_encode($shipsCoords);
2015-12-12 13:29:37 +01:00
break;
default:
break;
}