-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
export default class Hand { | ||
constructor({ type }) { | ||
this.type = type; | ||
this.cards = []; | ||
} | ||
|
||
isDealer() { | ||
return this.type === "dealer"; | ||
} | ||
|
||
isPlayer() { | ||
return this.type === "player"; | ||
} | ||
|
||
take(card) { | ||
this.cards.push(card); | ||
} | ||
|
||
split() { | ||
if ( | ||
!( | ||
this.type === "player" && | ||
this.cards.length === 2 && | ||
this.cards[0].name === this.cards[1].name | ||
) | ||
) { | ||
throw new Error("failed to split"); | ||
} | ||
|
||
const popped = this.cards.pop(); | ||
|
||
const splitted = new Hand({ type: "player" }); | ||
splitted.take(popped); | ||
|
||
return splitted; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { expect } from "chai"; | ||
import Hand from "./../../src/hand"; | ||
import Card from "./../../src/card"; | ||
|
||
describe("Hand", () => { | ||
describe("#split", () => { | ||
const hand = new Hand({ type: "player" }); | ||
|
||
it("success with a pair", () => { | ||
const card1 = new Card({ suite: "hearts", name: "10" }); | ||
const card2 = new Card({ suite: "diamonds", name: "10" }); | ||
|
||
hand.take(card1); | ||
hand.take(card2); | ||
|
||
const splitted = hand.split(); | ||
|
||
expect(hand.cards.length).to.eql(1); | ||
expect(splitted.cards.length).to.eql(1); | ||
}); | ||
|
||
it("fail with a non-pair", () => { | ||
const card1 = new Card({ suite: "hearts", name: "10" }); | ||
const card2 = new Card({ suite: "diamonds", name: "9" }); | ||
|
||
hand.take(card1); | ||
hand.take(card2); | ||
|
||
expect(() => hand.split()).to.throw("failed to split"); | ||
}); | ||
}); | ||
}); |