-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.js
49 lines (43 loc) · 1.88 KB
/
player.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
var basic = require('./basic.js');
const BUST_VALUE = 21;
const NUMBER_DEALER_CARDS = 2;
const _getNumberOfAvailableCardSmallerThanValue = function (value, playerCards, dealerCard) {
const nbTotalCards = value > 10 ? (10 - 1) * 4 : (value - 1) * 4;
// number of cards smaller than the value that are taken by player
const nbPlayerTakenCards = playerCards.filter(function(card){
return basic._getCardValue(card, true) < value;
}).length;
// number of card smaller than the value that is taken by dealer
const nbDealerTakenCard = dealerCard < value ? 1 : 0;
const nbDealerUnkownCard = 1;
return nbTotalCards - nbPlayerTakenCards - nbDealerTakenCard - nbDealerUnkownCard;
};
const _getIsPlayerSafeToHit = function (value, playerCards, dealerCard) {
const nbUnusedCards = basic.TOTAL_CARDS - playerCards.length - NUMBER_DEALER_CARDS;
const nbUnusedCardsSmallerThanValue = _getNumberOfAvailableCardSmallerThanValue(value, playerCards, dealerCard);
return nbUnusedCardsSmallerThanValue / nbUnusedCards > 0.5;
};
var player = function(playerCards, dealerCard) {
this.playerCards = playerCards;
this.dealerCard = dealerCard;
};
player.prototype.play = function(newCard) {
// check if player has black jack
if(basic._hasBlackJack(this.playerCards)) {
return basic.STAND;
}
// check if player will probably bust
const playerMinValue = basic._getMaxValueOfCards(this.playerCards);
const minValueForPlayerBust = BUST_VALUE - playerMinValue + 1;
const isPlayerSafeToHit = _getIsPlayerSafeToHit(minValueForPlayerBust, this.playerCards, this.dealerCard);
if (isPlayerSafeToHit) {
if(newCard !== undefined) {
this.playerCards.push(newCard);
}
return basic.HIT;
}
return basic.STAND;
};
// const playerA = new player(["1", "2"], "8");
// console.log("result=" + playerA.play("1"));
// console.log("result=" + playerA.playerCards);