diff --git a/chapter07/7.01 - Deck of Cards/deckOfCards.js b/chapter07/7.01 - Deck of Cards/deckOfCards.js index 47a5359..6f94f11 100644 --- a/chapter07/7.01 - Deck of Cards/deckOfCards.js +++ b/chapter07/7.01 - Deck of Cards/deckOfCards.js @@ -1,116 +1,122 @@ -var Card = function(suit, number) { - this.suit = suit; - this.number = number; - this.value = `${this.number} ${this.suit}`; -}; - -var Deck = function() { - this.cards = []; - this.newDeck(); -}; - -Deck.prototype.newDeck = function() { - this.clear(); - var suits = ['\u2660', '\u2663', '\u2665', '\u2666']; - var numbers = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']; - suits.forEach((suit) => { - numbers.forEach((number) => { - this.cards.push(new Card(suit, number)); - }); - }); -}; - -Deck.prototype.clear = function() { - while (this.cards.length > 0) { - this.cards.pop(); +// Create a deck of cards +class Card { + constructor(suit, number) { + this.suit = suit + this.number = number + this.value = `${this.suit} ${this.number}` } -}; - -Deck.prototype.shuffle = function() { - this.cards.sort(() => Math.random() > 0.5 ? 1 : -1); -}; - -Deck.prototype.deal = function() { - return this.cards.pop(); -}; - -// dealer - hand and deck -var Dealer = function() { - this.deck = new Deck(); - this.hand = []; -}; +} -Dealer.prototype.shuffleCards = function() { - this.deck.shuffle(); - this.deck.shuffle(); - this.deck.shuffle(); -}; - -Dealer.prototype.dealCard = function() { - return this.deck.deal(); -}; - -Dealer.prototype.receiveCard = function(card) { - this.hand.push(card); -}; +class Deck { + constructor() { + this.cards = [] + this.newDeck() + } -var Player = function() { - this.hand = []; -}; + newDeck() { + this.clear() + let suits = ['clubs', 'hearts', 'spades', 'diamonds'] + let numbers = ['A', '2', '3','4','5','6','7','8','9','10','J','Q','K'] + suits.forEach( s => { + numbers.forEach( n => { + this.cards.push(new Card(s, n)) + }) + }) + } -Player.prototype.receiveCard = function(card) { - this.hand.push(card); -}; + shuffle() { + this.cards.sort(() => Math.random() > 0.5 ? 1 : -1) + } -Player.prototype.discardHand = function() { - this.hand = []; -}; + clear() { + while (this.cards.length > 0) { + this.cards.pop() + } + } -// blackjack game table + deal() { + return this.cards.pop() + } -var Table = function() { - this.dealer = new Dealer(); - this.players = []; -}; +} -Table.prototype.join = function(player) { - if (this.players.length > 5) { - console.log('player is full'); - } else if (this.players.indexOf(player) > -1) { - console.log('player is already on table'); - } else { - this.players.push(player); - } -}; +class Dealer { + constructor() { + this.deck = new Deck() + this.hand = [] + } + shuffleCards() { + this.deck.shuffle() + this.deck.shuffle() + this.deck.shuffle() + } + dealCard() { + return this.deck.deal() + } + receiveCard(card) { + this.hand.push(card) + } +} -Table.prototype.runGame = function() { - var dealer = this.dealer; - var players = this.players; +class Player { + constructor(name){ + this.hand = [] + this.name = name + } + receiveCard(card) { + this.hand.push(card) + } + discardHand() { + this.hand = [] + } +} - if (players.length === 0) { - console.log('no players on table: game did not take place'); - } else { - console.log('start blackjack game!'); - dealer.shuffleCards(); - for (var i = 0; i < 2; i++) { +class Table { + constructor() { + this.dealer = new Dealer() + this.players = [] + } + join(player) { + if (this.players.length > 5) { + console.log('table is full') + } + else if (this.players.indexOf(player) > -1) { + console.log('Already at table') + } + else { + this.players.push(player) + } + } + runGame() { + const dealer = this.dealer + const players = this.players + + if (players.length === 0) { + console.log("no players. Game over") + } + else { + console.log("start blackjack") + dealer.shuffleCards() + for (let i = 0; i < 2; i++) { + players.forEach( p => { + p.receiveCard(dealer.dealCard()) + }) + dealer.receiveCard(dealer.dealCard()) + } + } + console.log('dealer hand', dealer.hand.map((card) => card.value)) players.forEach((player) => { - player.receiveCard(dealer.dealCard()); - }); - dealer.receiveCard(dealer.dealCard()); - } - console.log('dealer hand', dealer.hand.map((card) => card.value)); - players.forEach((player) => { - console.log('player hand', player.hand.map((card) => card.value)); - }); - } -}; + console.log(`${player.name} hand`, player.hand.map((card) => card.value)) + }) + } +} /* TEST */ var table = new Table(); -var eugene = new Player(); -var david = new Player(); -var luis = new Player(); -var eric = new Player(); +var eugene = new Player("eugene"); +var david = new Player("david"); +var luis = new Player("luis"); +var eric = new Player("eric"); table.join(eugene); table.join(david); @@ -118,4 +124,4 @@ table.join(luis); table.join(eric); /* build until dealing of first hand */ -table.runGame(); +table.runGame(); \ No newline at end of file