Card Marking System in Go
This challenge asks you to implement a simple card marking system in Go. Such a system is useful in various applications, such as card games, inventory management for collectible cards, or even tracking assets with unique identifiers. Your task is to create a system that can generate, store, and retrieve unique card markings.
Problem Description
You need to implement a CardMarker struct in Go that provides the following functionalities:
- Generate Markings: A method
GenerateMarking()that generates a unique, sequential card marking (a simple integer). The marking should start at 1 and increment with each call. - Store Markings: A method
MarkCard(cardName string)that takes a card name as input and associates it with the next available card marking. The method should store the card name and its assigned marking in an internal map. - Retrieve Markings: A method
GetMarking(cardName string)that takes a card name as input and returns the card marking associated with it. If the card name is not found, it should return -1. - Reset: A method
Reset()that resets the card marking counter to 1 and clears the internal map of card markings.
Key Requirements:
- Uniqueness: Each card marking must be unique.
- Sequential Generation: Markings should be generated sequentially.
- Error Handling: Handle the case where a card name is not found gracefully.
- Clear and Concise Code: Write clean, readable, and well-documented Go code.
Expected Behavior:
The CardMarker should maintain an internal state to track the next available marking and the mapping of card names to markings. The methods should behave as described above, ensuring uniqueness and sequential generation.
Edge Cases to Consider:
- Calling
MarkCardwith the same card name multiple times. The second call should not generate a new marking; it should return the existing marking. - Calling
GetMarkingwith a card name that has never been marked. - Calling
Reset()after cards have been marked. Subsequent calls toMarkCardshould start generating markings from 1 again. - Empty card names (consider how you want to handle this - you can choose to allow or disallow).
Examples
Example 1:
Input:
- cardMarker.GenerateMarking()
- cardMarker.MarkCard("Dragon")
- cardMarker.GenerateMarking()
- cardMarker.MarkCard("Elf")
- cardMarker.GetMarking("Dragon")
- cardMarker.GetMarking("Elf")
- cardMarker.GetMarking("Goblin")
Output:
- 1
- 2
- 3
- 4
- 2
- 3
- -1
Explanation:
The card markings are generated sequentially. "Dragon" is marked with 2, "Elf" with 3. "Goblin" is not marked, so -1 is returned.
Example 2:
Input:
- cardMarker.MarkCard("Wizard")
- cardMarker.GetMarking("Wizard")
- cardMarker.MarkCard("Wizard")
- cardMarker.GetMarking("Wizard")
Output:
- 1
- 1
- 1
- 1
Explanation:
Marking a card twice returns the same marking.
Example 3:
Input:
- cardMarker.MarkCard("Knight")
- cardMarker.GenerateMarking()
- cardMarker.MarkCard("Mage")
- cardMarker.Reset()
- cardMarker.MarkCard("Knight")
- cardMarker.GetMarking("Knight")
- cardMarker.GetMarking("Mage")
Output:
- 1
- 2
- 3
- 1
- -1
Explanation:
Resetting the card marker resets the counter and clears the mappings. "Knight" is marked with 1 again after the reset. "Mage" was not marked after the reset.
Constraints
- The card marking counter should be an integer.
- Card names are strings and can be up to 32 characters long.
- The number of cards marked should not exceed 1000. (This is to prevent excessive memory usage in testing).
- The
GenerateMarking()method should be efficient; avoid unnecessary computations.
Notes
- Consider using a
mapto store the card name to marking mappings. - Think about how to handle potential concurrency issues if this system were to be used in a multi-threaded environment (although concurrency is not required for this challenge).
- Focus on clarity and correctness. Well-structured code with comments is highly valued.
- You are free to choose your own naming conventions and coding style, but ensure it is consistent and readable.