botsArena/src/arenas/Battleship/functions.php

100 lines
2.2 KiB
PHP
Raw Normal View History

2015-12-10 20:53:13 +01:00
<?php
function get_Post_Params($botsCount){
$keysBots=array('bot1','bot2');
foreach($keysBots as $botKey){
if(!isset($_POST[$botKey])){
return false;
}
if(!is_numeric(($_POST[$botKey]))){
}
if(($_POST[$botKey] < 0) OR ($_POST[$botKey] > $botsCount)){
error(400,"wrong parameters");
die;
}
}
return array('bot1' => $_POST['bot1'],'bot2' => $_POST['bot2']);
}
2015-12-10 22:40:08 +01:00
function generate_numeric_select($start,$end,$selected,$name,$id){
$out="<select";
if($name !== ""){
2015-12-10 22:41:21 +01:00
$out.=' name="'.$name.'"';
2015-12-10 22:40:08 +01:00
}
if($id !== ""){
2015-12-10 22:41:45 +01:00
$out.=' id="'.$id.'"';
2015-12-10 22:40:08 +01:00
}
$out.=">";
2015-12-10 22:42:16 +01:00
if($selected == -1){
2015-12-10 22:40:08 +01:00
for($i=$start; $i <= $end; $i++ ){
$out.='<option value="'.$i.'">'.$i.'</option>';
}
}else{
for($i=$start; $i < $selected; $i++ ){
$out.='<option value="'.$i.'">'.$i.'</option>';
}
$out.='<option value="'.$selected.'" selected="selected">'.$selected.'</option>';
for($i=$selected + 1; $i <= $end; $i++ ){
$out.='<option value="'.$i.'">'.$i.'</option>';
}
}
return $out."</select>";
2015-12-12 10:25:40 +01:00
}
function get_IA_Response($iaUrl,$postParams){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $iaUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postParams);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return htmlentities($output);
2015-12-14 23:12:47 +01:00
}
function place_ship_on_map($x1,$y1,$x2,$y2,$map){
2015-12-14 23:19:10 +01:00
if ((($x1 <> $x2) && ($y1 <> $y2))
OR (!isset($map[$y1][$x1]))
OR (!isset($map[$y2][$x2]))){
return false;
2015-12-14 23:12:47 +01:00
}
2015-12-14 23:19:10 +01:00
2015-12-14 23:12:47 +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++){
if($map[$i][$x1]==0){
$map[$i][$x1]=1;
}else{
return false;
}
}
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++){
if( $map[$y1][$i] == 0){
$map[$y1][$i]=1;
}else{
return false;
}
}
return $map;
}
2015-12-10 22:40:08 +01:00
}