-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
57 lines (52 loc) · 1.8 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const inquirer = require('inquirer')
const { dealCards, evaluateGame } = require('./game')
const { createDeck } = require('./deck')
const { shuffleDeck, hit } = require('./deal')
const { createPlayer, dealerHit } = require('./player')
const { checkScore } = require('./score')
const enterPlayerName = {
type: 'input',
name: 'name',
message: 'Enter your player name'
}
const choice = {
type: 'list',
choices: ['hit', 'stand'],
message: 'Would you like to?',
name: 'choice'
};
(async function () {
console.log('Can you beat Dealer Dan without going bust?')
const deck = shuffleDeck(createDeck())
console.log('Shuffling the deck...')
const dealer = createPlayer('Dealer Dan')
const playerName = await inquirer.prompt([enterPlayerName])
const player = createPlayer(playerName.name)
dealCards(player, deck)
dealCards(dealer, deck)
let keepAsking = true
while (keepAsking) {
const decision = await inquirer.prompt([choice])
if (decision.choice === 'stand') {
console.log(`${player.playerName} stands with a final score of ${player.currentScore.score}`)
keepAsking = false
} else {
if (player.currentScore.score < 21) {
const card = hit(deck)
player.hand.push(card)
player.currentScore = checkScore(player.hand)
console.log(`${player.playerName} recieves the ${player.hand[player.hand.length - 1].name} and now has a score of ${player.currentScore.score}.`)
}
if (player.currentScore.score === 21) {
console.log(`${player.playerName} stands with a final score of ${player.currentScore.score}`)
keepAsking = false
}
if (player.currentScore.score > 21) {
console.log(`${player.playerName} is bust.`)
keepAsking = false
}
};
}
dealerHit(dealer, deck)
evaluateGame(dealer, player)
})()