diff --git a/src/models/cardCollection/cardCollection.interface.ts b/src/models/cardCollection/cardCollection.interface.ts index b33ecaa..b1c6aca 100644 --- a/src/models/cardCollection/cardCollection.interface.ts +++ b/src/models/cardCollection/cardCollection.interface.ts @@ -12,4 +12,6 @@ export interface ICardCollection { hasCard (card: ICard): boolean hasCards (cards: ICard[]): boolean shuffle (): void + indexOfCard (card: ICard): number + cardAtIndex (index: number): ICard } diff --git a/src/models/cardCollection/cardCollection.model.ts b/src/models/cardCollection/cardCollection.model.ts index 4bfc169..3103aad 100644 --- a/src/models/cardCollection/cardCollection.model.ts +++ b/src/models/cardCollection/cardCollection.model.ts @@ -2,6 +2,8 @@ import { ICard } from '../card/card.interface' import { DurstenfeldShuffleService } from '../../services/shuffle.service' import { ICardCollection } from './cardCollection.interface' import { IShuffleService } from '../../services/shuffleService.interface' +import { IObjectComparer } from '../../common/objectComparer.interface' +import { StringifyComparer } from '../../common/stringifyComparer.model' /** * Basic class to represent a grouping of ICards. @@ -19,6 +21,7 @@ export class CardCollection implements ICardCollection { */ public friendlyName: string private shuffleService: IShuffleService = new DurstenfeldShuffleService() + private objectComparer: IObjectComparer = new StringifyComparer() constructor (private cards: ICard[] = []) { @@ -35,7 +38,7 @@ export class CardCollection implements ICardCollection { public removeCards (cards: ICard[]): this { cards.forEach((card) => { - const position: number = this.getCards().indexOf(card) + const position: number = this.indexOfCard(card) if (position > -1) { this.getCards().splice(position, 1) } else { @@ -103,4 +106,22 @@ export class CardCollection implements ICardCollection { public shuffle (): void { this.setCards(this.shuffleService.shuffle(this.getCards())) } + + public indexOfCard (card: ICard): number { + for (let i = 0; i < this.getCount(); i++) { + const loopCard = this.getCards()[i] as ICard + if (this.objectComparer.areEquivalent(card, loopCard)) { + return i + } + } + return -1 + } + + public cardAtIndex (index: number): ICard { + if (index >= 0 && index <= (this.getCount() - 1)) { + return this.getCards()[index] as ICard + } else { + throw new Error('Card collection does not contain card at index') + } + } } diff --git a/src/models/cardCollection/cardCollection.spec.ts b/src/models/cardCollection/cardCollection.spec.ts index 132bee8..ad30504 100644 --- a/src/models/cardCollection/cardCollection.spec.ts +++ b/src/models/cardCollection/cardCollection.spec.ts @@ -213,3 +213,36 @@ test('throws error if taking more cards than available', async t => { t.deepEqual(err.message, 'No cards remaining in pile') } }) + +test('can identify index of card', async t => { + const cardCollection = new CardCollection() + const card1 = new PlayingCard(CardName.Ace, Suit.Spades) + const card2 = new PlayingCard(CardName.Ace, Suit.Clubs) + const card3 = new PlayingCard(CardName.Ace, Suit.Hearts) + cardCollection.addCards([card1, card2, card3]) + t.true(cardCollection.indexOfCard(new PlayingCard(CardName.Ace, Suit.Spades)) === 0) + t.true(cardCollection.indexOfCard(new PlayingCard(CardName.Ace, Suit.Clubs)) === 1) + t.true(cardCollection.indexOfCard(new PlayingCard(CardName.Ace, Suit.Hearts)) === 2) + t.true(cardCollection.indexOfCard(new PlayingCard(CardName.Queen, Suit.Hearts)) === -1) +}) + +test('can return card at index', async t => { + const cardCollection = new CardCollection() + const card1 = new PlayingCard(CardName.Ace, Suit.Spades) + const card2 = new PlayingCard(CardName.Ace, Suit.Clubs) + const card3 = new PlayingCard(CardName.Ace, Suit.Hearts) + cardCollection.addCards([card1, card2, card3]) + t.true(cardCollection.cardAtIndex(2) === card3) + try { + cardCollection.cardAtIndex(3) + t.fail('Error should have thrown') + } catch (err) { + t.deepEqual(err.message, 'Card collection does not contain card at index') + } + try { + cardCollection.cardAtIndex(-5) + t.fail('Error should have thrown') + } catch (err) { + t.deepEqual(err.message, 'Card collection does not contain card at index') + } +})