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: used fisher-yates algorithm to shuffle deck array #13

Closed
Closed
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
51 changes: 36 additions & 15 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 numValues: Array<CardNumbers> = [
'0',
'1',
'2',
Expand All @@ -10,12 +10,10 @@
'7',
'8',
'9',
'skip',
'reverse',
'draw2',
];
const specialCards = ['wild', 'draw4'];
const deck = [];
const specialValues: Array<SpecialCardNames> = ['skip', 'reverse', 'draw2'];
const wildCardValue: Array<SpecialCardNames> = ['colchange', 'draw4'];
const deck: Array<UNOCard> = [];

Check failure on line 16 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

/**
* In a standard UNO deck, there are 108 cards. Here's the breakdown:
Expand All @@ -41,11 +39,27 @@
@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) => {
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));
});
if (color === 'black') {
wildCardValue.forEach((value) => {
for (let i = 0; i < 4; i++) {
deck.push(makeCard('wild', color, value));
}
});
}
});

shuffle(deck);
return deck;
Expand All @@ -55,7 +69,11 @@
* Helper function to make a card object.
* @returns {UNOCard} The composed UNO card.
*/
export function makeCard(type: CardType, color: CardColor, value: CardValue): UNOCard {
export function makeCard(
type: CardType,
color: CardColor,
value: CardValue
): UNOCard {
//todo: Implement unique identification of cards by assigning an id to each card
return { type, color, value, id: undefined };
}
Expand All @@ -64,7 +82,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]];
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]];
}
}
2 changes: 1 addition & 1 deletion backend/uno-game-engine/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
type CardType = 'number' | 'special' | 'wild';

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

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