-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.js
More file actions
194 lines (178 loc) · 5.05 KB
/
code.js
File metadata and controls
194 lines (178 loc) · 5.05 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
MEC TTP 2021 Winter Assignment 2: Blackjack Game Engine
===== PURPOSE =====
- To practice implementing core javascript principles within the framing of building a blackjack card game engine.
===== MAJOR FEATURES =====
- User should be able to hit.
- User should be able to stand.
- User should be able to check status.
- User should be able to reset the game.
- User should be able to start the Blackjack game.
===== FUNCTION STUBS =====
- hit()
- stand()
- status()
- reset()
- start()
*/
//The 1's are considered aces and 10's are faces
let deck = [
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10
];
let wins = 0;
let losses = 0;
let ties = 0;
let aiHand = [];
let userHand = [];
let index = 0;
let aiTotal = 0;
let userTotal = 0;
let aceIndex = 0;
// this variable is used in .reduce() to add an array together
const reducer = (accumulator, currentValue) => accumulator + currentValue;
// returns the Blackjack game to its initial state
function reset() {
deck = [
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10
];
aiHand = [];
userHand = [];
index = 0;
aiTotal = 0;
userTotal = 0;
aceIndex = 0;
}
// displays wins, losses, and ties
function status() {
console.log("Wins: " + wins);
console.log("Losses: " + losses);
console.log("Ties: " + ties);
console.log("AI Hand: " + aiTotal)
console.log("User Hand: " + userTotal)
}
// this function should remove the card from deck once it's dealt
function cardRemove(index) {
deck.splice(index, 1);
}
//adds to player hand only!
function hit() {
///////////////////////////
index = Math.floor(Math.random()*deck.length);
aceIndex = deck[index];
console.log("Index removed: " + index + " Value: " + aceIndex);
userHand.push(aceIndex);
cardRemove(index);
///////////////////////////
userTotal = userHand.reduce(reducer);
if(userTotal + 11 <= 22){
if(aceIndex == 1){
userHand.push(10)
userTotal = aiHand.reduce(reducer);
}
}
console.log("Your current Total: " + userTotal);
if (userTotal > 21) {
losses += 1;
console.log("You bust! Game has reset! start() to play again!")
status();
reset();
}
}
// user doesn't want to hit anymore and compares their numbers with the AI for the win or loss
function stand() {
aiTotal = aiHand.reduce(reducer);
userTotal = userHand.reduce(reducer);
if (aiTotal > userTotal) {
console.log("AI wins! Game has reset! start() to play again! Winning Number: " + aiTotal)
losses += 1;
status();
reset();
}
else if (aiTotal < userTotal) {
console.log("You won! Game has reset! start() to play again! Your total is: " + userTotal)
wins += 1;
status();
reset();
}
else if(aiTotal == userTotal){
console.log("It's a tie! Game has reset! start() to play again!")
ties += 1;
status();
reset();
}
}
// starts the game
function start() {
reset();
for(let i = 0; i < 2; i++){
//assigning a random index from deck size to cutCard
index = Math.floor(Math.random()*deck.length);
//pushing the value if the cardIndex into hand
aceIndex = deck[index];
//tracks the index selected and displays the value
console.log("Index removed: " + index + " Value: " + aceIndex);
//pushing index value into aiHand
aiHand.push(aceIndex);
//finally cutting out the card!
cardRemove(index);
//adding the hand up for a total value
aiTotal = aiHand.reduce(reducer);
//condition for aces
if(aiTotal + 11 <= 22){
if(aceIndex == 1){
aiHand.push(10)
aiTotal = aiHand.reduce(reducer);
}
}
}
//AI will always hit if it's count is under 17
if(aiTotal < 17){
index = Math.floor(Math.random()*deck.length);
aceIndex = deck[index];
console.log("Index removed: " + index + " Value: " + aceIndex);
aiHand.push(aceIndex);
cardRemove(index);
aiTotal = aiHand.reduce(reducer);
if(aiTotal + 11 <= 21){
if(aceIndex == 1){
aiHand.push(10)
aiTotal = aiHand.reduce(reducer);
}
}
}
//AI will bust over 21
if(aiTotal > 21){
console.log("AI has busted! Game has been reset!");
console.log("AI's total was: " + aiTotal);
reset();
}
console.log("AI current hand: " + aiTotal);
for(let i = 0; i < 2; i++){
index = Math.floor(Math.random()*deck.length);
aceIndex = deck[index];
console.log("Index removed: " + index + " Value: " + aceIndex);
userHand.push(aceIndex);
cardRemove(index);
userTotal = userHand.reduce(reducer);
if(userTotal + 11 <= 22){
if(aceIndex == 1){
userHand.push(10)
userTotal = userHand.reduce(reducer);
}
}
}
console.log("User current hand: " + userTotal);
}
// user menu
console.log("===== Welcome to the Blackjack Game Engine =====")
console.log("Enter start() to begin the Blackjack game.")
console.log("Enter status() to check your wins, losses, ties, and hands.")
console.log("Enter hit() to draw a card from the deck.")
console.log("Enter stand() to end the match and compare hands.")
console.log("Enter reset() to restart the game.")