如何取消设置此数组中的元素?
How to unset an element in this array?
我不知道如何在绘制后取消设置我的卡片,我的数组总是完整的事件 avec 我使用我的函数 draw()
当我在我的绘图函数之后使用 var_dump 但当我在数组已满时使用它时,它会起作用。
我不知道数组是否被重新填充以及为什么即使在 unset
之后它 returns 到 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é';
}
?>
尝试通过引用传递 $deck 数组:
public function draw(& $deck){
$sortedDeck = array_values($deck);
$rand = mt_rand(0,count( $sortedDeck ) -1 );
$drawnCard = $sortedDeck[$rand];
unset($deck[$rand]);
return $drawnCard;
}
Are arrays in PHP copied as value or as reference to new variables, and when passed to functions?
我不知道如何在绘制后取消设置我的卡片,我的数组总是完整的事件 avec 我使用我的函数 draw() 当我在我的绘图函数之后使用 var_dump 但当我在数组已满时使用它时,它会起作用。 我不知道数组是否被重新填充以及为什么即使在 unset
之后它 returns 到 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é';
}
?>
尝试通过引用传递 $deck 数组:
public function draw(& $deck){
$sortedDeck = array_values($deck);
$rand = mt_rand(0,count( $sortedDeck ) -1 );
$drawnCard = $sortedDeck[$rand];
unset($deck[$rand]);
return $drawnCard;
}
Are arrays in PHP copied as value or as reference to new variables, and when passed to functions?