Skip to content

Commit 3b32f51

Browse files
authored
Merge pull request careercup#1 from AmmarCSE/master
chapter 7 init and first solution
2 parents 9ea198f + 229ba29 commit 3b32f51

File tree

8 files changed

+405
-0
lines changed

8 files changed

+405
-0
lines changed

src/chapter7/q1/BlackJackCard.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import {
2+
Card
3+
} from './Card.js';
4+
5+
export class BlackJackCard extends Card {
6+
constructor(c, s) {
7+
super(c, s);
8+
}
9+
10+
value() {
11+
if (this.isAce()) {
12+
return 1;
13+
} else if (this.faceValue >= 11 && this.faceValue <= 13) {
14+
return 10;
15+
} else {
16+
return this.faceValue;
17+
}
18+
}
19+
20+
minValue() {
21+
if (this.isAce()) {
22+
return 1;
23+
} else {
24+
return this.value();
25+
}
26+
}
27+
28+
maxValue() {
29+
if (this.isAce()) {
30+
return 11;
31+
} else {
32+
return this.value();
33+
}
34+
}
35+
36+
isAce() {
37+
return this.faceValue === 1;
38+
}
39+
40+
isFaceCard() {
41+
return this.faceValue >= 11 && this.faceValue <= 13;
42+
}
43+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import {
2+
Suit
3+
} from './Suit.js';
4+
import {
5+
Deck
6+
} from './Deck.js';
7+
import {
8+
BlackJackHand
9+
} from './BlackJackHand.js';
10+
import {
11+
BlackJackCard
12+
} from './BlackJackCard.js';
13+
14+
export class BlackJackGameAutomator {
15+
constructor(numPlayers) {
16+
this.deck = undefined;
17+
18+
this.hands = [];
19+
for (let i = 0; i < numPlayers; i++) {
20+
this.hands.push(new BlackJackHand());
21+
}
22+
}
23+
24+
//underscore for psuedo private
25+
static get _HIT_UNTIL() {
26+
return 16;
27+
}
28+
29+
dealInitial() {
30+
this.hands.forEach(hand => {
31+
let card1 = this.deck.dealCard();
32+
let card2 = this.deck.dealCard();
33+
if (card1 === null || card2 === null) {
34+
return false;
35+
}
36+
hand.addCard(card1);
37+
hand.addCard(card2);
38+
});
39+
return true;
40+
}
41+
42+
getBlackJacks() {
43+
let winners = [];
44+
for (let i = 0; i < this.hands.length; i++) {
45+
if (this.hands[i].isBlackJack()) {
46+
winners.push(i);
47+
}
48+
}
49+
return winners;
50+
}
51+
52+
playHandIndex(i) {
53+
let hand = this.hands[i];
54+
return this.playHand(hand);
55+
}
56+
57+
playHand(hand) {
58+
while (hand.score() < this._HIT_UNTIL) {
59+
let card = this.deck.dealCard();
60+
if (card === null) {
61+
return false;
62+
}
63+
hand.addCard(card);
64+
}
65+
return true;
66+
}
67+
68+
playAllHands() {
69+
this.hands.forEach(hand => {
70+
if (!this.playHand(hand)) {
71+
return false;
72+
}
73+
});
74+
return true;
75+
}
76+
77+
getWinners() {
78+
let winners = [];
79+
let winningScore = 0;
80+
for (let i = 0; i < this.hands.length; i++) {
81+
let hand = this.hands[i];
82+
if (!hand.busted()) {
83+
if (hand.score() > winningScore) {
84+
winningScore = hand.score();
85+
winners = [];
86+
winners.push(i);
87+
} else if (hand.score() === winningScore) {
88+
winners.push(i);
89+
}
90+
}
91+
}
92+
return winners;
93+
}
94+
95+
initializeDeck() {
96+
let cards = [];
97+
for (let i = 1; i <= 13; i++) {
98+
for (let j = 0; j <= 3; j++) {
99+
let suit = Suit.getSuitFromValue(j);
100+
let card = new BlackJackCard(i, suit);
101+
cards.push(card);
102+
}
103+
}
104+
105+
this.deck = new Deck();
106+
this.deck.setDeckOfCards(cards);
107+
this.deck.shuffle();
108+
}
109+
110+
printHandsAndScore() {
111+
this.hands.forEach((hand, i) => {
112+
process.stdout.write('Hand ' + i + ' (' + hand.score() + '): ');
113+
hand.print();
114+
console.log('');
115+
});
116+
}
117+
}

src/chapter7/q1/BlackJackHand.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import {
2+
Hand
3+
} from './Hand.js';
4+
5+
export class BlackJackHand extends Hand {
6+
score() {
7+
let scores = this.possibleScores();
8+
let maxUnder = Number.MIN_VALUE;
9+
let minOver = Number.MAX_VALUE;
10+
for (let score of scores) {
11+
if (score > 21 && score < minOver) {
12+
minOver = score;
13+
} else if (score <= 21 && score > maxUnder) {
14+
maxUnder = score;
15+
}
16+
}
17+
return maxUnder === Number.MIN_VALUE ? minOver : maxUnder;
18+
}
19+
20+
possibleScores() {
21+
let scores = [];
22+
if (this.cards.length === 0) {
23+
return scores;
24+
}
25+
for (let card of this.cards) {
26+
this.addCardToScoreList(card, scores);
27+
}
28+
return scores;
29+
}
30+
31+
addCardToScoreList(card, scores) {
32+
if (scores.length === 0) {
33+
scores.push(0);
34+
}
35+
let length = scores.length;
36+
for (let i = 0; i < length; i++) {
37+
let score = scores[i];
38+
scores[i] = score + card.minValue();
39+
if (card.minValue() !== card.maxValue()) {
40+
scores.push(score + card.maxValue());
41+
}
42+
}
43+
}
44+
45+
busted() {
46+
return this.score() > 21;
47+
}
48+
49+
is21() {
50+
return this.score() === 21;
51+
}
52+
53+
isBlackJack() {
54+
if (this.cards.length !== 2) {
55+
return false;
56+
}
57+
let first = this.cards[0];
58+
let second = this.cards[1];
59+
return (first.isAce() && second.isFaceCard()) || (second.isAce() && first.isFaceCard());
60+
}
61+
}

src/chapter7/q1/Card.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import {
2+
Suit
3+
} from './Suit.js';
4+
5+
export class Card {
6+
constructor(c, s) {
7+
this.available = true;
8+
this.faceValue = c;
9+
this.suit = s;
10+
}
11+
12+
suit() {
13+
return this.suit;
14+
}
15+
16+
isAvailable() {
17+
return this.available;
18+
}
19+
20+
markUnavailable() {
21+
this.available = false;
22+
}
23+
24+
markAvailable() {
25+
this.available = true;
26+
}
27+
print() {
28+
let faceValues = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'];
29+
process.stdout.write(faceValues[this.faceValue - 1]);
30+
switch (this.suit) {
31+
case Suit.Club:
32+
process.stdout.write('c');
33+
break;
34+
case Suit.Heart:
35+
process.stdout.write('h');
36+
break;
37+
case Suit.Diamond:
38+
process.stdout.write('d');
39+
break;
40+
case Suit.Spade:
41+
process.stdout.write('s');
42+
break;
43+
}
44+
process.stdout.write(' ');
45+
}
46+
}

src/chapter7/q1/Deck.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
export class Deck {
2+
constructor() {
3+
this.dealtIndex = 0;
4+
this.cards = [];
5+
}
6+
7+
setDeckOfCards(deckOfCards) {
8+
this.cards = deckOfCards;
9+
}
10+
11+
randomInt(n) {
12+
return Math.round(Math.random() * n);
13+
}
14+
15+
randomIntInRange(min, max) {
16+
return this.randomInt(max + 1 - min) + min;
17+
}
18+
19+
shuffle() {
20+
for (let i = 0; i < this.cards.length; i++) {
21+
let j = this.randomIntInRange(i, this.cards.length - i - 1);
22+
23+
let card1 = this.cards[i];
24+
let card2 = this.cards[j];
25+
26+
this.cards[i] = card2;
27+
this.cards[j] = card1;
28+
}
29+
}
30+
31+
remainingCards() {
32+
return this.cards.length - this.dealtIndex;
33+
}
34+
35+
dealHand(number) {
36+
if (this.remainingCards() < number) {
37+
return null;
38+
}
39+
40+
let hand = [];
41+
let count = 0;
42+
while (count < number) {
43+
let card = this.dealCard();
44+
if (card !== null) {
45+
hand[count] = card;
46+
count++;
47+
}
48+
}
49+
50+
return hand;
51+
}
52+
53+
dealCard() {
54+
if (this.remainingCards() === 0) {
55+
return null;
56+
}
57+
let card = this.cards[this.dealtIndex];
58+
card.markUnavailable();
59+
this.dealtIndex++;
60+
61+
return card;
62+
}
63+
64+
print() {
65+
this.cards.forEach(card => card.print());
66+
}
67+
}

src/chapter7/q1/Hand.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {Card} from './Card.js';
2+
3+
export class Hand extends Card{
4+
constructor(){
5+
super();
6+
this.cards = [];
7+
}
8+
9+
score(){
10+
return this.cards.reduce((aggregate, card) => aggregate + card.value());
11+
}
12+
13+
addCard(card){
14+
this.cards.push(card);
15+
}
16+
17+
print(){
18+
this.cards.forEach(card => card.print());
19+
}
20+
}

src/chapter7/q1/Suit.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export const Suit = {
2+
Club : 0,
3+
Diamond : 1,
4+
Heart : 2,
5+
Spade : 3,
6+
getSuitFromValue: (value) => Suit[Object.keys(Suit).find(key => Suit[key] === value)]
7+
};

0 commit comments

Comments
 (0)