将每个多维数组值与第二个多维数组中的所有值进行比较?

时间:2015-01-04 01:19:35

标签: php arrays multidimensional-array foreach

我在这里有点新意,感谢您的帮助。

我在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
        )

)

数组名称为:

$攻击者

$维护者

我需要比较$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>";
                }
            }
        }

我希望这是足够的信息。 问候 的Jesper

2 个答案:

答案 0 :(得分:1)

进行这种检查非常可怕且无法扩展...

你可能会从拥有一张位置图中受益。然后,您可以通过观察事件和应用结果来简化。

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);

这里有很多很大的改进空间,但希望你能看到你不需要做所有这些检查。士兵可以攻击,攻击通知地图,地图检查哪些位置在攻击范围内。然后这些位置称为该位置任何士兵的防御方法。然后士兵捍卫他们的健康减少了攻击量。

正如我所说的那样,需要改进的空间,例如引入侧面并确保不会发生任何友好的射击。通过向士兵应用移动方法来改善移动,然后他们可以在MapPositions上触发离开/输入事件(也许使MapPosition成为士兵的观察者)。

但重点是这种循环并不是真正必要的,并且会受到缩放的影响。更好的应用程序设计将获得自己的奖励......

答案 1 :(得分:0)

您需要使用嵌套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>";
            }
        }
     }

    }