Skip to content

Commit

Permalink
feat(poker): Added Poker hand scoring
Browse files Browse the repository at this point in the history
feat(poker): Added Poker hand scoring (#1)
  • Loading branch information
mitch-b committed Oct 7, 2017
1 parent 58644f8 commit a70a50f
Show file tree
Hide file tree
Showing 16 changed files with 672 additions and 94 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ A [TypeScript](https://www.typescriptlang.org/) library for playing cards.
## Getting Started

* See the [documentation](https://mitch-b.github.io/typedeck/).
* See code on [GitHub](https://github.com/mitch-b/typedeck/).
* Try it yourself!

## Features
Expand All @@ -29,9 +30,12 @@ A [TypeScript](https://www.typescriptlang.org/) library for playing cards.
* `Deck` - extension of `CardPile` with additional game-time helpers
* **Services**
* Shuffle
* Poker Hand Scoring
* **And so much more** ... see the [documentation](https://mitch-b.github.io/typedeck/) for a full list.

## Credits

* Card images by Ben Davis from the Noun Project
* This library package is built from work by Jason Dreyzehner's [typescript-starter](https://github.com/bitjson/typescript-starter) project. This was the building block for docs, code coverage, and testing (among others).
* This library package is built from work by Jason Dreyzehner's [typescript-starter](https://github.com/bitjson/typescript-starter) project. This was the building block for docs, code coverage, and testing (among others).
* Poker hand scoring from [@kequc](https://bitbucket.org/Kequc/poker-hand/)
* [How to score a poker hand in JavaScript](http://www.kequc.com/2016/07/31/how-to-score-a-poker-hand-in-javascript)
99 changes: 44 additions & 55 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"trash-cli": "^1.4.0",
"tslint": "^5.4.3",
"tslint-config-standard": "^6.0.1",
"typedoc": "^0.8.0",
"typedoc": "^0.9.0",
"typescript": "^2.4.1"
},
"keywords": [
Expand All @@ -91,7 +91,8 @@
"nyc": {
"exclude": [
"**/*.spec.js",
"build/browser/**"
"build/browser/**",
"build/**/errors/**"
]
},
"ava": {
Expand Down
5 changes: 5 additions & 0 deletions src/errors/invalidArgument.error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export class InvalidArgumentError extends Error {
constructor (message?: string) {
super(`Invalid Argument: ${message || ''}`)
}
}
5 changes: 5 additions & 0 deletions src/errors/pokerScoring.error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export class PokerScoringError extends Error {
constructor (message?: string) {
super(`Poker Scoring: ${message || ''}`)
}
}
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ export { IObjectComparer } from './common/objectComparer.interface'
export { StringifyComparer } from './common/stringifyComparer.model'
export { MapIndexable } from './common/mapIndexable.interface'
export { MapExtensions } from './common/mapExtensions.model'
export { PokerHandType } from './models/poker/pokerHandType.model'
export { InvalidArgumentError } from './errors/invalidArgument.error'
export { PokerScoringError } from './errors/pokerScoring.error'

export { ICard } from './models/card/card.interface'
export { IRankSet } from './models/card/rankSet.interface'
Expand All @@ -31,6 +34,7 @@ export { JokerCard } from './models/card/jokerCard.model'
export { RankSet } from './models/card/cardRanks.model'
export { AceHighRankSet } from './models/card/aceHighRankSet.model'
export { AceLowRankSet } from './models/card/aceLowRankSet.model'
export { PokerHandResult } from './models/poker/pokerHandResult.model'

export { CardCollection } from './models/cardCollection/cardCollection.model'
export { CardPile } from './models/cardCollection/cardPile.model'
Expand All @@ -51,3 +55,6 @@ export { ComputerPlayer } from './models/player/computerPlayer.model'

export { IndexedMap } from './common/indexedMap.model'
export { ICardImageService } from './services/cardImageService.interface'

export { IPokerScoreService } from './services/pokerScoreService.interface'
export { PokerScoreService } from './services/pokerScore.service'
25 changes: 3 additions & 22 deletions src/models/cardCollection/cardCollection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,39 +119,20 @@ test('can take multiple cards', async t => {
t.true(cardCollection.getCount() === 1)
})

test('can shuffle cards', async t => {
const staticCards: ICard[] = [
new PlayingCard(CardName.Ace, Suit.Spades),
new PlayingCard(CardName.Ace, Suit.Clubs),
new PlayingCard(CardName.Ace, Suit.Hearts),
new PlayingCard(CardName.Ace, Suit.Diamonds)
]
test('shuffles cards', async t => {
const injectedCards: ICard[] = [
new PlayingCard(CardName.Ace, Suit.Spades),
new PlayingCard(CardName.Ace, Suit.Clubs),
new PlayingCard(CardName.Ace, Suit.Hearts),
new PlayingCard(CardName.Ace, Suit.Diamonds)
]

// ensure equivalent first
const cardCollection = new CardCollection(injectedCards)
let cardsInCollection = cardCollection.getCards()
for (let i = 0; i < cardsInCollection.length; i++) {
t.deepEqual(cardsInCollection[i].getIndex(), staticCards[i].getIndex())
}
const initialLength = cardCollection.getCount()

cardCollection.shuffle()

// now prove shuffled
cardsInCollection = cardCollection.getCards()
let allEquivalent = true
for (let i = 0; i < cardsInCollection.length; i++) {
if (cardsInCollection[i].getIndex() !== staticCards[i].getIndex()) {
allEquivalent = false
break
}
}
t.false(allEquivalent)
t.true(cardCollection.getCount() === initialLength, 'After shuffling cards, card count no longer equivalent')
})

test('throws error if removing card not in collection', async t => {
Expand Down
3 changes: 2 additions & 1 deletion src/models/player/player.interface.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { IHand } from '../cardCollection/hand.interface'
import { MapIndexable } from '../../common/mapIndexable.interface'

export interface IPlayer {
export interface IPlayer extends MapIndexable {
id: string
name: string
score: number
Expand Down
4 changes: 4 additions & 0 deletions src/models/player/player.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,8 @@ export class Player implements IPlayer {
public toString (): string {
return `${this.name}`
}

public getIndex (): string {
return this.id
}
}
6 changes: 6 additions & 0 deletions src/models/player/player.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,9 @@ test('player prints out name', async t => {
const player = new Player(playerName)
t.deepEqual(player.toString(), playerName)
})

test('player id is same as index', async t => {
const playerName = 'GenericPlayerName'
const player = new Player(playerName)
t.deepEqual(player.id, player.getIndex())
})
37 changes: 37 additions & 0 deletions src/models/poker/pokerHandResult.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { PokerHandType } from './pokerHandType.model'
import { PlayingCard } from '../card/playingCard.model'

export class PokerHandResult {
/**
* Type of hand created with
* `cardsUsed`.
*/
public handType: PokerHandType
/**
* Comparable value of current hand
* to rank above or below another
* `PokerHandResult`.
*/
public value: number = 0
/**
* All cards used to determine
* result.
*/
public cards: PlayingCard[] = []
/**
* Cards in result.
*/
public cardsUsed: PlayingCard[] = []

constructor (
cards: PlayingCard[] = [],
value: number = 0) {
this.cards = cards
this.value = value
}

setHandType (type: PokerHandType): this {
this.handType = type
return this
}
}
12 changes: 12 additions & 0 deletions src/models/poker/pokerHandType.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export enum PokerHandType {
HighCard,
OnePair,
TwoPair,
ThreeOfAKind,
Straight,
Flush,
FullHouse,
FourOfAKind,
StraightFlush,
RoyalFlush
}
Loading

0 comments on commit a70a50f

Please sign in to comment.