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
f02f42b8
Commit
f02f42b8
authored
2 years ago
by
Jonathan Vitale
Browse files
Options
Downloads
Patches
Plain Diff
Split the game logic so to fit the user interactions
parent
e766f0ba
No related branches found
Branches containing commit
Tags
tutorial-7
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
game-logic.js
+83
-5
83 additions, 5 deletions
game-logic.js
with
83 additions
and
5 deletions
game-logic.js
+
83
−
5
View file @
f02f42b8
...
...
@@ -10,10 +10,10 @@ function collectBet(){
let
isValidBet
=
false
;
//let counter = 0;
do
{
console
.
log
(
"
please provide your bet
"
)
currentBet
=
10
;
// mocking up with a value
c
onsole
.
log
(
`You provided the bet being
${
currentBet
}
`
);
if
(
currentBet
<=
0
||
currentBet
>
gameState
.
playerWallet
){
console
.
log
(
"
please provide your bet
"
)
;
let
inputBetElement
=
document
.
getElementById
(
'
player-bet-input
'
);
c
urrentBet
=
Number
(
inputBetElement
.
value
);
if
(
isNaN
(
currentBet
)
||
currentBet
<=
0
||
currentBet
>
gameState
.
playerWallet
){
console
.
log
(
'
Your bet is not valid!
'
);
//counter++;
}
else
{
...
...
@@ -55,6 +55,66 @@ function handValue(cards){
return
sum
;
}
function
resetGameState
(){
gameState
.
currentBet
=
null
;
gameState
.
cards
=
[[],
[]];
gameState
.
hiddenCard
=
null
;
}
function
newGame
(){
resetGameState
();
let
inputBetElement
=
document
.
getElementById
(
'
player-bet-input
'
);
inputBetElement
.
disabled
=
false
;
let
buttonBetElement
=
document
.
getElementById
(
'
player-bet-button
'
);
buttonBetElement
.
disabled
=
false
;
let
walletCellElement
=
document
.
getElementById
(
'
player-wallet
'
);
walletCellElement
.
innerHTML
=
gameState
.
playerWallet
;
let
buttonRunElement
=
document
.
getElementById
(
'
button-run
'
);
buttonRunElement
.
disabled
=
true
;
}
function
collectBetAndDealCards
(){
gameState
.
currentBet
=
collectBet
();
let
inputBetElement
=
document
.
getElementById
(
'
player-bet-input
'
);
inputBetElement
.
disabled
=
true
;
let
buttonBetElement
=
document
.
getElementById
(
'
player-bet-button
'
);
buttonBetElement
.
disabled
=
true
;
// 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
cardIndex
=
0
;
cardIndex
<
2
;
cardIndex
++
){
if
(
cardIndex
===
1
&&
playerIndex
===
1
){
gameState
.
hiddenCard
=
drawCard
();
}
else
{
gameState
.
cards
[
playerIndex
][
cardIndex
]
=
drawCard
();
}
}
}
let
curPlayerScore
=
handValue
(
gameState
.
cards
[
0
]);
let
curDealerScore
=
handValue
(
gameState
.
cards
[
1
]);
console
.
log
(
`Player score is
${
curPlayerScore
}
, Dealer score is
${
curDealerScore
}
`
);
let
playerScoreElement
=
document
.
getElementById
(
'
player-score
'
);
let
dealerScoreElement
=
document
.
getElementById
(
'
dealer-score
'
);
playerScoreElement
.
innerHTML
=
curPlayerScore
;
dealerScoreElement
.
innerHTML
=
curDealerScore
;
// check immediately that the player has blackjack
// if so, we need to pay 2.5 the bet -> end the game
if
(
curPlayerScore
===
21
){
console
.
log
(
'
Player has blackjack!
'
);
gameState
.
playerWallet
+=
gameState
.
currentBet
*
2.5
;
return
true
;
}
}
function
blackjack
(){
// decide the bet, collect it
gameState
.
currentBet
=
collectBet
();
...
...
@@ -165,4 +225,22 @@ function blackjack(){
}
blackjack
();
\ No newline at end of file
function
addEventsListeners
(){
let
buttonRunElement
=
document
.
getElementById
(
'
button-run
'
);
buttonRunElement
.
addEventListener
(
'
click
'
,
(
event
)
=>
{
newGame
();
}
);
let
buttonBetElement
=
document
.
getElementById
(
'
player-bet-button
'
);
buttonBetElement
.
addEventListener
(
'
click
'
,
(
event
)
=>
{
collectBetAndDealCards
();
}
);
}
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