Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
I
ict100_tutorials_T2_2022
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Kevin Barnes
ict100_tutorials_T2_2022
Commits
15f0f76b
Commit
15f0f76b
authored
2 years ago
by
Jon
Browse files
Options
Downloads
Patches
Plain Diff
Update code for week 5
parent
dda53987
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
game-logic.js
+94
-4
94 additions, 4 deletions
game-logic.js
with
94 additions
and
4 deletions
game-logic.js
+
94
−
4
View file @
15f0f76b
...
@@ -66,6 +66,32 @@ function handValue(totalScore, numAces){
...
@@ -66,6 +66,32 @@ function handValue(totalScore, numAces){
return
finalScore
;
return
finalScore
;
}
}
function
hitOrStay
(){
const
threshold
=
12
+
Math
.
floor
(
Math
.
random
()
*
6
);
const
currentValue
=
handValue
(
playerScore
,
playerNumAces
);
if
(
currentValue
>
threshold
){
// stay
return
false
;
}
else
{
// hit
return
true
;
}
}
function
doesDealerHit
(){
const
threshold
=
17
;
const
currentValue
=
handValue
(
dealerScore
,
dealerNumAces
);
if
(
currentValue
>=
threshold
){
// stay
return
false
;
}
else
{
// hit
return
true
;
}
}
function
newGame
(){
function
newGame
(){
// player places a bet
// player places a bet
// we need to check that the bet is valid (> 0 and < total wallet)
// we need to check that the bet is valid (> 0 and < total wallet)
...
@@ -90,21 +116,85 @@ function newGame(){
...
@@ -90,21 +116,85 @@ function newGame(){
return
true
;
return
true
;
}
}
// otherwise
// otherwise
console
.
log
(
"
Game still on
"
)
// LOOP
// LOOP
// player decides if hitting another card or sit
// player decides if hitting another card or sit
while
(
hitOrStay
()){
let
newCard
=
dealCard
();
console
.
log
(
"
You got a new card with value
"
+
newCard
);
if
(
newCard
==
11
){
playerNumAces
++
;
}
playerScore
+=
newCard
;
let
currentScore
=
handValue
(
playerScore
,
playerNumAces
);
console
.
log
(
"
Your current score is
"
+
currentScore
);
// we check the cards, if 21 automatic WINS, if more than 21 is busted (LOST)
// we check the cards, if 21 automatic WINS, if more than 21 is busted (LOST)
// LOOP ENDS if we get 21 or more than 21 OR player sits
// LOOP ENDS if we get 21 or more than 21 OR player sits
if
(
currentScore
>=
21
){
if
(
currentScore
==
21
){
// blackjack
console
.
log
(
"
You win!
"
);
playerWallet
+=
bet
*
2
;
return
true
;
}
else
{
// busted
console
.
log
(
"
You lose!
"
);
return
false
;
}
}
}
console
.
log
(
"
Game still on
"
)
// we show the dealer cards
// we show the dealer cards
console
.
log
(
"
The dealer hidden card is
"
+
hiddenCard
);
console
.
log
(
`The dealer has a score of
${
handValue
(
dealerScore
,
dealerNumAces
)}
`
);
// LOOP
// LOOP
// we check if dealer has 17 or more, if not hit a new card
// we check if dealer has 17 or more, if not hit a new card
// LOOP ENDS if the dealer has 17 or more
while
(
doesDealerHit
()){
let
newCard
=
dealCard
();
console
.
log
(
"
The dealer got a new card with value
"
+
newCard
);
if
(
newCard
==
11
){
dealerNumAces
++
;
}
dealerScore
+=
newCard
;
let
currentScore
=
handValue
(
dealerScore
,
dealerNumAces
);
console
.
log
(
"
The dealer current score is
"
+
currentScore
);
if
(
currentScore
>
21
){
// busted
console
.
log
(
"
You win!
"
);
playerWallet
+=
bet
*
2
;
return
true
;
}
}
// dealer is not busted: either player or dealer win (based on their score)
// dealer is not busted: either player or dealer win (based on their score)
// OR dealer got busted and player WIN
// OR dealer got busted and player WIN
let
playerFinalScore
=
handValue
(
playerScore
,
playerNumAces
);
console
.
log
(
"
Your final score is:
"
+
playerFinalScore
);
let
dealerFinalScore
=
handValue
(
dealerScore
,
dealerNumAces
);
console
.
log
(
"
The dealer final score is:
"
+
dealerFinalScore
);
if
(
playerFinalScore
>
dealerFinalScore
){
console
.
log
(
"
You win!
"
);
playerWallet
+=
bet
*
2
;
return
true
;
}
else
{
console
.
log
(
"
You lose!
"
);
return
false
;
}
}
}
newGame
();
newGame
();
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment