-
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.
Merge pull request #1 from vmolero/develop
Push to master
- Loading branch information
Showing
31 changed files
with
1,108 additions
and
67 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 |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
|
||
# testing | ||
/coverage | ||
.vscode/chrome | ||
|
||
# production | ||
/build | ||
|
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 @@ | ||
{ | ||
"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}/*" | ||
} | ||
} | ||
] | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,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: | ||
* |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,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; | ||
} | ||
} |
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,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]; | ||
} | ||
} |
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,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; | ||
} | ||
} |
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,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; | ||
} | ||
} |
Oops, something went wrong.