Skip to content

Commit

Permalink
fix(cardCollection): Fix removal of specific cards by using new objec…
Browse files Browse the repository at this point in the history
…t equality service
  • Loading branch information
mitch-b committed Sep 27, 2017
1 parent e72d765 commit e0cbab4
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/models/cardCollection/cardCollection.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
23 changes: 22 additions & 1 deletion src/models/cardCollection/cardCollection.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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[] = []) {

Expand All @@ -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 {
Expand Down Expand Up @@ -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')
}
}
}
33 changes: 33 additions & 0 deletions src/models/cardCollection/cardCollection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
})

0 comments on commit e0cbab4

Please sign in to comment.