Skip to content

Instantly share code, notes, and snippets.

@Pr3d4dor
Created September 19, 2015 17:09
Show Gist options
  • Save Pr3d4dor/91660ab7da7944c92728 to your computer and use it in GitHub Desktop.
Save Pr3d4dor/91660ab7da7944c92728 to your computer and use it in GitHub Desktop.
batalhanaval.php
<?php
define("LIN", 7);
define ("COL", 7);
function verificaCoordenadas($x, $y){
$controle1 = 0;
$controle2 = 0;
if (($x>=65)&&($x<71)){
$controle1=1;
}
if (($y>=0)&&($y<7))
$controle2=1;
if (($controle1==1)&&($controle2==1))
return true;
else
return false;
}
function imprimirCampo($campo){
$x = 65;
$i = 0;
$j = 0;
for ($i=0; $i < LIN; $i++) {
for ($j=0; $j < COL; $j++){
echo " ";
print ($campo[$i][$j]);
}
print "<br/>";
}
}
function geraNumerosHidroaviao(&$x,&$y){
$x = mt_rand(0,5) + 1;
$y = mt_rand (0,3) + 1;
}
function geraNumerosCruzador(&$x,&$y){
$x = mt_rand(0,5) + 1;
$y = mt_rand (0,4) + 1;
}
function insereHidroaviao(&$campo){
$cont = 0;
$x = 0;
$y = 0;
geraNumerosHidroaviao($x,$y);
while($cont < 2){
if (($campo[$x][$y]=="x")&&($campo[$x-1][$y+1]=="x")&&($campo[$x][$y+2]=="x")&&($campo[$x-1][$y+2]=="x")&&($campo[$x-1][$y]=="x")){
$campo[$x][$y]="H";
$campo[$x-1][$y+1]="H";
$campo[$x][$y+2]="H";
$cont+=1;
}
geraNumerosHidroaviao($x,$y);
}
}
function insereCruzador(&$campo){
$cont = 0;
$x = 0;
$y = 0;
geraNumerosCruzador($x,$y);
while($cont < 2){
if (($campo[$x][$y]=="x")&&($campo[$x][$y+1]=="x")){
$campo[$x][$y]="C";
$campo[$x][$y+1]="C";
$cont+=1;
}
geraNumerosCruzador($x,$y);
}
}
?>
<?php
require 'batalhanaval.php';
$campo = array( array('x','x','x','x','x','x','x'),
array('x','x','x','x','x','x','x'),
array('x','x','x','x','x','x','x'),
array('x','x','x','x','x','x','x'),
array('x','x','x','x','x','x','x'),
array('x','x','x','x','x','x','x'),
array('x','x','x','x','x','x','x')
);
$campoVisao = array( array('x','x','x','x','x','x','x'),
array('x','x','x','x','x','x','x'),
array('x','x','x','x','x','x','x'),
array('x','x','x','x','x','x','x'),
array('x','x','x','x','x','x','x'),
array('x','x','x','x','x','x','x'),
array('x','x','x','x','x','x','x')
);
$contadorDeJogadas = 0;
$totalDeAcertosParaVencer = 10;
$i = 0;
$j = 0;
$controle1 = 0;
$controle2 = 0;
$y = 0;
$x = 0;
insereCruzador($campo);
insereHidroaviao($campo);
do{
echo "Campo: <br/>";
imprimirCampo($campoVisao);
printf("Digite as coordenadas onde o ataque sera realizado [x,y]:<br/>");
}while ($totalDeAcertosParaVencer!=0);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment