Skip to content

Commit

Permalink
hand#split
Browse files Browse the repository at this point in the history
  • Loading branch information
serv committed Aug 9, 2020
1 parent b7d5fd3 commit c2da9f4
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/hand.js
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;
}
}
32 changes: 32 additions & 0 deletions test/src/hand.test.js
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");
});
});
});

0 comments on commit c2da9f4

Please sign in to comment.