Skip to content

Commit 5b3bdc3

Browse files
committed
fix: incorrect logic when checking high card
fixes #5, #7
1 parent 1fd5ec8 commit 5b3bdc3

File tree

2 files changed

+196
-43
lines changed

2 files changed

+196
-43
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import { CardRankMetadataMap } from "../components/Card/Card";
2+
import { determineWinner, BestHand } from "./deckUtilities";
3+
describe("deckUtilities", () => {
4+
describe("determineWinner", () => {
5+
let dealerHand: BestHand;
6+
let playerHand: BestHand;
7+
8+
beforeEach(() => {
9+
dealerHand = {
10+
cards: [
11+
{
12+
card: { facing: "Up", rank: "Ace", suit: "Club" },
13+
rankRelativeValueUsed: Math.min(
14+
...CardRankMetadataMap["Ace"].relativeValues
15+
),
16+
rankValueUsed: Math.min(...CardRankMetadataMap["Ace"].values),
17+
},
18+
],
19+
score: 1,
20+
};
21+
playerHand = {
22+
cards: [
23+
{
24+
card: { facing: "Up", rank: "Ace", suit: "Diamond" },
25+
rankRelativeValueUsed: Math.min(
26+
...CardRankMetadataMap["Ace"].relativeValues
27+
),
28+
rankValueUsed: Math.min(...CardRankMetadataMap["Ace"].values),
29+
},
30+
],
31+
score: 1,
32+
};
33+
});
34+
35+
describe("Winner based on score", () => {
36+
it("Should return Dealer when dealer score is greater than player score", () => {
37+
dealerHand.score = playerHand.score + 1;
38+
const result = determineWinner(playerHand, dealerHand);
39+
expect(result.winner).toBe("Dealer");
40+
expect(result.winnerText).toBe("Dealer wins with a high score");
41+
});
42+
it("Should return Player when player score is greater than dealer score", () => {
43+
playerHand.score = dealerHand.score + 1;
44+
const result = determineWinner(playerHand, dealerHand);
45+
expect(result.winner).toBe("Player");
46+
expect(result.winnerText).toBe("Player wins with a high score");
47+
});
48+
});
49+
50+
describe("Winner based on number of cards", () => {
51+
it("Should return Dealer when dealer has more cards than player", () => {
52+
dealerHand.cards.push({
53+
card: { facing: "Up", rank: "Eight", suit: "Diamond" },
54+
rankRelativeValueUsed: 8,
55+
rankValueUsed: 8,
56+
});
57+
dealerHand.score = playerHand.score;
58+
const result = determineWinner(playerHand, dealerHand);
59+
expect(result.winner).toBe("Dealer");
60+
expect(result.winnerText).toBe("Dealer wins with the most cards");
61+
});
62+
it("Should return Player when player has more cards than player", () => {
63+
playerHand.cards.push({
64+
card: { facing: "Up", rank: "Eight", suit: "Diamond" },
65+
rankRelativeValueUsed: 8,
66+
rankValueUsed: 8,
67+
});
68+
playerHand.score = dealerHand.score;
69+
const result = determineWinner(playerHand, dealerHand);
70+
expect(result.winner).toBe("Player");
71+
expect(result.winnerText).toBe("Player wins with the most cards");
72+
});
73+
});
74+
75+
describe("Winner based on high card", () => {
76+
it("Should return Dealer when dealers high card is greater than players", () => {
77+
dealerHand.cards.push({
78+
card: { facing: "Up", rank: "Eight", suit: "Diamond" },
79+
rankRelativeValueUsed: 8,
80+
rankValueUsed: 8,
81+
});
82+
playerHand.cards.push({
83+
card: { facing: "Up", rank: "Seven", suit: "Diamond" },
84+
rankRelativeValueUsed: 7,
85+
rankValueUsed: 7,
86+
});
87+
dealerHand.score = playerHand.score;
88+
const result = determineWinner(playerHand, dealerHand);
89+
expect(result.winner).toBe("Dealer");
90+
expect(result.winnerText).toBe("Dealer wins with a high card");
91+
});
92+
it("Should return Player when player has more cards than player", () => {
93+
dealerHand.cards.push({
94+
card: { facing: "Up", rank: "Seven", suit: "Diamond" },
95+
rankRelativeValueUsed: 7,
96+
rankValueUsed: 7,
97+
});
98+
playerHand.cards.push({
99+
card: { facing: "Up", rank: "Eight", suit: "Diamond" },
100+
rankRelativeValueUsed: 8,
101+
rankValueUsed: 8,
102+
});
103+
playerHand.score = dealerHand.score;
104+
const result = determineWinner(playerHand, dealerHand);
105+
expect(result.winner).toBe("Player");
106+
expect(result.winnerText).toBe("Player wins with a high card");
107+
});
108+
});
109+
110+
it("Should return no winner when both players have same card values, count and high card", () => {
111+
const result = determineWinner(playerHand, dealerHand);
112+
expect(result.winner).toBeNull();
113+
expect(result.winnerText).toBe(
114+
"Draw! Same hand value, number of cards, and card ranks!"
115+
);
116+
});
117+
});
118+
});

