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
08354cd1
Commit
08354cd1
authored
2 years ago
by
Jonathan Vitale
Browse files
Options
Downloads
Patches
Plain Diff
Add code for tutorial 8
parent
f02f42b8
No related branches found
Branches containing commit
Tags
tutorial-8
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
game-logic.js
+115
-101
115 additions, 101 deletions
game-logic.js
with
115 additions
and
101 deletions
game-logic.js
+
115
−
101
View file @
08354cd1
...
...
@@ -33,25 +33,27 @@ function drawCard(){
}
function
handValue
(
cards
){
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
--
;
let
sum
=
0
;
if
(
cards
.
length
>
0
)
{
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
--
;
}
}
return
sum
;
}
...
...
@@ -61,8 +63,32 @@ function resetGameState(){
gameState
.
hiddenCard
=
null
;
}
function
updateGUI
(){
let
walletElement
=
document
.
getElementById
(
'
player-wallet
'
);
walletElement
.
innerHTML
=
gameState
.
playerWallet
;
let
ids
=
[
'
player-cards
'
,
'
dealer-cards
'
];
let
scoreIds
=
[
'
player-score
'
,
'
dealer-score
'
];
for
(
let
k
=
0
;
k
<
ids
.
length
;
k
++
){
// clean all the cells of player's cards and dealer's cards
let
table
=
document
.
getElementById
(
ids
[
k
]);
let
cells
=
table
.
getElementsByTagName
(
'
td
'
);
for
(
let
i
=
0
;
i
<
cells
.
length
;
i
++
){
cells
[
i
].
innerHTML
=
'
'
;
}
// set values of player's cards and dealer's cards in each cell
for
(
let
i
=
0
;
i
<
gameState
.
cards
[
k
].
length
;
i
++
){
cells
[
i
].
innerHTML
=
gameState
.
cards
[
k
][
i
];
}
let
curScore
=
handValue
(
gameState
.
cards
[
k
]);
let
scoreElement
=
document
.
getElementById
(
scoreIds
[
k
]);
scoreElement
.
innerHTML
=
curScore
;
}
}
function
newGame
(){
resetGameState
();
updateGUI
();
log
(
'
'
);
let
inputBetElement
=
document
.
getElementById
(
'
player-bet-input
'
);
inputBetElement
.
disabled
=
false
;
let
buttonBetElement
=
document
.
getElementById
(
'
player-bet-button
'
);
...
...
@@ -76,6 +102,7 @@ function newGame(){
function
collectBetAndDealCards
(){
gameState
.
currentBet
=
collectBet
();
gameState
.
playerWallet
-=
gameState
.
currentBet
;
let
inputBetElement
=
document
.
getElementById
(
'
player-bet-input
'
);
inputBetElement
.
disabled
=
true
;
...
...
@@ -95,95 +122,52 @@ function collectBetAndDealCards(){
}
}
let
curPlayerScore
=
handValue
(
gameState
.
cards
[
0
]);
let
curDealerScore
=
handValue
(
gameState
.
cards
[
1
]);
console
.
log
(
`Player score is
${
curPlayerScore
}
, Dealer score is
${
curDealerScore
}
`
);
updateGUI
();
let
playerScoreElement
=
document
.
getElementById
(
'
player-score
'
);
let
dealerScoreElement
=
document
.
getElementById
(
'
dealer-score
'
);
playerScoreElement
.
innerHTML
=
curPlayerScore
;
dealerScoreElement
.
innerHTML
=
curDealerScore
;
let
curPlayerScore
=
handValue
(
gameState
.
cards
[
0
]);
// 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
;
log
(
'
Player has blackjack!
'
);
payBet
(
2.5
);
}
}
function
blackjack
(){
// decide the bet, collect it
gameState
.
currentBet
=
collectBet
();
let
buttonStay
=
document
.
getElementById
(
'
button-stay
'
);
buttonStay
.
disabled
=
false
;
let
buttonHit
=
document
.
getElementById
(
'
button-hit
'
);
buttonHit
.
disabled
=
false
;
}
// 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
();
}
}
}
function
log
(
message
){
let
logElement
=
document
.
getElementById
(
'
console-log
'
);
logElement
.
innerHTML
=
message
;
}
function
hit
(){
let
curCard
=
drawCard
();
gameState
.
cards
[
0
].
push
(
curCard
);
let
curPlayerScore
=
handValue
(
gameState
.
cards
[
0
]);
let
curDealerScore
=
handValue
(
gameState
.
cards
[
1
]);
console
.
log
(
`Player score is
${
curPlayerScore
}
, Dealer score is
${
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
;
}
// REPEAT THIS ------
// player makes a choice: hit or stand
// if player hits:
// we deal a new card to the player
// compute the new score
// if new score > 21 player loses -> end the game
// --- 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
;
}
}
updateGUI
()
if
(
curPlayerScore
>
21
){
console
.
log
(
`The p
layer
has
busted
`
);
return
false
;
log
(
'
P
layer busted
!
'
)
payBet
(
0
);
}
}
console
.
log
(
gameState
.
cards
[
0
]);
// show the hidden card of the dealer
console
.
log
(
`The hidden card of the dealer was
${
gameState
.
hiddenCard
}
`
);
function
stay
(){
let
buttonStay
=
document
.
getElementById
(
'
button-stay
'
);
buttonStay
.
disabled
=
true
;
let
buttonHit
=
document
.
getElementById
(
'
button-hit
'
);
buttonHit
.
disabled
=
true
;
gameState
.
cards
[
1
].
push
(
gameState
.
hiddenCard
);
gameState
.
hiddenCard
=
null
;
curDealerScore
=
handValue
(
gameState
.
cards
[
1
]);
let
curPlayerScore
=
handValue
(
gameState
.
cards
[
0
]);
let
curDealerScore
=
handValue
(
gameState
.
cards
[
1
]);
console
.
log
(
`The dealer current score is
${
curDealerScore
}
`
);
updateGUI
(
);
// REPEAT THIS -----
// If dealer has less than 17
...
...
@@ -195,15 +179,14 @@ function blackjack(){
gameState
.
cards
[
1
].
push
(
curCard
);
curDealerScore
=
handValue
(
gameState
.
cards
[
1
]);
console
.
log
(
`The dealer got a score of
${
curDealerScore
}
`
);
updateGUI
();
}
// if new score > 21 dealer loses ->
// need to pay the player 2 times the bet
// -> end the game
if
(
curDealerScore
>
21
){
console
.
log
(
`Dealer has busted`
);
console
.
log
(
`The player wins`
);
gameState
.
playerWallet
+=
gameState
.
currentBet
*
2
;
return
true
;
log
(
`Dealer has busted. The player wins.`
);
payBet
(
2
);
}
// we need to compare player's score with dealer's score
...
...
@@ -211,18 +194,33 @@ function blackjack(){
// -> pay 2 times the bet
// end the game
if
(
curDealerScore
>
curPlayerScore
){
console
.
log
(
`The player lost`
);
return
false
;
log
(
`The player lost`
);
payBet
(
0
)
;
}
else
if
(
curDealerScore
===
curPlayerScore
){
console
.
log
(
`That's a tie`
);
gameState
.
playerWallet
+=
gameState
.
currentBet
;
return
true
log
(
`That's a tie`
);
payBet
(
1
);
}
else
{
console
.
log
(
`The player wins`
);
gameState
.
playerWallet
+=
gameState
.
currentBet
*
2
;
return
true
;
log
(
`The player wins`
);
payBet
(
2
);
}
}
function
payBet
(
times
){
gameState
.
playerWallet
+=
gameState
.
currentBet
*
times
;
updateGUI
();
let
buttonBetElement
=
document
.
getElementById
(
'
player-bet-button
'
);
buttonBetElement
.
disabled
=
true
;
let
buttonStay
=
document
.
getElementById
(
'
button-stay
'
);
buttonStay
.
disabled
=
true
;
let
buttonHit
=
document
.
getElementById
(
'
button-hit
'
);
buttonHit
.
disabled
=
true
;
let
buttonRunElement
=
document
.
getElementById
(
'
button-run
'
);
buttonRunElement
.
disabled
=
false
;
}
function
addEventsListeners
(){
...
...
@@ -242,5 +240,21 @@ function addEventsListeners(){
collectBetAndDealCards
();
}
);
let
buttonStay
=
document
.
getElementById
(
'
button-stay
'
);
buttonStay
.
addEventListener
(
'
click
'
,
(
event
)
=>
{
stay
();
}
);
let
buttonHit
=
document
.
getElementById
(
'
button-hit
'
);
buttonHit
.
addEventListener
(
'
click
'
,
(
event
)
=>
{
hit
();
}
);
}
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