diff --git a/src/model/Hand.js b/src/model/Hand.js new file mode 100644 index 0000000..33a65ec --- /dev/null +++ b/src/model/Hand.js @@ -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); + } +} diff --git a/src/model/Hand.test.js b/src/model/Hand.test.js index 32bd2b5..3f22038 100644 --- a/src/model/Hand.test.js +++ b/src/model/Hand.test.js @@ -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', () => { @@ -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); @@ -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]) }); }); diff --git a/src/model/game.js b/src/model/game.js index 990b19f..946e450 100644 --- a/src/model/game.js +++ b/src/model/game.js @@ -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()) {