Я не знаю, как отключить мои карты после рисования, мой массив всегда заполнен, поэтому я использую свою функцию draw (). Она работает, когда я использую var_dump сразу после моей функции рисования, но когда я использую ее в то время как массив полный. Я не знаю, переполняется ли массив или нет, и почему он возвращается к 30 элементам даже после сброса

Спасибо за помощь!

  <?php
    class Card{
        public $name;
        public $life;
        public $damage;
        function __construct($name, $life, $damage) {
            $this->name = $name;
            $this->life = $life;
            $this->damage = $damage;
        }
    }

    class Deck{
        public $cards;

        public function draw($deck){
            $sortedDeck = array_values($deck);
            $rand = mt_rand(0,count( $sortedDeck ) -1 );
            $drawnCard = $sortedDeck[$rand];
            unset($deck[$rand]);
            return $drawnCard;
        }

        public function shuffle_array($array) {
            $shuffled_array = [];
            $arr_length = sizeof($array);
        
            if($arr_length < 2) {
                return $array;
            }
        
            while($arr_length) {
                --$arr_length;
                $rand_key = array_keys($array)[mt_rand(0, $arr_length)];
        
                $shuffled_array[$rand_key] = $array[$rand_key];
                unset($array[$rand_key]);
            }
        
            return $shuffled_array;
        }
        
    

        function __construct() {
            $names = ["loup", "troll", "gobelin"];
            $a = 0;
            $this->cards = [];
            while ($a <= 29) {
                array_push($this->cards, new Card($names[rand(0, count($names) -1 )], rand(5, 15), rand(5, 15)));
                $a++;
            }
            
        } 
    }

    class Player{
        public $name;
        public $health;
        public $card;

        function __construct($name, $health, $card) {
            $this->name = $name;
            $this->health = $health;
            $this->card = $card;
        } 
    }

    $deck1 = new Deck();
    $deck2 = new Deck();

    $finalDeck1 = $deck1->shuffle_array($deck1->cards) ;
    $finalDeck2 = $deck2->shuffle_array($deck2->cards) ;


    if (empty($finalDeck1)) {
        echo "empty";
        foreach ($deck1 as $key => $value) {
            foreach ($value as $key1 => $value1) {
                array_push($finalDeck1, $value1);
            }
        }
    }


    if (empty($finalDeck2)) {
        echo "empty";
        foreach ($deck2 as $key => $value) {
            foreach ($value as $key1 => $value1) {
                array_push($finalDeck2, $value1);
            }
        }
        
    }


    $player1 = new Player("player1", 30 , $deck1->draw($finalDeck1));
    $player2 = new Player("player2", 30 , $deck2->draw($finalDeck2));



    while ($player1->health > 0 && $player2->health > 0) {
        if ($player1->card->life < $player2->card->damage) {
            echo "La carte du player 1 " . $player1->card->name . " à " . $player1->card->damage . " de dégats et " . $player1->card->life . " pv". "<br>";
            echo "La carte du player 2 " . $player2->card->name . " à "  . $player2->card->damage . " de dégats et " . $player2->card->life . " pv". "<br>" ; 
            $player1->health = $player1->health + ($player1->card->life - $player2->card->damage);
            echo 'PDV du player 1 = ' . $player1->health . "<br>";
            echo 'PDV du player 2 = ' . $player2->health . "<br>" . "<br>";
            $player1->card = $deck1->draw($finalDeck1);
            $player2->card = $deck2->draw($finalDeck2);
            var_dump($finalDeck1);
        }
        if ($player2->card->life < $player1->card->damage){
            echo "La carte du player 1 " . $player1->card->name . " à " . $player1->card->damage . " de dégats et " . $player1->card->life . " pv". "<br>";
            echo "La carte du player 2 " . $player1->card->name . " à "  . $player2->card->damage . " de dégats et " . $player2->card->life . " pv". "<br>" ; 
            $player2->health = $player2->health + ($player2->card->life - $player1->card->damage);
            echo 'PDV du player 1 = ' . $player1->health . "<br>";
            echo 'PDV du player 2 = ' . $player2->health . "<br>" . "<br>";
            $player1->card = $deck1->draw($finalDeck1);
            $player2->card = $deck2->draw($finalDeck2);
            var_dump($finalDeck1);
        }
        if ($player2->card->life > $player1->card->damage && $player2->card->life > $player1->card->damage) {
            echo "La carte du player 1 " . $player1->card->name ." à " . $player1->card->damage . " de dégats et " . $player1->card->life . " pv". "<br>";
            echo "La carte du player 2 " . $player1->card->name ." à "  . $player2->card->damage . " de dégats et " . $player2->card->life . " pv". "<br>" ; 
            echo 'PDV du player 1 = ' . $player1->health . "<br>";
            echo 'PDV du player 2 = ' . $player2->health . "<br>" . "<br>";
            $player1->card = $deck1->draw($finalDeck1);
            $player2->card = $deck2->draw($finalDeck2);
            var_dump($finalDeck1);
        }
        else{
            $player1->card = $deck1->draw($finalDeck1);
            $player2->card = $deck2->draw($finalDeck2);

        }
    }

    if ($player1->health < 1 && $player2->health < 1) {
        echo "Egalité";
    }
    elseif (count($finalDeck1) == 0) {
        echo 'Le player 2 à gagné';
    }
    elseif (count($finalDeck2) == 0) {
        echo 'Le player 1 à gagné';
    }
    elseif ($player1->health < 1) {
        echo 'Le player 2 à gagné';
    }
    elseif ($player2->health <1) {
        echo 'Le player 1 à gagné';
    }
?>
1
Antoine Simonot 28 Фев 2021 в 02:35

1 ответ

Лучший ответ

Попробуйте передать массив $ deck по ссылке:

public function draw(& $deck){
            $sortedDeck = array_values($deck);
            $rand = mt_rand(0,count( $sortedDeck ) -1 );
            $drawnCard = $sortedDeck[$rand];
            unset($deck[$rand]);
            return $drawnCard;
        }

Копируются ли массивы в PHP как значения или как ссылки на новые переменные и при передаче в функции?

0
Joe Ward 27 Фев 2021 в 23:54