我创造了一张非常简单的牌组。我使用了3个阵列,$ deck,$ cards,& $使用。 $ deck是玩牌,$ cards是比较数组,$ used是丢弃数组。当从$ deck使用卡时,它们将被放置在$ used数组中。我的问题是当它们放在$ used数组中时,它们被放入数字顺序。我不想要这个。我需要它们与从$ deck数组中删除它们的顺序相同。完整的代码如下。
<?php
/*
This code creates a deck of cards and deals them
It also creates a discard pile of used cards. It never
deals duplicates and will re-shuffle the deck if it
gets under 7 cards.
*/
dealCards();
/*
This function creates the deck of cards.
It will deal out 5 cards and remove those
5 cards from the deck.
*/
function dealCards() {
// checks to see if a session has been created
if (isset($_SESSION["deck"])) {
$deck = $_SESSION["deck"];
} else {
// if session has not been created it
$deck = range(1, 52);
shuffle($deck);
$_SESSION["deck"] = $deck;
}
// comparison array.
$cards = range(1, 52);
// will use the same deck until it has 7 or less cards
if (count($deck) > 7) {
// deals 5 cards from the array $deck
for ($i = 0; $i < 5; $i++) {
print "<img src="/main/deck/$deck[$i].png" alt="Card: $deck[$i]" />";
// removes dealt cards from the deck
unset($deck[$i]);
// clears empty values from the array
$deck = array_values(array_filter($deck));
// replaces session data with new deck
$_SESSION["deck"] = $deck;
}
} else {
// shuffle the full 52 cards again if the deck is less than 7 cards
$deck = range(1, 52);
shuffle($deck);
$_SESSION["deck"] = $deck;
}
/*
compares $deck to the comparison array $cards
and places the differences in an array
called $used which then can be used as a discard
pile.
*/
$used = array_diff($cards, $deck);
// testing purposes.
print "<pre>";
print_r ($deck);
print "</pre>";
print "<pre>";
print_r ($used);
print "</pre>";
}
?>
答案 0 :(得分:0)
我想到的第一件事就是当你取消设置deck
数组
// removes dealt cards from the deck
unset($deck[$i]);
您可以将卡片放入$used
数组
// removes dealt cards from the deck then put them in the discard pile
$used[$i] = $deck[$i];
但这只是第一次有效。在dealCards
的第二次使用中,它将被覆盖。
另外,$used
数组肯定会有一些空位。
你需要它们永久存储吗?
答案 1 :(得分:0)
我已经开始工作了。代码需要清理一下但至少它可以工作。感谢Wrikken和其他所有人的帮助。以下是修改后的工作代码。
<?php
/*
This code creates a deck of cards and deals them
It also creates a discard pile of used cards. It never
deals duplicates and will re-shuffle the deck if it
gets under 7 cards.
*/
dealCards();
/*
This function creates the deck of cards.
It will deal out 5 cards and remove those
5 cards from the deck.
*/
function dealCards() {
$used = array();
// checks to see if a session has been created
if (isset($_SESSION["deck"])) {
$deck = $_SESSION["deck"];
} else {
$deck = range(1, 52);
shuffle($deck);
$_SESSION["deck"] = $deck;
}
// will use the same deck until it has 7 or less cards
if (count($deck) > 7) {
if (isset($_SESSION["used"])) {
$used = $_SESSION["used"];
} else {
$_SESSION["used"] = $used;
}
// deals 5 cards from the array $deck
for ($i = 0; $i < 5; $i++) {
print <img src="/main/deck/$deck[$i].png" alt="Card: $deck[$i]" />;
$used[] = $deck[$i];
unset($deck[$i]);
// clears empty values from the array
$deck = array_values(array_filter($deck));
// replaces session data with new deck
$_SESSION["deck"] = $deck;
}
} else {
// shuffle the full 52 cards again if the deck is less than 7 cards
$deck = range(1, 52);
shuffle($deck);
$_SESSION["deck"] = $deck;
}
$_SESSION["used"] = $used;
}
?>