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: Implemented Fisher-Yates shuffling algorithm to shuffle the deck #31

Merged
merged 1 commit into from
Jun 2, 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
23 changes: 22 additions & 1 deletion backend/tests/deck.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
import { makeCard } from '../uno-game-engine/deck';
import { shuffle, makeCard } from '../uno-game-engine/deck';
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
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);
});
Copy link
Collaborator

Choose a reason for hiding this comment

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

Great work detecting order change!

});
9 changes: 6 additions & 3 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'];

Check failure on line 1 in backend/uno-game-engine/deck.ts

View workflow job for this annotation

GitHub Actions / eslint-backend

'colors' is assigned a value but never used
const values = [

Check failure on line 2 in backend/uno-game-engine/deck.ts

View workflow job for this annotation

GitHub Actions / eslint-backend

'values' is assigned a value but never used
'0',
'1',
'2',
Expand All @@ -14,8 +14,8 @@
'reverse',
'draw2',
];
const specialCards = ['wild', 'draw4'];

Check failure on line 17 in backend/uno-game-engine/deck.ts

View workflow job for this annotation

GitHub Actions / eslint-backend

'specialCards' is assigned a value but never used
const deck = [];

Check failure on line 18 in backend/uno-game-engine/deck.ts

View workflow job for this annotation

GitHub Actions / eslint-backend

'deck' is assigned a value but never used
const sameCardCount = []; // to keep track of same cards in assigning unique id to each card

/**
Expand Down Expand Up @@ -74,7 +74,10 @@
* This function shuffles the elements of the given array *in place* . The function behaves in a type-agnostic way.
* Time complexity: O(n)
*/
export function shuffle(deck: Array<any>) {
//todo: Implement a generic shuffling algorithm
[deck[0], deck[1]] = [deck[1], deck[0]];
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]];
}
}
Loading