banner



3d Design Deck Of Cards

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.

Become a success story instead of just reading about them. Prepare for coding interviews at Amazon and other top product-based companies with our Amazon Test Series. Includes topic-wise practice questions on all important DSA topics along with 10 practice contests of 2 hours each. Designed by industry experts that will surely help you practice and sharpen your programming skills. Wait no more, start your preparation today!

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Solution:

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.

#include <bits/stdc++.h>

using namespace std;

namespace SUIT {

enum Enum {

SPADE,

HEART,

CLUB,

DIAMOND

};

};

class Card {

private :

SUIT::Enum s;

int v;

public :

virtual SUIT::Enum suit() const

{

return s;

};

virtual int val() const

{

return v;

};

Card( int val, SUIT::Enum suit)

: s(suit), v(val){};

};

class BlackJackCard : public Card {

public :

virtual int val()

{

int v = Card::val();

if (v < 10)

return v;

return 10;

}

BlackJackCard( int val, SUIT::Enum suit)

: Card(val, suit){};

};

class player {

private :

int id;

int bet;

set< int > points;

vector<BlackJackCard*> bjcs;

bool addPoint(set< int >& amp; points, BlackJackCard * card)

{

if (points.empty()) {

points.insert(card->val());

if (card->val() == 1)

points.insert(11);

} else {

set< int > tmp;

for ( auto it = points.begin(); it != points.end(); ++it) {

tmp.insert(*it + card->val());

if (card->val() == 1)

tmp.insert(*it + 11);

}

points = tmp;

}

}

void getPoints()

{

cout << "You All Possible Points : " << endl;

for ( auto it = points.begin(); it != points.end(); ++it) {

cout << *it << endl;

}

};

int getMinPoints()

{

return *(points.begin());

};

void printCards()

{

cout << "You Cards : " << endl;

for ( auto it = bjcs.begin(); it != bjcs.end(); ++it) {

cout << (*it)->val() << endl;

}

}

public :

player( int i, int j)

: id(i), bet(j)

{

bjcs.push_back( new BlackJackCard( rand () % 13 + 1, SUIT::SPADE));

bjcs.push_back( new BlackJackCard( rand () % 13 + 1, SUIT::SPADE));

addPoint(points, bjcs[0]);

addPoint(points, bjcs[1]);

};

void getAnotherCard()

{

for (set< int >::iterator it = points.begin(); it != points.end(); ++it) {

if (*it <= 21 && 21 - *it <= 4) {

printCards();

getPoints();

cout << "Stand" << endl;

exit (1);

}

}

bjcs.push_back( new BlackJackCard( rand () % 13 + 1, SUIT::SPADE));

addPoint(points, bjcs.back());

if (getMinPoints() > 21) {

printCards();

getPoints();

cout << "Busted" << endl;

exit (2);

}

};

virtual ~player()

{

for ( auto it = bjcs.begin(); it != bjcs.end(); ++it) {

delete *it;

}

};

};

int main()

{

srand ( time (NULL));

player p(1, 1000);

p.getAnotherCard();

p.getAnotherCard();

p.getAnotherCard();

return 0;

}

Output:

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.

          Blackjack        

Main logic of Blackjack in Java

public class BlackJackHand extends Hand<BlackJackCard> {

public int score()

{

Arraylist<Integer> scores = possibleScores();

int maxUnder = Integer.MIN_VALUE;

int minOver = Integer.MAX_VALUE;

for ( int score : scores) {

if (score > 21 & amp; &score < minOver) {

minOver = score;

} else if (score <= 21 & amp; &score > maxUnder) {

maxUnder = score;

}

}

return maxUnder Integer.MIN_VALUE ? minOver maxUnder;

}

private Arraylist<Integer> possibleScores() { ... }

public boolean busted() { return score() > 21 ; }

public boolean is21() { return score() == 21 ; }

public boolean isBlackJack() { ... }

}

public class BlackJackCard extends Card {

public BlackJackCard( int c, Suit s) { super (c, s); }

public int value()

{

if (isAce())

return 1 ;

else if (faceValue >= 11 & amp; &faceValue <= 13 )

return 10 ;

else

return faceValue;

}

public int minValue()

{

if (isAce())

return 1 ;

else

return value();

}

public int maxValue()

{

if (isAce())

return 11 ;

else

return value();

}

public boolean isAce()

{

return faceValue == 1 ;

}

public boolean isFaceCard()

{

return faceValue >= 11 & amp;

&faceValue <= 13 ;

}

}

References :
https://www.careercup.com/question?id=2983
http://stackoverflow.com/questions/37363008/a-singleton-class-to-design-a-generic-deck-of-card

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 contribute@geeksforgeeks.org. 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.


3d Design Deck Of Cards

Source: https://www.geeksforgeeks.org/design-data-structuresclasses-objectsfor-generic-deck-cards/

Posted by: finksalict.blogspot.com

0 Response to "3d Design Deck Of Cards"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel