Я создал очень простую колоду карт. Я использую 3 массива, $ deck, $ cards и $ used. $ 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>";
}
?>
У меня это работает. Код нужно немного почистить, но, по крайней мере, он работает. Спасибо за помощь 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;
}
?>
Первое, что приходит мне в голову, это то, что когда вы отключите свой массив deck
// removes dealt cards from the deck
unset($deck[$i]);
вы можете поместить карту в $used
array
// removes dealt cards from the deck then put them in the discard pile
$used[$i] = $deck[$i];
Но это будет работать только в первый раз. Во втором использовании dealCards
оно будет перезаписано. Кроме того, $used
array, несомненно, будет иметь некоторую пустую позицию в нем. Вам нужно, чтобы они хранились постоянно?
1, 4, 6, 9
сброшенных карт, как должен выглядеть массив $used
?
unset($deck[$i]);
, сделайте$_SESSION['used'][] = $deck[$i];
, Без некоторого места для хранения порядка, в котором использовались карты, вы никогда не сможете «перестроить» этот точный массив в том же порядке.