Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
I
ICT100_Tutorials_T3_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
Jonathan Vitale
ICT100_Tutorials_T3_2022
Commits
6d56dfcb
Commit
6d56dfcb
authored
2 years ago
by
Jonathan Vitale
Browse files
Options
Downloads
Patches
Plain Diff
Add code for tutorial 6
parent
4a0dc8d1
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
game-logic.js
+49
-3
49 additions, 3 deletions
game-logic.js
with
49 additions
and
3 deletions
game-logic.js
+
49
−
3
View file @
6d56dfcb
...
...
@@ -33,14 +33,20 @@ function drawCard(){
}
function
handValue
(
cards
){
let
sum
=
0
;
let
aces
=
0
;
let
sum
=
cards
.
reduce
(
(
accumulator
,
item
)
=>
accumulator
+
item
);
let
aces
=
cards
.
filter
(
(
item
)
=>
item
===
11
).
length
;
/*
for (let i = 0; i < cards.length; i++){
sum = sum + cards[i];
if (cards[i] == 11){
aces++;
}
}
*/
while
(
sum
>
21
&&
aces
>
0
){
sum
=
sum
-
10
;
aces
--
;
...
...
@@ -102,21 +108,61 @@ function blackjack(){
}
}
if
(
curPlayerScore
>
21
){
console
.
log
(
`The player has busted`
);
return
false
;
}
console
.
log
(
gameState
.
cards
[
0
]);
// show the hidden card of the dealer
console
.
log
(
`The hidden card of the dealer was
${
gameState
.
hiddenCard
}
`
);
gameState
.
cards
[
1
].
push
(
gameState
.
hiddenCard
);
gameState
.
hiddenCard
=
null
;
curDealerScore
=
handValue
(
gameState
.
cards
[
1
]);
console
.
log
(
`The dealer current score is
${
curDealerScore
}
`
);
// REPEAT THIS -----
// If dealer has less than 17
// dealer draws a card
// --- UNTIL THE DEALER has 17 or more
while
(
curDealerScore
<
17
){
let
curCard
=
drawCard
();
console
.
log
(
`The dealer got a new card with value
${
curCard
}
`
);
gameState
.
cards
[
1
].
push
(
curCard
);
curDealerScore
=
handValue
(
gameState
.
cards
[
1
]);
console
.
log
(
`The dealer got a score of
${
curDealerScore
}
`
);
}
// if new score > 21 dealer loses ->
// need to pay the player 2 times the bet
// -> end the game
// --- UNTIL THE DEALER has 17 or more
if
(
curDealerScore
>
21
){
console
.
log
(
`Dealer has busted`
);
console
.
log
(
`The player wins`
);
gameState
.
playerWallet
+=
gameState
.
currentBet
*
2
;
return
true
;
}
// we need to compare player's score with dealer's score
// if player's score > dealer's score: player wins
// -> pay 2 times the bet
// end the game
if
(
curDealerScore
>
curPlayerScore
){
console
.
log
(
`The player lost`
);
return
false
;
}
else
if
(
curDealerScore
===
curPlayerScore
){
console
.
log
(
`That's a tie`
);
gameState
.
playerWallet
+=
gameState
.
currentBet
;
return
true
}
else
{
console
.
log
(
`The player wins`
);
gameState
.
playerWallet
+=
gameState
.
currentBet
*
2
;
return
true
;
}
}
blackjack
();
\ 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