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

Fix issue#1 #27

Closed
Changes from 1 commit
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
Prev Previous commit
backend:deck.ts
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
Abhishek committed Jun 2, 2024
commit 1154a98546fb72b2531f0eff86cdbe0a20fca4fa
21 changes: 0 additions & 21 deletions backend/tests/deck.test.ts
Original file line number Diff line number Diff line change
@@ -62,24 +62,3 @@ describe('shuffle function', () => {
expect(orderChanged).toBe(true);
});
});
describe('shuffle function', () => {
test('should change order of elements', () => {
// Create a mock deck
const deck = [
makeCard('number', 'red', '1'),
makeCard('number', 'blue', '2'),
makeCard('number', 'red', '1'),
makeCard('number', 'yellow', '1'),
];

// Create a copy of the deck for comparison
const originalDeck = [...deck];
shuffle(deck);

// Check that the order of elements has changed
const orderChanged = deck.some(
(card, index) => card !== originalDeck[index]
);
expect(orderChanged).toBe(true);
});
});
13 changes: 13 additions & 0 deletions backend/uno-game-engine/deck.ts
Original file line number Diff line number Diff line change
@@ -84,6 +84,13 @@ export function makeCard(
if (!sameCardCount[id]) sameCardCount[id] = 0;
sameCardCount[id]++; // increment the count of same cards to assign unique id

const uid = `${id}-${sameCardCount[id]}`;
return { type, color, value, id: uid };
const id = `card-${type}-${color}-${value}`;

if (!sameCardCount[id]) sameCardCount[id] = 0;
sameCardCount[id]++; // increment the count of same cards to assign unique id

const uid = `${id}-${sameCardCount[id]}`;
return { type, color, value, id: uid };
}
@@ -98,4 +105,10 @@ export function shuffle(deck: Array<UNOCard>) {
const j = Math.floor(Math.random() * i);
[deck[i], deck[j]] = [deck[j], deck[i]];
}
export function shuffle(deck: Array<UNOCard>) {
// Fisher-Yates shuffle algorithm to shuffle card deck
for (let i = deck.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * i);
[deck[i], deck[j]] = [deck[j], deck[i]];
}
}
You are viewing a condensed version of this merge commit. You can view the full changes here.