Skip to content

Commit

Permalink
refactor to inmutable objects
Browse files Browse the repository at this point in the history
  • Loading branch information
vmolero committed Mar 6, 2018
1 parent fa0ef71 commit e20fc99
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 231 deletions.
85 changes: 69 additions & 16 deletions src/Board.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,56 @@
import Player from './Player';
import CardDeck from './CardDeck';
import Deal from './Deal';
import House from './House';
import Hand from './Hand';
import Card from './Card';
import CardDeck from './CardDeck';

export default class Board {
private readonly SCORE_THRESHOLD: number = 21;

private deal: Deal;
private playerWins: number = 0;
private houseWins: number = 0;

public constructor();
public constructor(deal?: Deal) {
this.deal = deal || new Deal(CardDeck.createStandard52CardDeck());
private house: House;
private player1: Player;

public static newGame(): Board {
let deal: Deal = new Deal(CardDeck.createStandard52CardDeck());
let playerHand: Hand = new Hand([]);
let player1: Player = new Player(playerHand);
let houseHand: Hand = new Hand([]);
let house: House = new House(houseHand);

return new Board(deal, house, player1);
}

public firstDeal() {
this.deal.start();
public newDeal(): Board {
let deal: Deal = new Deal(CardDeck.createStandard52CardDeck());
let playerHand: Hand = new Hand([deal.pullCard()]);
let player1: Player = new Player(playerHand);
let houseHand: Hand = new Hand([deal.pullCard()]);
let house: House = new House(houseHand);

return new Board(deal, house, player1);
}

public addPlayer(player: Player) {
this.deal.addPlayer(player);
}
public dealPlayer(): Board {
let card: Card = this.deal.pullCard();
let player1: Player = this.player1.hit(card);

public giveCardToPlayer() {
this.deal.dealPlayer();
return new Board(this.deal, this.house, player1);
}

public giveCardToHouse() {
this.deal.dealHouse();
public dealHouse() {
let card: Card = this.deal.pullCard();
let house: Player = this.house.hit(card);

return new Board(this.deal, house, this.player1);
}

public isHouseWinning() {
return (this.deal.getHouseScore() >= this.deal.getPlayerScore() ||
this.deal.getPlayerScore() > 21) && this.deal.getHouseScore() <= 21;
return (this.getHouseScore() >= this.getPlayerScore() ||
this.getPlayerScore() > 21) && this.getHouseScore() <= 21;
}

public getDeal(): Deal {
Expand All @@ -44,4 +64,37 @@ export default class Board {
public getHouseWins(): number {
return this.houseWins;
}

public getHouse(): House {
return this.house;
}

public getPlayerHand(): Hand {
return this.player1.getHand();
}

public getHouseHand(): Hand {
return this.house.getHand();
}

public getPlayerScore(): number {
return this.getPlayerHand().getScore();
}

public getHouseScore(): number {
return this.house.getScore();
}

public isGameOver(): boolean {
return this.getHouseScore() >= this.SCORE_THRESHOLD ||
this.getPlayerScore() > this.SCORE_THRESHOLD ||
(this.getHouseScore() >= this.getPlayerScore() &&
this.house.getHand().getCards().length > 1);
}

private constructor(deal: Deal, house: House, player1: Player) {
this.deal = deal;
this.house = house;
this.player1 = player1;
}
}
106 changes: 3 additions & 103 deletions src/Deal.ts
Original file line number Diff line number Diff line change
@@ -1,119 +1,19 @@
import Hand from './Hand';
import Card from './Card';
import CardDeck from './CardDeck';
import House from './House';
import Player from './Player';

export default class Deal {
public readonly SCORE_THRESHOLD: number = 21;

// private cardCount: number = 0;
private dealOver: boolean = false;
private house: House;
private players: Array<Player>;
private cardDeck: CardDeck;

public constructor(cardDeck?: CardDeck) {
this.cardDeck = cardDeck || CardDeck.createStandard52CardDeck();
this.house = new House();
this.players = new Array<Player>();
this.cardDeck.shuffle();
this.dealOver = false;
}

public start() {
this.setDealOn();
this.pullPlayerCard();
this.pullHouseCard();
}

public setHouse(house: House): Deal {
this.house = house;

return this;
}

public getHouse(): House {
return this.house;
}

public addPlayer(player: Player) {
this.players.push(player);
}

public getPlayer(order: number): Player {
if (this.players[order - 1] !== undefined) {
return this.players[order - 1];
}
throw new Error('Player not found');
}

public getPlayerHand(): Hand {
return this.getPlayer(1).getHand();
}

public getHouseHand(): Hand {
return this.house.getHand();
}

public getPlayerScore(): number {
return this.getPlayerHand().getScore();
}

public getHouseScore(): number {
return this.house.getScore();
}

public pullPlayerCard() {
try {
let card: Card = this.cardDeck.popCard();
this.getPlayer(1).pullCard(card);
} catch (doNothingOnEmptyDeck) {
this.dealOver = true;
}
}

public pullHouseCard() {
public pullCard(): Card {
try {
let card: Card = this.cardDeck.popCard();
this.getHouseHand().add(card);

return this.cardDeck.popCard();
} catch (doNothingOnEmptyDeck) {
this.dealOver = true;
}
}

public setDealOver() {
this.dealOver = true;
}

public setDealOn() {
this.dealOver = false;
}

public isDealOver(): boolean {
return this.dealOver;
}

public dealHouse() {
if (this.getHouseScore() >= this.getPlayerScore()) {
this.setDealOver();
}
if (!this.isDealOver()) {
this.pullHouseCard();
if (this.getHouseScore() >= this.SCORE_THRESHOLD ||
(this.getHouseScore() >= this.getPlayerScore())) {
this.setDealOver();
}
}
}

public dealPlayer() {
if (!this.isDealOver()) {
this.pullPlayerCard();
if (this.getPlayerScore() > this.SCORE_THRESHOLD) {
this.setDealOver();
}
throw Error('No cards');
}
}
}
1 change: 0 additions & 1 deletion src/Hand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import Card from './Card';
export default class Hand {
private cards: Array<Card>;

public constructor();
public constructor(cards?: Array<Card>) {
this.cards = cards || new Array<Card>();
}
Expand Down
11 changes: 6 additions & 5 deletions src/Player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import Card from './Card';
export default class Player {
private hand: Hand;

public constructor();
public constructor(hand?: Hand) {
this.hand = hand || new Hand();
public constructor(hand: Hand) {
this.hand = hand;
}

public pullCard(card: Card) {
this.hand.add(card);
public hit(card: Card): Player {
let cards: Array<Card> = this.hand.getCards();
cards.push(card);
return new Player(new Hand(cards));
}

public getScore(): number {
Expand Down
Loading

0 comments on commit e20fc99

Please sign in to comment.