Skip to content

Commit

Permalink
feat(cardCollection): add helpers to reduce repeated code
Browse files Browse the repository at this point in the history
  • Loading branch information
mitch-b committed Sep 26, 2017
1 parent f9e2bb3 commit 8c9f28c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/models/cardCollection/cardCollection.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { ICard } from '../card/card.interface'

export interface ICardCollection {
getCards (): ICard[]
addCard (card: ICard): ICardCollection
addCards (cards: ICard[]): ICardCollection
removeCards (cards: ICard[]): ICardCollection
takeCard (): ICard
takeCards (amount: number): ICard[]
getCount (): number
hasCards (): boolean
shuffle (): void
}
12 changes: 10 additions & 2 deletions src/models/cardCollection/cardCollection.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export class CardCollection implements ICardCollection {

}

public addCard (card: ICard): this {
return this.addCards([card])
}

public addCards (cards: ICard[]): this {
this.getCards().unshift(...cards)
return this
Expand All @@ -42,7 +46,7 @@ export class CardCollection implements ICardCollection {
}

public takeCard (): ICard {
if (this.getCount() > 0) {
if (this.hasCards()) {
return this.getCards().shift() as ICard
}
throw new Error('No cards remaining in pile.')
Expand All @@ -58,7 +62,7 @@ export class CardCollection implements ICardCollection {
}
// tslint:disable-next-line:prefer-const
let pulledCards: ICard[] = []
while (this.getCount() > 0 && pulledCards.length < amount) {
while (this.hasCards() && pulledCards.length < amount) {
pulledCards.push(this.getCards().shift() as ICard)
}
return pulledCards
Expand All @@ -77,6 +81,10 @@ export class CardCollection implements ICardCollection {
return this.getCards().length
}

public hasCards (): boolean {
return this.getCount() > 0
}

public shuffle (): void {
this.setCards(this.shuffleService.shuffle(this.getCards()))
}
Expand Down
4 changes: 2 additions & 2 deletions src/models/cardCollection/cardPile.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ export class CardPile extends CardCollection implements ICardPile {
}

public takeCardFromBottom (): ICard {
if (this.getCount() > 0) {
if (this.hasCards()) {
return this.getCards().pop() as ICard
}
throw new Error('No cards remaining in pile.')
}

public takeCardsFromBottom (amount: number): ICard[] {
let pulledCards: ICard[] = []
while (this.getCount() > 0 && pulledCards.length < amount) {
while (this.hasCards() && pulledCards.length < amount) {
pulledCards.push(this.getCards().pop() as ICard)
}
return pulledCards
Expand Down
2 changes: 1 addition & 1 deletion src/models/cardCollection/hand.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class Hand extends CardCollection implements IHand {
*/
public sortCards (cardRank: IRankSet): this {
const cards = this.getCards()
if (cards.length === 0) {
if (!this.hasCards()) {
throw new Error('No cards to sort.')
}
if (this.suitOrder.length === 0) {
Expand Down

0 comments on commit 8c9f28c

Please sign in to comment.