How To Change The Ace Value From 11 To 1 In Blackjack Using Jquery
Design the data structures for a generic deck of cards Explicate how you would sub-class it to implement particular card games and how you would subclass the data structures to implement blackjack.
Recommended: Please endeavor your approach on {IDE} get-go, before moving on to the solution.
Solution:
First, we need to recognize that a "generic" deck of cards can hateful many things. Generic could mean a standard deck of cards that can play a poker-like game, or information technology could even stretch to Uno or Baseball cards.
To implement particular card games
Let'south assume that the deck is a standard 52-card ready similar you might see used in a blackjack or poker game. If so, the pattern might wait similar this:
The structure is clear hither: a deck contains iv suits and a suit contains thirteen card. Each card has a numerical value from 1 to 13. If you think about a card game, unlike games differ from means of dealing cards and putting cards back in. And then nosotros can have a fix of abstract methods within the class 'Deck' to permit sub-class implements its own way of dealing. The class diagram I draw is here:
Below is C++ implementation of the idea.
#include <$.25/stdc++.h>
using
namespace
std;
namespace
Suit {
enum
Enum {
SPADE,
HEART,
Order,
DIAMOND
};
};
course
Menu {
private
:
SUIT::Enum s;
int
v;
public
:
virtual
SUIT::Enum accommodate()
const
{
render
s;
};
virtual
int
val()
const
{
return
v;
};
Card(
int
val, SUIT::Enum suit)
: southward(suit), v(val){};
};
class
BlackJackCard :
public
Card {
public
:
virtual
int
val()
{
int
v = Card::val();
if
(v < 10)
return
five;
return
10;
}
BlackJackCard(
int
val, SUIT::Enum suit)
: Card(val, accommodate){};
};
class
player {
individual
:
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
(bill of fare->val() == 1)
points.insert(11);
}
else
{
prepare<
int
> tmp;
for
(
motorcar
it = points.begin(); information technology != points.end(); ++it) {
tmp.insert(*it + bill of fare->val());
if
(bill of fare->val() == 1)
tmp.insert(*it + 11);
}
points = tmp;
}
}
void
getPoints()
{
cout <<
"You All Possible Points : "
<< endl;
for
(
auto
it = points.brainstorm(); it != points.terminate(); ++it) {
cout << *information technology << endl;
}
};
int
getMinPoints()
{
return
*(points.begin());
};
void
printCards()
{
cout <<
"You Cards : "
<< endl;
for
(
auto
it = bjcs.begin(); information technology != bjcs.finish(); ++it) {
cout << (*it)->val() << endl;
}
}
public
:
player(
int
i,
int
j)
: id(i), bet(j)
{
bjcs.push_back(
new
BlackJackCard(
rand
() % thirteen + 1, Adapt::SPADE));
bjcs.push_back(
new
BlackJackCard(
rand
() % 13 + one, SUIT::SPADE));
addPoint(points, bjcs[0]);
addPoint(points, bjcs[1]);
};
void
getAnotherCard()
{
for
(set<
int
>::iterator it = points.begin(); it != points.terminate(); ++it) {
if
(*it <= 21 && 21 - *it <= 4) {
printCards();
getPoints();
cout <<
"Stand up"
<< endl;
exit
(1);
}
}
bjcs.push_back(
new
BlackJackCard(
rand
() % 13 + 1, Accommodate::SPADE));
addPoint(points, bjcs.back());
if
(getMinPoints() > 21) {
printCards();
getPoints();
cout <<
"Busted"
<< endl;
exit
(ii);
}
};
virtual
~player()
{
for
(
auto
it = bjcs.brainstorm(); it != bjcs.end(); ++it) {
delete
*it;
}
};
};
int
master()
{
srand
(
time
(Goose egg));
player p(i, 1000);
p.getAnotherCard();
p.getAnotherCard();
p.getAnotherCard();
return
0;
}
Output:
Yous Cards : 10 10 You lot All Possible Points : twenty Stand
To implement Blackjack.
Note: At present, let'south say we're edifice a blackjack game, and then nosotros need to know the value of the cards. Confront cards are ten and an ace is 11 (most of the time, but that's the job of the Mitt class, not the following class).
At the beginning of a blackjack game, the players and the dealer receive two cards each. The players' cards are normally dealt confront, while the dealer has 1 confront downwardly (called the hole menu) and ane face.
The best possible blackjack manus is an opening bargain of an ace with whatever ten-indicate carte. This is chosen a "blackjack", or a natural 21, and the actor belongings this automatically wins unless the dealer also has a blackjack. If a role player and the dealer each take a blackjack, the result is a push for that player. If the dealer has a blackjack, all players not holding a blackjack lose.
Chief logic of Blackjack in Java
public
class
BlackJackHand
extends
Mitt<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 southward) {
super
(c, s); }
public
int
value()
{
if
(isAce())
return
1
;
else
if
(faceValue >=
eleven
& amp; &faceValue <=
13
)
render
x
;
else
return
faceValue;
}
public
int
minValue()
{
if
(isAce())
return
1
;
else
return
value();
}
public
int
maxValue()
{
if
(isAce())
return
11
;
else
render
value();
}
public
boolean
isAce()
{
render
faceValue ==
1
;
}
public
boolean
isFaceCard()
{
render
faceValue >=
11
& amp;
&faceValue <=
13
;
}
}
References :
https://world wide web.careercup.com/question?id=2983
http://stackoverflow.com/questions/37363008/a-singleton-grade-to-design-a-generic-deck-of-carte du jour
This article is contributed by Mr. Somesh Awasthi. If you like GeeksforGeeks and would like to contribute, y'all can also write an article using contribute.geeksforgeeks.org or mail service your commodity to contribute@geeksforgeeks.org. See your article actualization on the GeeksforGeeks main page and help other Geeks.
Please write comments if yous find anything incorrect, or you want to share more information about the topic discussed in a higher place.
Source: https://www.geeksforgeeks.org/design-data-structuresclasses-objectsfor-generic-deck-cards/
Posted by: kingtordese.blogspot.com
0 Response to "How To Change The Ace Value From 11 To 1 In Blackjack Using Jquery"
Post a Comment