-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
154 lines (142 loc) · 4.2 KB
/
Copy pathscript.js
File metadata and controls
154 lines (142 loc) · 4.2 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
let makeDeck = function () {
// 1. we need 52 cards
// 2. 13 of each suit
// 3. hearts, diamonds, clubs, spades
// 4. special naming for ace, jack, queen and king
// 5.
var cardDeck = [];
// making suits dynamic
var suits = ["hearts", "diamonds", "clubs", "spades"];
// create an outer loop for the suit
var colors = ["red", "red", "black", "black"];
var outerCounter = 0;
while (outerCounter < suits.length) {
var counter = 1;
while (counter < 14) {
var card = {};
card.suits = suits[outerCounter];
card.color = colors[outerCounter];
if (counter == 1) {
card.name = "Ace";
} else if (counter == 11) {
card.name = "Jack";
} else if (counter == 12) {
card.name = "Queen";
} else if (counter == 13) {
card.name = "King";
} else {
card.name = counter;
}
cardDeck.push(card);
// console.log(card);
counter += 1;
}
outerCounter += 1;
}
return cardDeck;
};
let shuffleDeck = function (deck) {
// loop through an array to shuffle
var counter = 0;
while (counter < deck.length) {
var randomIndex = Math.floor(Math.random() * 52);
var currentCard = deck[counter];
deck[counter] = deck[randomIndex];
deck[randomIndex] = currentCard;
// counter = +1; typo
counter += 1;
}
// console.log(cardDeck);
return deck;
};
let shuffledDeck = shuffleDeck(makeDeck());
// Initialize 2 players for base version
const player = { name: "Player", hand: [] };
const dealer = { name: "Dealer", hand: [] };
//Deal 2 cards to each
function dealCard(player) {
player.hand.push(shuffledDeck.pop());
}
function printHand(hand) {
let output = "Your cards are: \n ";
for (let i = 0; i < hand.length; i++) {
output += hand[i].name + " of " + hand[i].suits + "\n";
}
return output;
}
function main(playerChoice) {
let outcome = "";
if (playerChoice == "start") {
dealCard(player);
dealCard(dealer);
dealCard(player);
dealCard(dealer);
outcome =
printHand(player.hand) +
"\n" +
"One of dealer's cards is: \n" +
dealer.hand[0].name +
" of " +
dealer.hand[0].suits;
} else if (playerChoice == "hit") {
dealCard(player);
outcome = printHand(player.hand);
} else if (playerChoice == "stand") {
// Function to calculate the score of a hand
function calculateScore(hand) {
let score = 0;
for (let card of hand) {
if (card.name == "Ace") {
score += 11;
} else if (
card.name == "King" ||
card.name == "Queen" ||
card.name == "Jack"
) {
score += 10;
} else {
score += parseInt(card.name);
}
}
return score;
}
// Function to check if a hand has a blackjack (21 with two cards)
function hasBlackjack(hand) {
return hand.length === 2 && calculateScore(hand) === 21;
}
// Function to check if a hand is bust (score above 21)
function isBust(hand) {
return calculateScore(hand) > 21;
}
console.log(printHand(dealer.hand));
console.log(printHand(player.hand));
function dealerTurn() {
var decision = "";
while (calculateScore(dealer.hand) < 17) {
dealCard(dealer);
}
let dealerScore = calculateScore(dealer.hand);
let playerScore = calculateScore(player.hand);
if (dealerScore > playerScore && dealerScore <= 21) {
decision = "Dealer wins with " + dealerScore + " points!";
} else if (hasBlackjack(dealer.hand)) {
decision = "Blackjack! Dealer wins!";
} else if (isBust(player.hand)) {
decision = "Dealer wins with " + dealerScore + " points!";
} else if (hasBlackjack(player.hand)) {
decision = "Blackjack! Player wins!";
} else if (isBust(dealer.hand)) {
decision = "Player wins with " + playerScore + " points!";
} else if (hasBlackjack(dealer.hand) && hasBlackjack(player.hand)) {
decision = "Tied Blackjack! Dealer and Player draw!";
} else {
decision = "Player wins with " + playerScore + " points!";
}
return decision;
}
outcome = dealerTurn();
player.hand = [];
dealer.hand = [];
}
return outcome;
}