"XGame"
"Uni1"
"Ochrona ciot"
Jestem pod wrażeniem

Żeby napisać skrypt ataku, musisz po pierwsze zdać sobie sprawę czym on jest - a jest niczym innym jak matematycznym wzorem, które przyjmuje pewne dane wejściowe (np. siłę dwóch flot) i wypluwa dane wyjściowe (która strona wygrała, jakie poniosła straty). Nikt tego nie zrobi za ciebie, musisz sam wykombinować tzw. algorytm. Kiedyś podszedłem do tego problemu czysto teoretycznie (chciałem komuś wyjaśnić na przykładzie bitwy kosmicznej projektowanie obiektowe

) i wymyśliłem takie coś: (Oczywiście jest to przerost formy nad treścią

)
<?php
/* Statek kosmiczny: */
class starship
{
var $health;
var $weapon_power;
};
/* Pole bitwy: */
class battlefield
{
var $starships; // wszystkie zaangażowane statki kosmiczne
function add_starship($starship, $side)
{
$this->starships[$side][] = $starship;
}
function do_battle()
{
$current_ship = array(); // do oznaczania, który z nich się aktualnie rusza $count = 0;
$total_sides = count($this->starships); while(true)
{
$count ++ ;
if($count>100)
return "Bitwa nieroztrzygnięta";
print "Początek rundy<br />"; for($i=0; $i<$total_sides; $i++)
{
if(!isset($this->starships[$i])) continue;
if(count($this->starships)==1
) {
return "Raport z bitwy. Zwycięzka strona: " . $i . ", ocalało " . count($this->starships[$i]) . " statków"; }
print "Tura " . $i . '<br />'; $current_ship[$i]++;
if(!isset($this->starships[$i][$current_ship[$i]])) {
$current_ship[$i] = 0;
}
$rand = $i;
while($rand==$i && isset($this->starships[$rand])) $rand = rand(0
, count($this->starships)-1
); // wybieramy losową cudzą floty $id = rand(0
, count($this->starship[$rand])-1
); // losowy cel z określonej wyżej cudzej floty $this->starships[$rand][$id]->health -= ($this->starships[$i][$current_ship[$i]]->weapon_power*rand(50
, 150
)/100
); // dodamy trochę losowości echo "Celem jest flota " . $rand . " i statek o id " . $id . ", który ma jeszcze " . $this->starships[$rand][$id]->health . " punktów życia<br />"; if($this->starships[$rand][$id]->health<=0)
{
echo "Statek zestrzelony<br />"; unset($this->starships[$rand][$id]); if(count($this->starships[$rand])==0
) {
unset($this->starships[$rand]); }
else
{
foreach($this->starships[$rand] as $key => $value)
{
$temp[] = $value;
}
$this->starships[$rand] = $temp;
}
}
}
}
}
};
/* Jakieś przykładowe dane: */
$battlefield = new battlefield;
$enterprise = new starship;
$enterprise->health = 500;
$enterprise->weapon_power = 150;
$battlefield->add_starship($enterprise, 0);
$borg_sphere = new starship;
$borg_sphere->health = 750;
$borg_sphere->weapon_power = 125;
$battlefield->add_starship($borg_sphere, 1);
$result_of_battle = $battlefield->do_battle();
?>
Wynik tego skryptu wygląda podobnie do tego:
Kod
Początek rundy
Tura 0
Celem jest flota 1 i statek o id 0, który ma jeszcze 607.5 punktów życia
Tura 1
Celem jest flota 0 i statek o id 0, który ma jeszcze 430 punktów życia
Początek rundy
Tura 0
Celem jest flota 1 i statek o id 0, który ma jeszcze 525 punktów życia
Tura 1
Celem jest flota 0 i statek o id 0, który ma jeszcze 295 punktów życia
Początek rundy
Tura 0
Celem jest flota 1 i statek o id 0, który ma jeszcze 412.5 punktów życia
Tura 1
Celem jest flota 0 i statek o id 0, który ma jeszcze 142.5 punktów życia
Początek rundy
Tura 0
Celem jest flota 1 i statek o id 0, który ma jeszcze 237 punktów życia
Tura 1
Celem jest flota 0 i statek o id 0, który ma jeszcze -8.75 punktów życia
Statek zestrzelony
Początek rundy
Raport z bitwy. Zwycięzka strona: 1, ocalało 1 statków