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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Jonathan Vitale
ICT100_Tutorials_T3_2022
Compare revisions
81c0bb9642452ce70dab072126dc8d4e7bb09281 to f02f42b809ea9cc1db916afd21bcc3f44a8f84c2
Compare revisions
Changes are shown as if the
source
revision was being merged into the
target
revision.
Learn more about comparing revisions.
Source
jvitale/ict100_tutorials_t3_2022
Select target project
No results found
f02f42b809ea9cc1db916afd21bcc3f44a8f84c2
Select Git revision
Loading items
Swap
Target
jvitale/ict100_tutorials_t3_2022
Select target project
jvitale/ict100_tutorials_t3_2022
1 result
81c0bb9642452ce70dab072126dc8d4e7bb09281
Select Git revision
Loading items
Show changes
Only incoming changes from source
Include changes to target since source was created
Compare
Commits on Source (2)
Add the events listeners for the buttons
· e766f0ba
Jonathan Vitale
authored
2 years ago
e766f0ba
Split the game logic so to fit the user interactions
· f02f42b8
Jonathan Vitale
authored
2 years ago
f02f42b8
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
game-logic.js
+83
-5
83 additions, 5 deletions
game-logic.js
index.html
+1
-1
1 addition, 1 deletion
index.html
with
84 additions
and
6 deletions
game-logic.js
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.
index.html
View file @
f02f42b8
...
...
@@ -2,7 +2,7 @@
<head>
<script
type=
"text/javascript"
src=
"game-logic.js"
></script>
</head>
<body
style=
"background-color: green; color:white; font-family:Arial, Helvetica, sans-serif;"
>
<body
onload=
"addEventsListeners()"
style=
"background-color: green; color:white; font-family:Arial, Helvetica, sans-serif;"
>
<table
border=
"1"
width=
"50%"
align=
"center"
>
<tr>
<td
colspan=
"2"
>
...
...
This diff is collapsed.
Click to expand it.