将每个多维数组值与第二个多维数组中的所有值进行比较?
Compare each multidimensional array value to all values in second multidimensional array?
我有点新手,感谢您的帮助。
我在 PHP 中得到了 2 个多维数组
两者都非常像这个:
Array
(
[0] => Array
(
[0] => 1
[Id] => 1
[1] => Soldier
[Name] => Soldier
[2] => 100
[Hitpoints] => 100
[3] => 15
[Attack] => 15
[4] => 50
[Speed] => 50
[5] => 50
[Range] => 50
[Total Units] => 511
[Combined Damage] => 7588.35
[Combined Hitpoints] => 51100
[Position] => 50
)
[1] => Array
(
[0] => 2
[Id] => 2
[1] => Sniper
[Name] => Sniper
[2] => 20
[Hitpoints] => 20
[3] => 50
[Attack] => 50
[4] => 20
[Speed] => 20
[5] => 300
[Range] => 0
[Total Units] => 0
[Combined Damage] => 0
[Combined Hitpoints] => 0
[Position] => 50
)
)
数组名称是:
$攻击者
$Defenders
我需要比较来自 $Attackers[*]['Position'] with $Defenders[*]['Position']
的值。
我确实做了这个循环,但我的问题是,它只检查彼此相同的索引计数。我需要检查 $Attackers[0] 到所有 $Defender 索引,然后检查 $Attackers[1] 到所有 $Defender 索引,依此类推。
这是我的原始代码,但只检查相同的索引。
for($rowcount=0;$rowcount<count($Attackers);$rowcount++){
if (isset($Attackers[$rowcount]['Range']) && $Attackers[$rowcount]['Range'] != 0) {
if (($Attackers[$rowcount]['Position'] + $Defenders[$rowcount]['Position']) < $Attackers[$rowcount]['Range']) {
echo "within range, ATTACK";
} else {
echo "Before: " . $Attackers[$rowcount]['Position'] . "<br>";
$Attackers[$rowcount]['Position'] = $Attackers[$rowcount]['Position'] - $Attackers[$rowcount]['Speed'];
echo "After: " . $Attackers[$rowcount]['Position'] . "<br>";
}
}
}
我希望这些信息足够了。
问候
杰斯帕
您需要使用嵌套 for 循环来检查所有防御者位置与一个攻击者位置。
像这样的事情应该让你开始。
for($rowcount=0;$rowcount<count($Attackers);$rowcount++){
for($defrowcount=0; $defrowcount<count($Defenders); $defrowcount++){
if (isset($Attackers[$rowcount]['Range']) && $Attackers[$rowcount]['Range'] != 0) {
if (($Attackers[$rowcount]['Position'] + $Defenders[$defrowcount]['Position']) < $Attackers[$rowcount]['Range']) {
echo "within range, ATTACK";
} else {
echo "Before: " . $Attackers[$rowcount]['Position'] . "<br>";
$Attackers[$rowcount]['Position'] = $Attackers[$rowcount]['Position'] - $Attackers[$rowcount]['Speed'];
echo "After: " . $Attackers[$rowcount]['Position'] . "<br>";
}
}
}
}
进行这种检查非常可怕且不可扩展...
您可能会受益于位置图。然后,您可以通过观察事件和应用结果来简化。
class GameMap implements SplObserver
{
private $positions;
public function __construct($xSize, $ySize)
{
$this->positions = array();
for ($x = 0; $x < $xSize; $x++) {
$this->positions[$x] = array();
for ($y = 0; $y < $ySize; $y++) {
$this->positions[$x][$y] = new MapPosition($x, $y);
}
}
}
public function update(SplSubject $subject)
{
switch ($subject->getAction($this)) {
case "attack":
$positions = $this->getPositionsInRange($subject);
foreach ($positions as $position) {
$position->defend($subject);
}
break;
}
}
private function getPositionsInRange(Soldier $soldier)
{
$inRange = array();
$position = $soldier->getPosition();
$range = $soldier->range;
for ($x = ($position->coord["x"] - $range); $x < ($position->coord["x"] + $range); $x++) {
for ($y = ($position->coord["y"] - $range); $y < ($position->coord["y"] + $range); $y++) {
if (isset($this->positions[$x][$y])) {
$inRange[] = $this->positions[$x][$y];
}
}
}
return $inRange;
}
public function __get($key)
{
return isset($this->$key) ? $this->$key : null;
}
}
class MapPosition
{
private $coords = array();
private $players;
public function __construct($x, $y)
{
$this->coords["x"] = $x;
$this->coords["y"] = $y;
$this->players = new SplObjectStorage();
}
public function enter(Soldier $player)
{
$this->players->attach($player);
return $this;
}
public function leave(Soldier $player)
{
$this->players->detach($player);
return $this;
}
public function defend(Soldier $soldier)
{
foreach($this->players as $player)
{
$player->defend($soldier);
}
}
public function __get($key)
{
return isset($this->$key) ? $this->$key : null;
}
}
class Soldier implements SplSubject
{
private $id;
private $name;
private $hitPoints;
private $health;
private $attack;
private $speed;
private $range;
private $observers;
private $action;
private $position;
public function __construct($soldierData)
{
$this->id = $soldierData["id"];
$this->name = $soldierData["name"];
$this->hitPoints = $soldierData["hit_points"];
$this->health = $soldierData["health"];
$this->attack = $soldierData["attack"];
$this->speed = $soldierData["speed"];
$this->range = $soldierData["range"];
$this->observers = new SplObjectStorage();
}
public function attach(SplObserver $observer)
{
$this->observers->attach($observer);
}
public function detach(SplObserver $observer)
{
$this->observers->detach($observer);
}
public function notify()
{
foreach ($this->observers as $observer) {
$observer->update($this);
}
}
public function getAction($observer)
{
return $this->observers->contains($observer) ? $this->action : null;
}
public function setPosition(MapPosition $position)
{
$this->postion = $position;
}
public function getPosition()
{
return $this->position;
}
public function attack()
{
$this->action = "attack";
$this->notify();
}
public function defend(Soldier $soldier)
{
$this->health -= $soldier->attack;
}
public function __get($key)
{
return isset($this->$key) ? $this->$key : null;
}
}
$s1 = new Soldier(array(
"id" => 1,
"name" => 'Sniper',
"hit_points" => 1000,
"health" => 100,
"attack" => 20,
"speed" => 5,
"range" => 10
));
$s2 = new Soldier(array(
"id" => 1,
"name" => 'Medic',
"hit_points" => 10000,
"health" => 100,
"attack" => 4,
"speed" => 10,
"range" => 1
));
$s3 = new Soldier(array(
"id" => 1,
"name" => 'Private',
"hit_points" => 5000,
"health" => 100,
"attack" => 10,
"speed" => 15,
"range" => 3
));
$a1 = new Soldier(array(
"id" => 1,
"name" => 'Sniper',
"hit_points" => 1000,
"health" => 100,
"attack" => 20,
"speed" => 5,
"range" => 15
));
$a2 = new Soldier(array(
"id" => 1,
"name" => 'Medic',
"hit_points" => 10000,
"health" => 100,
"attack" => 4,
"speed" => 10,
"range" => 1
));
$a3 = new Soldier(array(
"id" => 1,
"name" => 'Private',
"hit_points" => 5000,
"health" => 100,
"attack" => 10,
"speed" => 15,
"range" => 3
));
$map = new GameMap(20, 20);
$s1->attach($map);
$s2->attach($map);
$s3->attach($map);
$a1->attach($map);
$a2->attach($map);
$a3->attach($map);
$map->positions[0][0]->enter($a1)->enter($a2)->enter($a3);
$map->positions[9][9]->enter($s1)->enter($s2)->enter($s3);
var_dump($s1->health, $s2->health, $s3->health);
$a1->attack();
var_dump($s1->health, $s2->health, $s3->health);
$map->positions[9][9]->leave($s3);
$map->positions[19][19]->enter($s3);
$a1->attack();
var_dump($s1->health, $s2->health, $s3->health);
这里有很多改进的空间,但希望您能看到您不需要进行所有这些检查。士兵可以攻击,攻击通知地图,地图检查哪些位置在攻击范围内。这些位置然后调用该位置上任何士兵的防御方法。防御的士兵生命减少攻击量
正如我所说,有很多改进空间,例如引入双方并确保不会发生友军射击。通过将 move 方法应用于 Soldiers 来改善运动,然后他们可以在 MapPositions 上触发 leave/enter 事件(也许也使 MapPosition 成为士兵的观察者)。
但要点是,这种循环并不是真正必要的,而且会影响缩放。更好地设计您的应用程序将收获自己的回报...
我有点新手,感谢您的帮助。
我在 PHP 中得到了 2 个多维数组
两者都非常像这个:
Array
(
[0] => Array
(
[0] => 1
[Id] => 1
[1] => Soldier
[Name] => Soldier
[2] => 100
[Hitpoints] => 100
[3] => 15
[Attack] => 15
[4] => 50
[Speed] => 50
[5] => 50
[Range] => 50
[Total Units] => 511
[Combined Damage] => 7588.35
[Combined Hitpoints] => 51100
[Position] => 50
)
[1] => Array
(
[0] => 2
[Id] => 2
[1] => Sniper
[Name] => Sniper
[2] => 20
[Hitpoints] => 20
[3] => 50
[Attack] => 50
[4] => 20
[Speed] => 20
[5] => 300
[Range] => 0
[Total Units] => 0
[Combined Damage] => 0
[Combined Hitpoints] => 0
[Position] => 50
)
)
数组名称是:
$攻击者
$Defenders
我需要比较来自 $Attackers[*]['Position'] with $Defenders[*]['Position']
的值。
我确实做了这个循环,但我的问题是,它只检查彼此相同的索引计数。我需要检查 $Attackers[0] 到所有 $Defender 索引,然后检查 $Attackers[1] 到所有 $Defender 索引,依此类推。
这是我的原始代码,但只检查相同的索引。
for($rowcount=0;$rowcount<count($Attackers);$rowcount++){
if (isset($Attackers[$rowcount]['Range']) && $Attackers[$rowcount]['Range'] != 0) {
if (($Attackers[$rowcount]['Position'] + $Defenders[$rowcount]['Position']) < $Attackers[$rowcount]['Range']) {
echo "within range, ATTACK";
} else {
echo "Before: " . $Attackers[$rowcount]['Position'] . "<br>";
$Attackers[$rowcount]['Position'] = $Attackers[$rowcount]['Position'] - $Attackers[$rowcount]['Speed'];
echo "After: " . $Attackers[$rowcount]['Position'] . "<br>";
}
}
}
我希望这些信息足够了。 问候 杰斯帕
您需要使用嵌套 for 循环来检查所有防御者位置与一个攻击者位置。
像这样的事情应该让你开始。
for($rowcount=0;$rowcount<count($Attackers);$rowcount++){
for($defrowcount=0; $defrowcount<count($Defenders); $defrowcount++){
if (isset($Attackers[$rowcount]['Range']) && $Attackers[$rowcount]['Range'] != 0) {
if (($Attackers[$rowcount]['Position'] + $Defenders[$defrowcount]['Position']) < $Attackers[$rowcount]['Range']) {
echo "within range, ATTACK";
} else {
echo "Before: " . $Attackers[$rowcount]['Position'] . "<br>";
$Attackers[$rowcount]['Position'] = $Attackers[$rowcount]['Position'] - $Attackers[$rowcount]['Speed'];
echo "After: " . $Attackers[$rowcount]['Position'] . "<br>";
}
}
}
}
进行这种检查非常可怕且不可扩展...
您可能会受益于位置图。然后,您可以通过观察事件和应用结果来简化。
class GameMap implements SplObserver
{
private $positions;
public function __construct($xSize, $ySize)
{
$this->positions = array();
for ($x = 0; $x < $xSize; $x++) {
$this->positions[$x] = array();
for ($y = 0; $y < $ySize; $y++) {
$this->positions[$x][$y] = new MapPosition($x, $y);
}
}
}
public function update(SplSubject $subject)
{
switch ($subject->getAction($this)) {
case "attack":
$positions = $this->getPositionsInRange($subject);
foreach ($positions as $position) {
$position->defend($subject);
}
break;
}
}
private function getPositionsInRange(Soldier $soldier)
{
$inRange = array();
$position = $soldier->getPosition();
$range = $soldier->range;
for ($x = ($position->coord["x"] - $range); $x < ($position->coord["x"] + $range); $x++) {
for ($y = ($position->coord["y"] - $range); $y < ($position->coord["y"] + $range); $y++) {
if (isset($this->positions[$x][$y])) {
$inRange[] = $this->positions[$x][$y];
}
}
}
return $inRange;
}
public function __get($key)
{
return isset($this->$key) ? $this->$key : null;
}
}
class MapPosition
{
private $coords = array();
private $players;
public function __construct($x, $y)
{
$this->coords["x"] = $x;
$this->coords["y"] = $y;
$this->players = new SplObjectStorage();
}
public function enter(Soldier $player)
{
$this->players->attach($player);
return $this;
}
public function leave(Soldier $player)
{
$this->players->detach($player);
return $this;
}
public function defend(Soldier $soldier)
{
foreach($this->players as $player)
{
$player->defend($soldier);
}
}
public function __get($key)
{
return isset($this->$key) ? $this->$key : null;
}
}
class Soldier implements SplSubject
{
private $id;
private $name;
private $hitPoints;
private $health;
private $attack;
private $speed;
private $range;
private $observers;
private $action;
private $position;
public function __construct($soldierData)
{
$this->id = $soldierData["id"];
$this->name = $soldierData["name"];
$this->hitPoints = $soldierData["hit_points"];
$this->health = $soldierData["health"];
$this->attack = $soldierData["attack"];
$this->speed = $soldierData["speed"];
$this->range = $soldierData["range"];
$this->observers = new SplObjectStorage();
}
public function attach(SplObserver $observer)
{
$this->observers->attach($observer);
}
public function detach(SplObserver $observer)
{
$this->observers->detach($observer);
}
public function notify()
{
foreach ($this->observers as $observer) {
$observer->update($this);
}
}
public function getAction($observer)
{
return $this->observers->contains($observer) ? $this->action : null;
}
public function setPosition(MapPosition $position)
{
$this->postion = $position;
}
public function getPosition()
{
return $this->position;
}
public function attack()
{
$this->action = "attack";
$this->notify();
}
public function defend(Soldier $soldier)
{
$this->health -= $soldier->attack;
}
public function __get($key)
{
return isset($this->$key) ? $this->$key : null;
}
}
$s1 = new Soldier(array(
"id" => 1,
"name" => 'Sniper',
"hit_points" => 1000,
"health" => 100,
"attack" => 20,
"speed" => 5,
"range" => 10
));
$s2 = new Soldier(array(
"id" => 1,
"name" => 'Medic',
"hit_points" => 10000,
"health" => 100,
"attack" => 4,
"speed" => 10,
"range" => 1
));
$s3 = new Soldier(array(
"id" => 1,
"name" => 'Private',
"hit_points" => 5000,
"health" => 100,
"attack" => 10,
"speed" => 15,
"range" => 3
));
$a1 = new Soldier(array(
"id" => 1,
"name" => 'Sniper',
"hit_points" => 1000,
"health" => 100,
"attack" => 20,
"speed" => 5,
"range" => 15
));
$a2 = new Soldier(array(
"id" => 1,
"name" => 'Medic',
"hit_points" => 10000,
"health" => 100,
"attack" => 4,
"speed" => 10,
"range" => 1
));
$a3 = new Soldier(array(
"id" => 1,
"name" => 'Private',
"hit_points" => 5000,
"health" => 100,
"attack" => 10,
"speed" => 15,
"range" => 3
));
$map = new GameMap(20, 20);
$s1->attach($map);
$s2->attach($map);
$s3->attach($map);
$a1->attach($map);
$a2->attach($map);
$a3->attach($map);
$map->positions[0][0]->enter($a1)->enter($a2)->enter($a3);
$map->positions[9][9]->enter($s1)->enter($s2)->enter($s3);
var_dump($s1->health, $s2->health, $s3->health);
$a1->attack();
var_dump($s1->health, $s2->health, $s3->health);
$map->positions[9][9]->leave($s3);
$map->positions[19][19]->enter($s3);
$a1->attack();
var_dump($s1->health, $s2->health, $s3->health);
这里有很多改进的空间,但希望您能看到您不需要进行所有这些检查。士兵可以攻击,攻击通知地图,地图检查哪些位置在攻击范围内。这些位置然后调用该位置上任何士兵的防御方法。防御的士兵生命减少攻击量
正如我所说,有很多改进空间,例如引入双方并确保不会发生友军射击。通过将 move 方法应用于 Soldiers 来改善运动,然后他们可以在 MapPositions 上触发 leave/enter 事件(也许也使 MapPosition 成为士兵的观察者)。
但要点是,这种循环并不是真正必要的,而且会影响缩放。更好地设计您的应用程序将收获自己的回报...