From 434c12476f200389279e7b0a48151a008f609c77 Mon Sep 17 00:00:00 2001 From: trickroll Date: Sat, 2 Apr 2022 10:08:31 -0400 Subject: [PATCH 1/2] change deckOfCards to use class constructor --- chapter07/7.01 - Deck of Cards/deckOfCards.js | 207 +++++++++--------- 1 file changed, 107 insertions(+), 100 deletions(-) diff --git a/chapter07/7.01 - Deck of Cards/deckOfCards.js b/chapter07/7.01 - Deck of Cards/deckOfCards.js index 47a5359..2aa1006 100644 --- a/chapter07/7.01 - Deck of Cards/deckOfCards.js +++ b/chapter07/7.01 - Deck of Cards/deckOfCards.js @@ -1,116 +1,123 @@ -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 +125,4 @@ table.join(luis); table.join(eric); /* build until dealing of first hand */ -table.runGame(); +table.runGame(); \ No newline at end of file From 95c1c7819510b7f21904c733925a3c3af67c160d Mon Sep 17 00:00:00 2001 From: trickroll Date: Sat, 2 Apr 2022 10:12:03 -0400 Subject: [PATCH 2/2] change deckOfCards to use class constructor --- chapter07/7.01 - Deck of Cards/deckOfCards.js | 1 - 1 file changed, 1 deletion(-) diff --git a/chapter07/7.01 - Deck of Cards/deckOfCards.js b/chapter07/7.01 - Deck of Cards/deckOfCards.js index 2aa1006..6f94f11 100644 --- a/chapter07/7.01 - Deck of Cards/deckOfCards.js +++ b/chapter07/7.01 - Deck of Cards/deckOfCards.js @@ -111,7 +111,6 @@ class Table { } } - /* TEST */ var table = new Table(); var eugene = new Player("eugene");