Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deck: completed function to create and shuffle a UNO card deck. #30

Merged
merged 1 commit into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions backend/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

type CardType = 'number' | 'special' | 'wild';

type CardColor = 'red' | 'blue' | 'green' | 'yellow';
type CardColor = 'red' | 'blue' | 'green' | 'yellow' | 'wild';

type SpecialCardNames = 'skip' | 'reverse' | 'draw2' | 'draw4' | 'colchange';
type CardNumbers = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
type SpecialCardName = 'skip' | 'reverse' | 'draw2' | 'draw4' | 'colchange';
type CardNumber = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';

type CardValue = SpecialCardNames | CardNumbers;
type CardValue = SpecialCardName | CardNumber;

type UNOCard = {
type: CardType;
Expand Down
60 changes: 59 additions & 1 deletion backend/tests/deck.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,68 @@
import { shuffle, makeCard } from '../uno-game-engine/deck';
import {
shuffle,
makeCard,
getShuffledCardDeck,
} from '../uno-game-engine/deck';

describe('getShuffledCardDeck', () => {
test('should return an array of 108 cards', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Appreciate the thorough testing!

const deck = getShuffledCardDeck();
expect(deck.length).toBe(108);
{
// Check the number of cards of each type
const numberCards = deck.filter((card) => card.type === 'number');
expect(numberCards.length).toBe(76);

const specialCards = deck.filter((card) => card.type === 'special');
expect(specialCards.length).toBe(24);

const wildCards = deck.filter((card) => card.type === 'wild');
expect(wildCards.length).toBe(8);
}
{
// Check the number of cards of red color
const redCards = deck.filter((card) => card.color === 'red');
expect(redCards.length).toBe(25); // 25 * 4 = 100 color cards
}
{
// Check the number of cards of 0 and 1 values
const zeroValueCards = deck.filter((card) => card.value === '0');
expect(zeroValueCards.length).toBe(4);

const oneValueCards = deck.filter((card) => card.value === '1');
expect(oneValueCards.length).toBe(8);
}
{
// Check the number of cards of special type
const skipCards = deck.filter((card) => card.value === 'skip');
expect(skipCards.length).toBe(8);

const draw2Cards = deck.filter((card) => card.value === 'draw2');
expect(draw2Cards.length).toBe(8);

const reverseCards = deck.filter(
(card) => card.value === 'reverse'
);
expect(reverseCards.length).toBe(8);
}
{
// Check the number of cards of wild type
const wildCards = deck.filter((card) => card.value === 'colchange');
expect(wildCards.length).toBe(4);

const draw4Cards = deck.filter((card) => card.value === 'draw4');
expect(draw4Cards.length).toBe(4);
}
});
});

describe('testing deck.ts', () => {
test('makeCard', () => {
const card = makeCard('number', 'blue', '3');
expect(card.color).toBe('blue');
});
});

describe('shuffle function', () => {
test('should change order of elements', () => {
// Create a mock deck
Expand Down
38 changes: 26 additions & 12 deletions backend/uno-game-engine/deck.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const colors: Array<CardColor> = ['red', 'yellow', 'green', 'blue'];
const values = [
const colors: Array<CardColor> = ['red', 'yellow', 'green', 'blue', 'wild'];
const numValues: Array<CardNumber> = [
'0',
'1',
'2',
Expand All @@ -10,12 +10,9 @@ const values = [
'7',
'8',
'9',
'skip',
'reverse',
'draw2',
];
const specialCards = ['wild', 'draw4'];
const deck = [];
const specialValues: Array<SpecialCardName> = ['skip', 'reverse', 'draw2'];
const wildCardValues: Array<SpecialCardName> = ['colchange', 'draw4'];
const sameCardCount = []; // to keep track of same cards in assigning unique id to each card

/**
Expand All @@ -42,11 +39,28 @@ const sameCardCount = []; // to keep track of same cards in assigning unique id
@returns {Array} deck - An array of 108 UNO cards.
*/
export function getShuffledCardDeck(): Array<UNOCard> {
const deck = [];
// todo: Implement the card generation logic
// dummy code:
// deck.push(makeCard('special', 'wild', 'wild'))
// deck.push(makeCard('number', 'red', '0'))
const deck: Array<UNOCard> = [];

colors.forEach((color) => {
if (color === 'wild') {
wildCardValues.forEach((value) => {
for (let i = 0; i < 4; i++) {
deck.push(makeCard('wild', color, value));
}
});
} else {
kuv2707 marked this conversation as resolved.
Show resolved Hide resolved
numValues.forEach((value) => {
deck.push(makeCard('number', color, value));
if (value !== '0') {
deck.push(makeCard('number', color, value));
}
});
specialValues.forEach((value) => {
deck.push(makeCard('special', color, value));
deck.push(makeCard('special', color, value));
});
}
});

shuffle(deck);
return deck;
Expand Down
Loading