Skip to content

Commit

Permalink
backend:deck.ts
Browse files Browse the repository at this point in the history
This commit involves one preperatory commit : adding wild color to CardColors

Added deck generation function to initialize the deck according to given constraints

The function generates cards and push them the deck array generating cards of each color and also wild cards

Also have checked the deck composition with jest tests

Fixes :#1
  • Loading branch information
Abhishek committed Jun 2, 2024
1 parent 9cc34c4 commit b0ca51d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
38 changes: 37 additions & 1 deletion backend/tests/deck.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,45 @@
import { shuffle, makeCard } from '../uno-game-engine/deck';
import {
getShuffledCardDeck,
makeCard,
shuffle,
} from '../uno-game-engine/deck';
describe('testing deck.ts', () => {
test('makeCard', () => {
const card = makeCard('number', 'blue', '3');
expect(card.color).toBe('blue');
});
test('getShuffledDeck', () => {
const deck = getShuffledCardDeck();
console.log(deck);
});
});
test('validateDeckComposition', () => {
const deck = getShuffledCardDeck();

const colorCount = { red: 0, yellow: 0, green: 0, blue: 0 };
const wildCount = { wild: 0 };
let totalCards = 0;

for (const card of deck) {
if (card.color in colorCount) {
colorCount[card.color]++;
} else if (card.color === 'wild') {
wildCount.wild++;
}
totalCards++;
}

// Verify total number of cards
expect(totalCards).toBe(108);

// Verify number of cards of each color
expect(colorCount.red).toBe(25);
expect(colorCount.yellow).toBe(25);
expect(colorCount.green).toBe(25);
expect(colorCount.blue).toBe(25);

// Verify number of wild cards
expect(wildCount.wild).toBe(8);
});
describe('shuffle function', () => {
test('should change order of elements', () => {
Expand Down
1 change: 1 addition & 0 deletions backend/uno-game-engine/deck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const values: CardValue[] = [
'reverse',
'draw2',
];

const specialCards: CardValue[] = ['colchange', 'draw4'];
const sameCardCount = [];
/**
Expand Down

0 comments on commit b0ca51d

Please sign in to comment.