From 02342d83786f394df9869872599191ef65178ad9 Mon Sep 17 00:00:00 2001 From: mavarius Date: Sat, 24 Sep 2016 18:00:21 -0700 Subject: [PATCH] Player can stand, player or house can win the game after stand. --- src/actions/DeckActions.js | 14 ++++++++---- src/components/Table.js | 8 +++++-- src/css/stylesheet.css | 2 +- src/stores/DeckStore.js | 45 ++++++++++++++++++++++++++++++++++---- 4 files changed, 58 insertions(+), 11 deletions(-) diff --git a/src/actions/DeckActions.js b/src/actions/DeckActions.js index 9bce0d0..ee38122 100644 --- a/src/actions/DeckActions.js +++ b/src/actions/DeckActions.js @@ -1,15 +1,21 @@ import AppDispatcher from '../AppDispatcher'; const DeckActions = { - shuffle(deck) { + shuffle() { AppDispatcher.dispatch({ type: 'SHUFFLE_DECK' }) }, - - hitMe(deck) { + + hitMe() { + AppDispatcher.dispatch({ + type: 'HIT_ME' + }) + }, + + stand() { AppDispatcher.dispatch({ - type: 'HIT_ME', + type: 'STAND' }) } } diff --git a/src/components/Table.js b/src/components/Table.js index 107eb5d..1d407b6 100644 --- a/src/components/Table.js +++ b/src/components/Table.js @@ -34,6 +34,10 @@ export default class Table extends Component { DeckActions.hitMe(); } + _stand() { + DeckActions.stand(); + } + _onChange() { this.setState( DeckStore.getAll() ) } @@ -53,8 +57,8 @@ export default class Table extends Component {
- { player.total > 0 && player.total <= 21 ? : } - { player.total > 0 && player.total <= 21 ? : } + { player.total > 0 && player.total <= 21 && dealer.hidden.length !== 0 ? : } + { player.total > 0 && player.total <= 21 && dealer.hidden.length !== 0 ? : }
) diff --git a/src/css/stylesheet.css b/src/css/stylesheet.css index 45a860d..e35114f 100644 --- a/src/css/stylesheet.css +++ b/src/css/stylesheet.css @@ -54,7 +54,7 @@ h2 { } .playerWell { - width: 400px; + width: 500px; } .playingCard span { diff --git a/src/stores/DeckStore.js b/src/stores/DeckStore.js index a25b184..f1460bf 100644 --- a/src/stores/DeckStore.js +++ b/src/stores/DeckStore.js @@ -9,7 +9,7 @@ let _player = { hand: [], total: 0, message: 'Player' }; let _playDeck = []; let _deck = [ - {suit: 'spades', value: [11, 1], img: '🂡'}, + {suit: 'spades', value: [1, 11], img: '🂡'}, {suit: 'spades', value: 2, img: '🂢'}, {suit: 'spades', value: 3, img: '🂣'}, {suit: 'spades', value: 4, img: '🂤'}, @@ -22,7 +22,7 @@ let _deck = [ {suit: 'spades', value: 10, img: '🂫'}, {suit: 'spades', value: 10, img: '🂭'}, {suit: 'spades', value: 10, img: '🂮'}, - {suit: 'hearts', value: [11, 1], img: '🂱'}, + {suit: 'hearts', value: [1, 11], img: '🂱'}, {suit: 'hearts', value: 2, img: '🂲'}, {suit: 'hearts', value: 3, img: '🂳'}, {suit: 'hearts', value: 4, img: '🂴'}, @@ -35,7 +35,7 @@ let _deck = [ {suit: 'hearts', value: 10, img: '🂻'}, {suit: 'hearts', value: 10, img: '🂽'}, {suit: 'hearts', value: 10, img: '🂾'}, - {suit: 'diamonds', value: [11, 1], img: '🃁'}, + {suit: 'diamonds', value: [1, 11], img: '🃁'}, {suit: 'diamonds', value: 2, img: '🃂'}, {suit: 'diamonds', value: 3, img: '🃃'}, {suit: 'diamonds', value: 4, img: '🃄'}, @@ -48,7 +48,7 @@ let _deck = [ {suit: 'diamonds', value: 10, img: '🃋'}, {suit: 'diamonds', value: 10, img: '🃍'}, {suit: 'diamonds', value: 10, img: '🃎'}, - {suit: 'clubs', value: [11, 1], img: '🃑'}, + {suit: 'clubs', value: [1, 11], img: '🃑'}, {suit: 'clubs', value: 2, img: '🃒'}, {suit: 'clubs', value: 3, img: '🃓'}, {suit: 'clubs', value: 4, img: '🃔'}, @@ -82,6 +82,7 @@ class DeckStore extends EventEmitter { case 'SHUFFLE_DECK': _playDeck = lodash.shuffle(_deck); _dealer.hand = []; + _dealer.hidden = []; _player.hand = []; _dealer.hidden.push(_playDeck.pop()); @@ -106,10 +107,46 @@ class DeckStore extends EventEmitter { if (_player.total > 21 ) { _player.message = `BUST! ${_player.total}`; + _dealer.message = `House Wins!`; } else { _player.message = `Player: ${_player.total}`; } + this.emit('CHANGE'); + break; + + case 'STAND': + _dealer.hand.push(_dealer.hidden.pop()); + _dealer.total = getTotal(_dealer); + _dealer.message = `Dealer: ${_dealer.total}`; + + if (_dealer.total > 21) { + _player.message = `You Win! ${_player.total}`; + _dealer.message = `BUST! ${_dealer.total}`; + } else if (_dealer.total === 21) { + if (_player.total === 21) { + _player.message = `PUSH ${_player.total}`; + _dealer.message = `PUSH ${_dealer.total}`; + } else { + _dealer.message = `House Wins! ${_dealer.total}`; + } + } else if (_dealer.total < 17) { + while (_dealer.total < 21) { + _dealer.hand.push(_playDeck.pop()); + _dealer.total = getTotal(_dealer); + _dealer.message = `Dealer: ${_dealer.total}`; + this.emit('CHANGE'); + } + if (_dealer.total < _player.total) { + _player.message = `You Win! ${_player.total}`; + } else if (_dealer.total > 21) { + _player.message = `You Win! ${_player.total}`; + _dealer.message = `BUST! ${_dealer.total}`; + } else { + _dealer.message = `House Wins! ${_dealer.total}`; + } + } + this.emit('CHANGE'); break; }