Skip to content

Commit

Permalink
Add class Hand
Browse files Browse the repository at this point in the history
  • Loading branch information
xpmatteo committed Nov 28, 2023
1 parent 861b336 commit aca96e5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 27 deletions.
33 changes: 33 additions & 0 deletions src/model/Hand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

export class Hand {
/** @param {Card[]} cards */
constructor(cards) {
this.cards = cards.slice();
this._sortCards();
}

*[Symbol.iterator](){
for (const card of this.cards) {
yield card;
}
}

/** @param {Card} card */
play(card) {
const index = this.cards.indexOf(card);
if (-1 === index) {
throw new Error(`This hand does not contain the card ${card.name}`);
}
this.cards.splice(index, 1);
}

/** @param {Card} card */
add(card) {
this.cards.push(card);
this._sortCards();
}

_sortCards() {
this.cards.sort((a, b) => a.order - b.order);
}
}
40 changes: 14 additions & 26 deletions src/model/Hand.test.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,17 @@
import {
ORDER_2_CENTER_CARD, ORDER_2_LEFT_CARD,
ORDER_2_RIGHT_CARD, ORDER_3_CENTER_CARD,
ORDER_3_LEFT_CARD, ORDER_3_RIGHT_CARD,
ORDER_2_CENTER_CARD,
ORDER_2_LEFT_CARD,
ORDER_2_RIGHT_CARD,
ORDER_3_CENTER_CARD,
ORDER_3_LEFT_CARD,
ORDER_3_RIGHT_CARD,
ORDER_4_CENTER_CARD,
ORDER_4_LEFT_CARD,
ORDER_4_RIGHT_CARD
} from "./cards.js";
import { Deck } from "./deck.js";
import { Hand } from "./Hand.js";

class Hand {
/** @param {Card[]} cards */
constructor(cards) {
this.cards = cards.slice();
this.cards.sort((a, b) => a.order - b.order);
}

*[Symbol.iterator](){
for (const card of this.cards) {
yield card;
}
}

play(card) {
const index = this.cards.indexOf(card);
if (-1 === index) {
throw new Error(`This hand does not contain the card ${card.name}`);
}
this.cards = this.cards.splice(index, 1);
}
}

describe('A hand of cards', () => {

Expand All @@ -38,13 +21,14 @@ describe('A hand of cards', () => {
ORDER_2_LEFT_CARD, ORDER_2_CENTER_CARD, ORDER_2_RIGHT_CARD,
]);

const hand = new Hand([ORDER_4_RIGHT_CARD, ORDER_4_LEFT_CARD, ORDER_4_CENTER_CARD]);

test('enumerating card in order', () => {
const hand = new Hand([ORDER_4_RIGHT_CARD, ORDER_4_LEFT_CARD, ORDER_4_CENTER_CARD]);

expect([...hand]).toEqual([ORDER_4_LEFT_CARD, ORDER_4_CENTER_CARD, ORDER_4_RIGHT_CARD,])
});

xtest('playing a card', () => {
test('playing a card', () => {
const hand = new Hand([ORDER_4_LEFT_CARD, ORDER_4_CENTER_CARD, ORDER_4_CENTER_CARD]);

hand.play(ORDER_4_CENTER_CARD);
Expand All @@ -53,6 +37,10 @@ describe('A hand of cards', () => {
});

test('adding a card', () => {
const hand = new Hand([ORDER_4_LEFT_CARD, ORDER_4_CENTER_CARD]);

hand.add(ORDER_3_LEFT_CARD);

expect([...hand]).toEqual([ORDER_4_LEFT_CARD, ORDER_3_LEFT_CARD, ORDER_4_CENTER_CARD])
});
});
2 changes: 1 addition & 1 deletion src/model/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { Unit } from "./units.js";

/**
* @param {Scenario} scenario
* @param {{roll(number):DiceResult[]}} dice
* @param {{roll(a: number):DiceResult[]}} dice
* @returns {Game}
*/
export default function makeGame(scenario, dice = new Dice()) {
Expand Down

0 comments on commit aca96e5

Please sign in to comment.