Skip to content

Commit 483aebe

Browse files
committed
fix: High score check on face cards
fixes #21
1 parent ac78895 commit 483aebe

File tree

2 files changed

+14
-16
lines changed

2 files changed

+14
-16
lines changed

src/utilities/deckUtilities.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe("deckUtilities", () => {
1010
cards: [
1111
{
1212
card: { facing: "Up", rank: "Ace", suit: "Club" },
13-
rankRelativeValueUsed: Math.min(
13+
rankGlobalValueUsed: Math.min(
1414
...CardRankMetadataMap["Ace"].relativeValues
1515
),
1616
rankValueUsed: Math.min(...CardRankMetadataMap["Ace"].values),
@@ -22,7 +22,7 @@ describe("deckUtilities", () => {
2222
cards: [
2323
{
2424
card: { facing: "Up", rank: "Ace", suit: "Diamond" },
25-
rankRelativeValueUsed: Math.min(
25+
rankGlobalValueUsed: Math.min(
2626
...CardRankMetadataMap["Ace"].relativeValues
2727
),
2828
rankValueUsed: Math.min(...CardRankMetadataMap["Ace"].values),
@@ -51,7 +51,7 @@ describe("deckUtilities", () => {
5151
it("Should return Dealer when dealer has more cards than player", () => {
5252
dealerHand.cards.push({
5353
card: { facing: "Up", rank: "Eight", suit: "Diamond" },
54-
rankRelativeValueUsed: 8,
54+
rankGlobalValueUsed: 8,
5555
rankValueUsed: 8,
5656
});
5757
dealerHand.score = playerHand.score;
@@ -63,7 +63,7 @@ describe("deckUtilities", () => {
6363
it("Should return Player when player has more cards than player", () => {
6464
playerHand.cards.push({
6565
card: { facing: "Up", rank: "Eight", suit: "Diamond" },
66-
rankRelativeValueUsed: 8,
66+
rankGlobalValueUsed: 8,
6767
rankValueUsed: 8,
6868
});
6969
playerHand.score = dealerHand.score;
@@ -77,12 +77,12 @@ describe("deckUtilities", () => {
7777
it("Should return Dealer when dealers high card is greater than players", () => {
7878
dealerHand.cards.push({
7979
card: { facing: "Up", rank: "Eight", suit: "Diamond" },
80-
rankRelativeValueUsed: 8,
80+
rankGlobalValueUsed: 8,
8181
rankValueUsed: 8,
8282
});
8383
playerHand.cards.push({
8484
card: { facing: "Up", rank: "Seven", suit: "Diamond" },
85-
rankRelativeValueUsed: 7,
85+
rankGlobalValueUsed: 7,
8686
rankValueUsed: 7,
8787
});
8888
dealerHand.score = playerHand.score;
@@ -94,12 +94,12 @@ describe("deckUtilities", () => {
9494
it("Should return Player when player has more cards than player", () => {
9595
dealerHand.cards.push({
9696
card: { facing: "Up", rank: "Seven", suit: "Diamond" },
97-
rankRelativeValueUsed: 7,
97+
rankGlobalValueUsed: 7,
9898
rankValueUsed: 7,
9999
});
100100
playerHand.cards.push({
101101
card: { facing: "Up", rank: "Eight", suit: "Diamond" },
102-
rankRelativeValueUsed: 8,
102+
rankGlobalValueUsed: 8,
103103
rankValueUsed: 8,
104104
});
105105
playerHand.score = dealerHand.score;

src/utilities/deckUtilities.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export const generateDeck = (
100100
export interface CardValue {
101101
card: CardObject;
102102
rankValueUsed: number;
103-
rankRelativeValueUsed: number;
103+
rankGlobalValueUsed: number;
104104
}
105105
export interface BestHand {
106106
score: number;
@@ -125,12 +125,12 @@ export const calculateBestHand = (cards: CardObject[]): BestHand => {
125125
}
126126

127127
const cardValue = Math.max(...CardRankMetadataMap[card.rank].values);
128-
const cardRelativeValue = Math.max(
128+
const cardGlobalValue = Math.max(
129129
...CardRankMetadataMap[card.rank].relativeValues
130130
);
131131
bestHand.cards.push({
132132
card,
133-
rankRelativeValueUsed: cardRelativeValue,
133+
rankGlobalValueUsed: cardGlobalValue,
134134
rankValueUsed: cardValue,
135135
});
136136
bestHand.score += cardValue;
@@ -197,17 +197,15 @@ export const determineWinner = (
197197
};
198198

199199
const getHighestCardValue = (cards: CardValue[]) => {
200-
return cards.sort((a, b) => b.rankValueUsed - a.rankValueUsed)[0];
200+
return cards.sort((a, b) => b.rankGlobalValueUsed - a.rankGlobalValueUsed)[0];
201201
};
202202

203203
const getHighCardWinner = (
204204
playerCards: CardValue[],
205205
dealerCards: CardValue[]
206206
): GameResult | null => {
207-
const playerHighCard = getHighestCardValue(playerCards).rankValueUsed;
208-
console.log("playerHighCard", playerHighCard);
209-
const dealerHighCard = getHighestCardValue(dealerCards).rankValueUsed;
210-
console.log("dealerHighCard", dealerHighCard);
207+
const playerHighCard = getHighestCardValue(playerCards).rankGlobalValueUsed;
208+
const dealerHighCard = getHighestCardValue(dealerCards).rankGlobalValueUsed;
211209

212210
const winner = getPlayerWithHighestValue(playerHighCard, dealerHighCard);
213211
if (!winner) return null;

0 commit comments

Comments
 (0)