In this challenge, you will create the logic for a Trading Card Game (TCG) where players can open booster packs to enhance their deck.
On TCG games, cards are grouped by collections that are groups of cards that belong together, usually sharing a common theme or set.
Booster packs are sealed packs that contains a fixed number of cards from a single collection, which players can open to obtain new cards for their deck.
Cards are assigned to booster packs based on specific probability rules and rarity constraints.
Your task is to write a function that simulates the opening of a booster pack for an online TCG game and assigns cards according to the given rules.
Write a service in Python that:
- Simulates the opening of a booster pack.
- Assigns cards to the player based on predefined probabilities, rarity constraints, and specific rules regarding card types and collections.
- Character card: Cards with a playable character, they have a rarity defined by the number of stars (1 to 5 stars) printed on the card.
- Equipment card: Cards with upgrades that can be equiped to character cards, they do not have star ratings.
- 5 stars: 0.5%
- 4 stars: 3.5%
- 3 stars: 16%
- 2 stars: 30%
- 1 star: 50%
- Each booster pack contains exactly 8 cards.
- A booster pack can only contain cards from a single collection.
- Each booster pack must have exactly one equipment card and seven character cards.
- Each booster pack is guaranteed to contain at least one character card of 3 stars or higher.
- Each booster pack can contain at most one 5-star character card.
- Each booster pack can contain up to three character cards of 3 to 5 stars.
- The remaining character cards must be of 1 or 2 stars.
- Card: Represents a card in the game.
- Collection: Represents a collection of cards and provides methods to access the cards that belongs to it.
- BoosterPackOpener: A service that defines the
open_booster_pack()method, which takes an instance of theCollectionclass as argument and returns a list of the cards assiged to the booster pack according to the described rules.
- Define the
CardandCollectionclasses. - Implement the
BoosterPackOpenerservice class to generate a booster pack of cards according to the rules. - Ensure the
open_booster_pack()method respects the minimum and maximum constraints for each rarity type and card type. - The
open_booster_pack()method should return a list of card types and rarities that make up the booster pack. - Only standard Python library packages can be used to implement the solution. No external libraries are allowed.
- The solution must include the necessary unit tests to assert the code works as expected. Tests should be written using
pytest, thepytest-mockandpytest-covlibraries are allowed to be used. Tests must cover 100% of the implemented code.
- Create a fork of this repo.
- Clone yout fork to your local development environment.
- Create a new branch were you'll implement your solution.
- Commit and push your code to the branch you have created.
- Open a Pull Request from your fork to this repository with your solution for the challenge.
- Write an explanation of your solution in the description of the Pull Request to allow reviewers to understand what you've done.
- Reviewers will add comments to your Pull Request asking specific questions of your solution, please make sure to answer comments and elaborate where necessary.