Skip to content

Commit

Permalink
Merge pull request #1 from vmolero/develop
Browse files Browse the repository at this point in the history
Push to master
  • Loading branch information
vmolero authored Mar 5, 2018
2 parents 3193577 + f9b806e commit 260e124
Show file tree
Hide file tree
Showing 31 changed files with 1,108 additions and 67 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

# testing
/coverage
.vscode/chrome

# production
/build
Expand Down
32 changes: 32 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug Tests",
"preLaunchTask": "debugBuild",
"program": "${workspaceRoot}/node_modules/jest/bin/jest.js",
"args": [
"--runInBand",
"--config",
"${workspaceRoot}/config/jest.debug.json"
],
"sourceMaps": true,
"outFiles": [
"${workspaceRoot}/output/debug/**/*"
]
},
{
"name": "Chrome",
"type": "chrome",
"request": "launch",
"url": "http://localhost:3000",
"webRoot": "${workspaceRoot}/src",
"userDataDir": "${workspaceRoot}/.vscode/chrome",
"sourceMapPathOverrides": {
"webpack:///src/*": "${webRoot}/*"
}
}
]
}
Binary file added public/img/bj-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/img/mobile-cards.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions public/manifest.appcache
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
CACHE MANIFEST

# 30/10/2011 19:18

CACHE:
css/base.css
css/enhanced.css
js/view.js
js/blackjack.js
js/storage-polyfill.js
img/mobile-cards.png

http://html5shim.googlecode.com/svn/trunk/html5.js

FALLBACK:
/wk7/ offline.html

NETWORK:
*
28 changes: 0 additions & 28 deletions src/App.css

This file was deleted.

22 changes: 0 additions & 22 deletions src/App.tsx

This file was deleted.

62 changes: 62 additions & 0 deletions src/Board.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import Player from './Player';
import CardDeck from './CardDeck';
import Deal from './Deal';

export default class Board {
private deal: Deal;
private playerWins: number = 0;
private houseWins: number = 0;

public constructor();
public constructor(deal?: Deal) {
this.deal = deal || new Deal(CardDeck.createStandard52CardDeck());
}

public firstDeal() {
this.deal.pullPlayerCard();
this.deal.pullHouseCard();
}

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

public giveCardToPlayer() {
if (!this.deal.isDealOver()) {
this.deal.pullPlayerCard();
if (this.deal.getPlayerScore() > this.deal.SCORE_THRESHOLD) {
this.deal.setDealOver();
}
}
}

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

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

public getDeal(): Deal {
return this.deal;
}

public getPlayerWins(): number {
return this.playerWins;
}

public getHouseWins(): number {
return this.houseWins;
}
}
56 changes: 56 additions & 0 deletions src/Card.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import Suit from './Suit';

export default class Card {
readonly MIN_RANK = 1;
readonly MAX_RANK = 13;
private readonly rank: number;
private readonly suit: Suit;

constructor(rank: number, suit: Suit) {
if (typeof(Suit[suit]) !== 'string') {
throw new Error('Invalid Suit');
}
if (rank < this.MIN_RANK || rank > this.MAX_RANK) {
throw new Error('Invalid Rank');
}
this.rank = rank;
this.suit = suit;
}

public getRank(): number {
return this.rank;
}

public getRankName(): string {
if (this.rank === 1) {
return 'Ace';
} else if (this.rank === 11) {
return 'Jack';
} else if (this.rank === 12) {
return 'Queen';
} else if (this.rank === 13) {
return 'King';
} else {
return '' + this.rank;
}
}

public getFullName(): string {
return this.getRankName() + this.getSuitName();
}

public getSuit(): Suit {
return this.suit;
}

public getSuitName(): string {
return Suit[this.suit];
}

public getScore(): Array<number> {
if (this.rank === 1) {
return [1, 11];
}
return this.rank < 10 ? [this.rank] : [10];
}
}
55 changes: 55 additions & 0 deletions src/CardDeck.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import Card from './Card';
import Suit from './Suit';

export default class CardDeck {
public readonly RANKS = 13;
private deck: Array<Card>;

public static createStandard52CardDeck() {
let deck: Array<Card> = new Array<Card>();
for (let suit: Suit = Suit.Clubs; suit <= Suit.Diamonds; suit++) {
for (let rank: number = 1; rank <= 13; rank++) {
const card = new Card(rank, suit);
deck.push(card);
}
}

return new CardDeck(deck);
}

/**
* Fisher-Yates shuffle algorithm
*
* @see https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm
*/
public shuffle() {
let randomizedDeck: Array<Card> = [];
let array: Array<Card> = this.deck.slice();
while (array.length !== 0) {
let rIndex = Math.floor(array.length * Math.random());
randomizedDeck.push(array[rIndex]);
array.splice(rIndex, 1);
}
this.deck = randomizedDeck;
}

public getLength(): number {
return this.deck.length;
}

public popCard(): Card {
const card: Card | undefined = this.deck.pop();
if (card === undefined) {
throw new Error('Empty deck!');
}
return card;
}

public pushCard(card: Card) {
this.deck.push(card);
}

private constructor(deck: Array<Card>) {
this.deck = deck;
}
}
86 changes: 86 additions & 0 deletions src/Deal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
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();
}

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() {
try {
let card: Card = this.cardDeck.popCard();
this.getHouseHand().add(card);

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

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

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

0 comments on commit 260e124

Please sign in to comment.