Skip to content

Commit

Permalink
Merge branch 'shivansh-bhatnagar18:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
PrathamX595 authored Jun 2, 2024
2 parents 9db95cc + b5100c3 commit cfeecb2
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 21 deletions.
8 changes: 7 additions & 1 deletion backend/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@
},
"plugins": ["@typescript-eslint"],
"rules": {
"indent": ["error", 4],
"indent": [
"error",
4,
{
"SwitchCase": 1
}
],
"quotes": ["error", "single"],
"semi": ["error", "always"]
}
Expand Down
6 changes: 1 addition & 5 deletions backend/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type UNOCard = {
type: CardType;
color: CardColor;
value: CardValue;
id: undefined;
id: string;
};

type Player = {
Expand All @@ -27,10 +27,6 @@ type EventResult = {
message: string;
};

declare global {
type GameEngine = import('./engine').GameEngine;
}

type GameEventType = 'DRAW_CARD' | 'THROW_CARD';

type GameEvent =
Expand Down
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);
});
});
18 changes: 9 additions & 9 deletions backend/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

/* Modules */
"module": "commonjs" /* Specify what module code is generated. */,
// "rootDir": "./", /* Specify the root folder within your source files. */
"rootDir": "./" /* Specify the root folder within your source files. */,
// "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
Expand Down Expand Up @@ -87,23 +87,23 @@
"strictNullChecks": true /* When type checking, take into account 'null' and 'undefined'. */,
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
// "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
// "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
// "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
"strictPropertyInitialization": true /* Check for class properties that are declared but not set in the constructor. */,
"noImplicitThis": true /* Enable error reporting when 'this' is given the type 'any'. */,
"useUnknownInCatchVariables": true /* Default catch clause variables as 'unknown' instead of 'any'. */,
"alwaysStrict": true /* Ensure 'use strict' is always emitted. */,
// "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
"noUnusedLocals": true /* Enable error reporting when local variables aren't read. */,
"noUnusedParameters": true /* Raise an error when a function parameter isn't read. */,
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
// "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
"noFallthroughCasesInSwitch": true /* Enable error reporting for fallthrough cases in switch statements. */
// "noUncheckedIndexedAccess": true /* Add 'undefined' to a type when accessed using an index. */
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */

/* Completeness */
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
// "skipLibCheck": true /* Skip type checking all .d.ts files. */
}
}
19 changes: 14 additions & 5 deletions backend/uno-game-engine/deck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const values = [
];
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

/**
* In a standard UNO deck, there are 108 cards. Here's the breakdown:
Expand Down Expand Up @@ -60,15 +61,23 @@ export function makeCard(
color: CardColor,
value: CardValue
): UNOCard {
//todo: Implement unique identification of cards by assigning an id to each card
return { type, color, value, id: undefined };
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 };
}

/**
* 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]];
}
}

0 comments on commit cfeecb2

Please sign in to comment.