src/utilities/deckUtilities.ts

Lines changed: 78 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -167,54 +167,89 @@ export const determineWinner = (
167167
playerHand: BestHand,
168168
dealerHand: BestHand
169169
): GameResult => {
170-
if (playerHand.score > dealerHand.score) {
171-
return {
172-
winner: "Player",
173-
winnerText: "Player wins with a high score",
174-
};
175-
}
176-
if (dealerHand.score > playerHand.score) {
177-
return {
178-
winner: "Dealer",
179-
winnerText: "Dealer wins with a high score",
180-
};
181-
}
182-
if (playerHand.cards.length > dealerHand.cards.length) {
183-
return {
184-
winner: "Player",
185-
winnerText: "Player wins with the most cards",
186-
};
170+
const scoreWinner = getScoreWinner(playerHand.score, dealerHand.score);
171+
172+
if (scoreWinner) {
173+
return scoreWinner;
187174
}
188-
if (dealerHand.cards.length > playerHand.cards.length) {
189-
return {
190-
winner: "Dealer",
191-
winnerText: "Dealer wins with the most cards",
192-
};
175+
176+
const cardCountWinner = getCardCountWinner(
177+
playerHand.cards,
178+
dealerHand.cards
179+
);
180+
181+
if (cardCountWinner) {
182+
return cardCountWinner;
193183
}
194-
const cardCount = playerHand.cards.length;
195-
let currentCardIndex = 0;
196-
while (currentCardIndex < cardCount) {
197-
if (
198-
playerHand.cards[currentCardIndex].rankValueUsed >
199-
dealerHand.cards[currentCardIndex].rankValueUsed
200-
) {
201-
return {
202-
winner: "Player",
203-
winnerText: "Player wins with a high card",
204-
};
205-
}
206-
if (
207-
dealerHand.cards[currentCardIndex].rankValueUsed >
208-
playerHand.cards[currentCardIndex].rankValueUsed
209-
) {
210-
return {
211-
winner: "Dealer",
212-
winnerText: "Dealer wins with a high card",
213-
};
214-
}
184+
185+
const highCardWinner = getHighCardWinner(playerHand.cards, dealerHand.cards);
186+
187+
if (highCardWinner) {
188+
return highCardWinner;
215189
}
190+
216191
return {
217192
winner: null,
218193
winnerText: "Draw! Same hand value, number of cards, and card ranks!",
219194
};
220195
};
196+
197+
const getHighestCardValue = (cards: CardValue[]) => {
198+
return cards.sort((a, b) => b.rankValueUsed - a.rankValueUsed)[0];
199+
};
200+
201+
const getHighCardWinner = (
202+
playerCards: CardValue[],
203+
dealerCards: CardValue[]
204+
): GameResult | null => {
205+
const playerHighCard = getHighestCardValue(playerCards).rankValueUsed;
206+
console.log("playerHighCard", playerHighCard);
207+
const dealerHighCard = getHighestCardValue(dealerCards).rankValueUsed;
208+
console.log("dealerHighCard", dealerHighCard);
209+
210+
const winner = getPlayerWithHighestValue(playerHighCard, dealerHighCard);
211+
if (!winner) return null;
212+
213+
return {
214+
winner,
215+
winnerText: `${winner} wins with a high card`,
216+
};
217+
};
218+
219+
const getScoreWinner = (
220+
playerScore: number,
221+
dealerScore: number
222+
): GameResult | null => {
223+
const winner = getPlayerWithHighestValue(playerScore, dealerScore);
224+
if (!winner) return null;
225+
226+
return {
227+
winner,
228+
winnerText: `${winner} wins with a high score`,
229+
};
230+
};
231+
232+
const getCardCountWinner = (
233+
playerCards: CardValue[],
234+
dealerCards: CardValue[]
235+
) => {
236+
const winner = getPlayerWithHighestValue(
237+
playerCards.length,
238+
dealerCards.length
239+
);
240+
if (!winner) return null;
241+
242+
return {
243+
winner,
244+
winnerText: `${winner} wins with the most cards`,
245+
};
246+
};
247+
248+
const getPlayerWithHighestValue = (
249+
playerValue: number,
250+
dealerValue: number
251+
): Participant | null => {
252+
if (playerValue > dealerValue) return "Player";
253+
if (dealerValue > playerValue) return "Dealer";
254+
return null;
255+
};

0 commit comments

Comments
 (0)