crobertsbmw / deckofcards

An API to simulate a deck of cards
MIT License
1.34k stars 335 forks source link

Add cards back into deck & Also shuffle existing deck #104

Closed Arniox closed 2 years ago

Arniox commented 3 years ago

I'm creating a game in discord and it is going well. But I need 2 things:

First is the ability to add cards back into the deck.

The use case for this is during a game if the deck has been finished, and the discard pile is full and there are still players left in a game that want/have to continue playing, I want to be able to grab the discard pile, draw all the cards from it, and reinsert them back into the deck without destroying the current piles.

So say there is 4 players, each with 5 cards left in play. That's 20 cards that are not in the deck. And then say the deck runs out, and the last 32 cards are sitting in the discard pile. I want to be able to take those 32 cards, and reinsert them into the deck and shuffle the deck without deleting the 4 players' hands.

Second, which directly follows on from the above, is the ability to shuffle an existing deck without deleting all the piles. As above, in that situation, if there are 32 cards sitting in the deck in a pretty ordered state after playing with them, I want to be able to re-shuffle the deck without touching the other piles.

Thanks

Arniox commented 3 years ago

At the moment, I am using something like this:

            //Draw cards from deck
            const allCards = (await axios.get(`https://deckofcardsapi.com/api/deck/${this.deck.deck_id}/draw/?count=${this.deck.remaining}`)).data,
                cardIdsPrior = allCards.map(v => v.code),
                cardIdsAfterShuffle = cardIdsPrior.shuffle();

            //Enter cards backinto deck
            const shuffledOldDeck = await axios.get(`https://deckofcardsapi.com/api/deck/${this.deck.deck_id}/shuffle/?cards=${cardIdsAfterShuffle.join(',')}`);
            //Update deck
            this.deck = shuffledOldDeck.data;

But this still results in the entire deck getting shuffled and all the piles being deleted.

crobertsbmw commented 3 years ago

Unfortunately, I'm not working on large features for this project anymore. Mostly just doing maintenance stuff, but I do accept PRs if you want to figure out how to add it yourself. Otherwise a couple workarounds I can think of: 1) Keep track of it locally. If you need to turn the discard pile into the main draw pile, you can create an array locally, shuffle it, and just have a boolean flag indicating that instead of drawing with the api, to draw from the array. Of course, once you start doing that, you may be tempted to just do everything locally, rather than using the API. 2) You could create a temporary "partial deck" specifying the cards from the discard pile. ie: https://deckofcardsapi.com/api/deck/new/shuffle/?cards=AS,2S,KS,AD,2D,KD,AC,2C,KC,AH,2H,KH And from that deck, shuffle and draw cards. Although, when moving cards to various piles, you may have to do some operations on both decks at the same time to make sure that they both stay in sync.

Arniox commented 3 years ago

Ok, I was thinking about possibly making my own cards system/API anyways. I'll have a look at your code and see if I can easily add these systems or not

jmb commented 3 years ago

I'm happy to look at implementing this for a PR or two, but so that I'm clear about the functionality changes, is it ok to make these changes?

crobertsbmw commented 3 years ago

The first change looks like it would be a breaking change, so if people have already written their card game, and the API changed to not shuffle all the cards, then this would break people's code, which I'd rather not do.

I think the easiest way to implement this would be to just add the ability to draw random cards from Piles. I think this could be done relatively simply (and could be useful for people playing Old Maid).

jmb commented 3 years ago

Good point. Maybe either another API endpoint or adding a parameter to the shuffle endpoint could do it.

jmb commented 3 years ago

I've started taking a look - this is my first commit on my fork to allow the remaining deck stack to be shuffled only with a remaining=true parameter: https://github.com/jmb/deckofcards/commit/8a6002c0ae0786a3ad287dfa1fa2c69f0a8ab163

There are a few places that things could be refactored as well I think.

jmb commented 2 years ago

I've now added the functionality to allow random cards to be drawn from piles: https://github.com/jmb/deckofcards/commit/7dd528fb107534af6412e7bd839555bd3e85c3ee

I'll next look to allow cards to be replaced into the main deck.

jmb commented 2 years ago

Final (hopefully!) PR #107 to tidy everything up!