Skip to content

Commit

Permalink
engine: Convert to typescript
Browse files Browse the repository at this point in the history
This commit converts the engine to typescript, and adds relevant types
for all relevant functions and variables.

The player type is set to any as required.

Fixes #25

Signed-off-by: Sagnik Mandal <[email protected]>
  • Loading branch information
criticic committed Jun 1, 2024
1 parent 6ba79dd commit 41a3222
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import { getShuffledCardDeck } from './deck';

const NUM_CARDS_PER_PLAYER = 7;

export class GameEngine {
cardDeck: UNOCard[];
thrownCards: UNOCard[];
players: Player[];
currentPlayerIndex: number;
lastThrownCard: UNOCard | null;
currentColor: number;
direction: number;
status: 'READY' | 'STARTED';

constructor() {
this.cardDeck = getShuffledCardDeck();
this.thrownCards = [];
Expand All @@ -18,12 +28,12 @@ export class GameEngine {
throw new Error('Not enough cards to distribute');
}

this.players = this.players.map((player) => {
this.players = this.players.map((player: Player) => {
player.cards = this.cardDeck.splice(0, NUM_CARDS_PER_PLAYER);
return player;
});
}
addPlayer(player) {
addPlayer(player: Player) {
this.players.push(player);
}
startGame() {
Expand All @@ -36,10 +46,10 @@ export class GameEngine {
this.currentPlayerIndex =
(this.currentPlayerIndex + this.direction) % this.players.length;
}
drawCardFromDeck(player) {
drawCardFromDeck(player: Player) {
//todo: Handle the case when the deck is empty and we have to move the thrown cards back to the deck
this.players
.find((p) => p.id === player.id)
.find((p: Player) => p.id === player.id)
.cards.push(this.cardDeck.pop());
}
}
2 changes: 2 additions & 0 deletions backend/uno-game-engine/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ type UNOCard = {
value: CardValue;
id: undefined;
};

type Player = any;

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

View workflow job for this annotation

GitHub Actions / eslint-backend

Unexpected any. Specify a different type

0 comments on commit 41a3222

Please sign in to comment.