Skip to content

Commit

Permalink
add deck cache
Browse files Browse the repository at this point in the history
  • Loading branch information
naueramant committed May 24, 2024
1 parent 7fcdfd3 commit 6af0708
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/utilities/deck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const GenerateDeck = (
return deck;
};

const GenerateShuffleIndices = (numberOfPlayers: number): number[] => {
function GenerateShuffleIndices(numberOfPlayers: number): number[] {
const numberOfIndices = numberOfPlayers * CardValues.length - 1;

const shuffleIndices = [];
Expand All @@ -39,14 +39,23 @@ const GenerateShuffleIndices = (numberOfPlayers: number): number[] => {
}

return shuffleIndices;
};
}

const deckCache = new Map<string, Card[]>();

const GetCardN = (
shuffleIndices: number[],
numberOfPlayers: number,
n: number,
): Card => {
const deck = GenerateDeck(shuffleIndices, numberOfPlayers);
const paramHash = shuffleIndices.join("") + numberOfPlayers;

let deck = deckCache.get(paramHash);
if (!deck) {
deck = GenerateDeck(shuffleIndices, numberOfPlayers);

deckCache.set(paramHash, deck);
}

return deck[n];
};
Expand Down

0 comments on commit 6af0708

Please sign in to comment.