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
4a0dc8d1
Commit
4a0dc8d1
authored
2 years ago
by
Jonathan Vitale
Browse files
Options
Downloads
Patches
Plain Diff
Add code for tutorial 5
parent
2bebaeb5
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
+53
-7
53 additions, 7 deletions
game-logic.js
with
53 additions
and
7 deletions
game-logic.js
+
53
−
7
View file @
4a0dc8d1
const
gameState
=
{
const
gameState
=
{
playerWallet
:
100
,
playerWallet
:
100
,
currentBet
:
null
,
currentBet
:
null
,
cards
:
[[],
[]]
cards
:
[[],
[]],
hiddenCard
:
null
}
}
function
collectBet
(){
function
collectBet
(){
...
@@ -31,24 +32,52 @@ function drawCard(){
...
@@ -31,24 +32,52 @@ function drawCard(){
return
rndNum
;
return
rndNum
;
}
}
function
handValue
(
cards
){
let
sum
=
0
;
let
aces
=
0
;
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
--
;
}
return
sum
;
}
function
blackjack
(){
function
blackjack
(){
// decide the bet, collect it
// decide the bet, collect it
gameState
.
currentBet
=
collectBet
();
gameState
.
currentBet
=
collectBet
();
// we deal the first 2 cards for the player
// we deal the first 2 cards for the dealer
// one is hidden
for
(
let
playerIndex
=
0
;
playerIndex
<
gameState
.
cards
.
length
;
playerIndex
++
){
for
(
let
playerIndex
=
0
;
playerIndex
<
gameState
.
cards
.
length
;
playerIndex
++
){
for
(
let
cardIndex
=
0
;
cardIndex
<
2
;
cardIndex
++
){
for
(
let
cardIndex
=
0
;
cardIndex
<
2
;
cardIndex
++
){
if
(
cardIndex
===
1
&&
playerIndex
===
1
){
gameState
.
hiddenCard
=
drawCard
();
}
else
{
gameState
.
cards
[
playerIndex
][
cardIndex
]
=
drawCard
();
gameState
.
cards
[
playerIndex
][
cardIndex
]
=
drawCard
();
}
}
}
}
}
console
.
log
(
gameState
.
cards
);
let
curPlayerScore
=
handValue
(
gameState
.
cards
[
0
]);
let
curDealerScore
=
handValue
(
gameState
.
cards
[
1
]);
console
.
log
(
`Player score is
${
curPlayerScore
}
, Dealer score is
${
curDealerScore
}
`
);
// we deal the first 2 cards for the player
// check immediately that the player has blackjack
// check immediately that the player has blackjack
// if so, we need to pay 2.5 the bet -> end the game
// if so, we need to pay 2.5 the bet -> end the game
if
(
curPlayerScore
===
21
){
// we deal the first 2 cards for the dealer
console
.
log
(
'
Player has blackjack!
'
);
// one is hidden
gameState
.
playerWallet
+=
gameState
.
currentBet
*
2.5
;
return
true
;
}
// REPEAT THIS ------
// REPEAT THIS ------
// player makes a choice: hit or stand
// player makes a choice: hit or stand
...
@@ -58,6 +87,23 @@ function blackjack(){
...
@@ -58,6 +87,23 @@ function blackjack(){
// if new score > 21 player loses -> end the game
// if new score > 21 player loses -> end the game
// --- UNTIL THE PLAYER stands
// --- UNTIL THE PLAYER stands
while
(
curPlayerScore
<
21
){
let
threshold
=
(
21
-
curPlayerScore
)
/
21
;
let
likelihood
=
Math
.
random
();
if
(
likelihood
<
threshold
){
console
.
log
(
'
Player hitting a new card
'
);
let
curCard
=
drawCard
();
gameState
.
cards
[
0
].
push
(
curCard
);
curPlayerScore
=
handValue
(
gameState
.
cards
[
0
]);
console
.
log
(
`Player got
${
curCard
}
and the current score is
${
curPlayerScore
}
`
);
}
else
{
console
.
log
(
'
Player is not taking any more card
'
);
break
;
}
}
console
.
log
(
gameState
.
cards
[
0
]);
// show the hidden card of the dealer
// show the hidden card of the dealer
// REPEAT THIS -----
// REPEAT THIS -----
// If dealer has less than 17
// If dealer has less than 17
...
...
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