Skip to content

Commit e0cbab4

Browse files
committed
fix(cardCollection): Fix removal of specific cards by using new object equality service
1 parent e72d765 commit e0cbab4

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

src/models/cardCollection/cardCollection.interface.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ export interface ICardCollection {
1212
hasCard (card: ICard): boolean
1313
hasCards (cards: ICard[]): boolean
1414
shuffle (): void
15+
indexOfCard (card: ICard): number
16+
cardAtIndex (index: number): ICard
1517
}

src/models/cardCollection/cardCollection.model.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { ICard } from '../card/card.interface'
22
import { DurstenfeldShuffleService } from '../../services/shuffle.service'
33
import { ICardCollection } from './cardCollection.interface'
44
import { IShuffleService } from '../../services/shuffleService.interface'
5+
import { IObjectComparer } from '../../common/objectComparer.interface'
6+
import { StringifyComparer } from '../../common/stringifyComparer.model'
57

68
/**
79
* Basic class to represent a grouping of ICards.
@@ -19,6 +21,7 @@ export class CardCollection implements ICardCollection {
1921
*/
2022
public friendlyName: string
2123
private shuffleService: IShuffleService = new DurstenfeldShuffleService()
24+
private objectComparer: IObjectComparer = new StringifyComparer()
2225

2326
constructor (private cards: ICard[] = []) {
2427

@@ -35,7 +38,7 @@ export class CardCollection implements ICardCollection {
3538

3639
public removeCards (cards: ICard[]): this {
3740
cards.forEach((card) => {
38-
const position: number = this.getCards().indexOf(card)
41+
const position: number = this.indexOfCard(card)
3942
if (position > -1) {
4043
this.getCards().splice(position, 1)
4144
} else {
@@ -103,4 +106,22 @@ export class CardCollection implements ICardCollection {
103106
public shuffle (): void {
104107
this.setCards(this.shuffleService.shuffle(this.getCards()))
105108
}
109+
110+
public indexOfCard (card: ICard): number {
111+
for (let i = 0; i < this.getCount(); i++) {
112+
const loopCard = this.getCards()[i] as ICard
113+
if (this.objectComparer.areEquivalent(card, loopCard)) {
114+
return i
115+
}
116+
}
117+
return -1
118+
}
119+
120+
public cardAtIndex (index: number): ICard {
121+
if (index >= 0 && index <= (this.getCount() - 1)) {
122+
return this.getCards()[index] as ICard
123+
} else {
124+
throw new Error('Card collection does not contain card at index')
125+
}
126+
}
106127
}

src/models/cardCollection/cardCollection.spec.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,36 @@ test('throws error if taking more cards than available', async t => {
213213
t.deepEqual(err.message, 'No cards remaining in pile')
214214
}
215215
})
216+
217+
test('can identify index of card', async t => {
218+
const cardCollection = new CardCollection()
219+
const card1 = new PlayingCard(CardName.Ace, Suit.Spades)
220+
const card2 = new PlayingCard(CardName.Ace, Suit.Clubs)
221+
const card3 = new PlayingCard(CardName.Ace, Suit.Hearts)
222+
cardCollection.addCards([card1, card2, card3])
223+
t.true(cardCollection.indexOfCard(new PlayingCard(CardName.Ace, Suit.Spades)) === 0)
224+
t.true(cardCollection.indexOfCard(new PlayingCard(CardName.Ace, Suit.Clubs)) === 1)
225+
t.true(cardCollection.indexOfCard(new PlayingCard(CardName.Ace, Suit.Hearts)) === 2)
226+
t.true(cardCollection.indexOfCard(new PlayingCard(CardName.Queen, Suit.Hearts)) === -1)
227+
})
228+
229+
test('can return card at index', async t => {
230+
const cardCollection = new CardCollection()
231+
const card1 = new PlayingCard(CardName.Ace, Suit.Spades)
232+
const card2 = new PlayingCard(CardName.Ace, Suit.Clubs)
233+
const card3 = new PlayingCard(CardName.Ace, Suit.Hearts)
234+
cardCollection.addCards([card1, card2, card3])
235+
t.true(cardCollection.cardAtIndex(2) === card3)
236+
try {
237+
cardCollection.cardAtIndex(3)
238+
t.fail('Error should have thrown')
239+
} catch (err) {
240+
t.deepEqual(err.message, 'Card collection does not contain card at index')
241+
}
242+
try {
243+
cardCollection.cardAtIndex(-5)
244+
t.fail('Error should have thrown')
245+
} catch (err) {
246+
t.deepEqual(err.message, 'Card collection does not contain card at index')
247+
}
248+
})

0 commit comments

Comments
 (0)