-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
76 lines (69 loc) · 2.02 KB
/
app.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
var _ = require('lodash');
var playerInfo = require('./playerInfo');
var deckInfo = require('./deckInfo');
var bets = {};
function getPlayerPoints(player, playerName) {
var points = 0;
player.cards.forEach(function addPoints(card){
points += card.value;
});
console.log(playerName + ' has ' + points + ' points.')
return points;
};
function serveCard(player, playerName) {
console.log('player:' + playerName);
playerInfo.updatePlayerCards(playerName, deckInfo.drawCard());
}
function playTurn(player, playerName) {
var currPoints = getPlayerPoints(player, playerName);
var cardCount = deckInfo.getDeck().length;
console.log(cardCount);
while(currPoints < 17 && cardCount > 0) {
serveCard(player,playerName);
currPoints = getPlayerPoints(player, playerName);
cardCount = deckInfo.getDeck().length;
}
if(playerName === 'dealer'){
if(currPoints > 21) {
playerInfo.updatePlayerBalance(playerName, -bets[playerName]);
bets[playerName] = 0;
console.log(playerName + ' lost!');
} else {
console.log("I will figure this out later!");
}
} else {
if(currPoints === 21) {
console.log(bets[playerName]);
playerInfo.updatePlayerBalance(playerName, bets[playerName]);
bets[playerName] = 0;
console.log(playerName + ' won!');
} else if(currPoints > 21) {
console.log(bets[playerName]);
playerInfo.updatePlayerBalance(playerName, -bets[playerName]);
bets[playerName] = 0;
console.log(playerName + ' lost!');
}
}
}
function placeBets() {
_.forIn(playerInfo.getPlayerList(), function placeBet(player, playerName) {
var amount = 100;
if(playerName !== 'dealer') {
bets[playerName] = amount;
console.log(playerName + ' bet $' + amount);
}
});
deckInfo.createDeck();
console.log('The game has started');
for(var i = 0; i < 2; i++) {
_.forIn(playerInfo.getPlayerList(), serveCard);
}
_.forIn(playerInfo.getPlayerList(), playTurn);
}
function startGame() {
var playerName = process.argv[2];
playerInfo.createPlayer(playerName);
placeBets();
console.log('The game has ended');
}
startGame();