Design the data structures for a generic deck of cards Explain how you would sub-class it to implement particular card games and how you would subclass the data structures to implement blackjack.
First, we need to recognize that a “generic” deck of cards can mean many things. Generic could mean a standard deck of cards that can play a poker-like game, or it could even stretch to Uno or Baseball cards.
To implement particular card games
Let’s assume that the deck is a standard 52-card set like you might see used in a blackjack or poker game. If so, the design might look like this:
The structure is clear here: a deck contains four suits and a suit contains 13 card. Each card has a numerical value from 1 to 13. If you think about a card game, different games differ from ways of dealing cards and putting cards back in. So we can have a set of abstract methods inside the class ‘Deck’ to allow sub-class implements its own way of dealing. The class diagram I draw is here:
Below is C++ implementation of the idea.
You Cards : 10 10 You All Possible Points : 20 Stand
To implement Blackjack.
Note: Now, let’s say we’re building a blackjack game, so we need to know the value of the cards. Face cards are 10 and an ace is 11 (most of the time, but that’s the job of the Hand class, not the following class).
At the start of a blackjack game, the players and the dealer receive two cards each. The players’ cards are normally dealt face up, while the dealer has one face down (called the hole card) and one face up.
The best possible blackjack hand is an opening deal of an ace with any ten-point card. This is called a “blackjack”, or a natural 21, and the player holding this automatically wins unless the dealer also has a blackjack. If a player and the dealer each have a blackjack, the result is a push for that player. If the dealer has a blackjack, all players not holding a blackjack lose.
Main logic of Blackjack in Java
This article is contributed by Mr. Somesh Awasthi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Design a data structure that supports insert, delete, search and getRandom in constant time
- Design data structures and algorithms for in-memory file system
- Design and Implement Special Stack Data Structure | Added Space Optimized Version
- The Decorator Pattern | Set 2 (Introduction and Design)
- Design a stack that supports getMin() in O(1) time and O(1) extra space
- Difference Between Architectural Style, Architectural Patterns and Design Patterns
- How to design a tiny URL or URL shortener?
- Design Patterns | Set 1 (Introduction)
- Design Patterns | Set 2 (Factory Method)
- Singleton Design Pattern | Implementation
- Decorator Pattern | Set 3 (Coding the Design)
- Flyweight Design Pattern
- Singleton Design Pattern | Introduction
- Java Singleton Design Pattern Practices with Examples
- How to design a parking lot using object-oriented principles?
- Facade Design Pattern | Introduction
- Proxy Design Pattern
- Composite Design Pattern
- Prototype Design Pattern
- Bridge Design Pattern
Improved By : AnamikaPandey2