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
694a9cab
Commit
694a9cab
authored
2 years ago
by
Jonathan Vitale
Browse files
Options
Downloads
Patches
Plain Diff
Final code for the game
parent
89904609
Branches
master
Tags
tutorial-9
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
game-logic.js
+87
-9
87 additions, 9 deletions
game-logic.js
with
87 additions
and
9 deletions
game-logic.js
+
87
−
9
View file @
694a9cab
const
gameState
=
{
const
gameState
=
{
deck
:
null
,
playerWallet
:
100
,
playerWallet
:
100
,
currentBet
:
null
,
currentBet
:
null
,
cards
:
[[],
[]],
cards
:
[[],
[]],
hiddenCard
:
null
hiddenCard
:
null
}
}
class
Card
{
constructor
(
face
,
suit
){
this
.
face
=
face
;
this
.
suit
=
suit
;
this
.
visible
=
true
;
}
setCardVisibility
(
visibility
){
this
.
visible
=
visibility
;
}
getValue
(){
if
(
this
.
visible
===
true
){
if
(
!
isNaN
(
Number
(
this
.
face
))){
return
Number
(
this
.
face
);
}
else
if
(
this
.
face
===
'
ace
'
){
return
11
;
}
else
{
return
10
;
}
}
else
{
return
0
;
}
}
draw
(){
if
(
this
.
visible
){
return
`<img style="background-color: white; width: 70px;" src="cards/
${
this
.
face
}
_of_
${
this
.
suit
}
.png" />`
;
}
else
{
return
`<img style="background-color: white; width: 70px;" src="cards/back.png" />`
}
}
}
class
Deck
{
constructor
(){
this
.
buildDeck
();
this
.
shuffle
();
}
buildDeck
(){
this
.
deck
=
[];
let
suits
=
[
'
hearts
'
,
'
clubs
'
,
'
spades
'
,
'
diamonds
'
];
let
faces
=
[
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
,
'
jack
'
,
'
queen
'
,
'
king
'
,
'
ace
'
];
for
(
let
i
=
0
;
i
<
suits
.
length
;
i
++
){
for
(
let
j
=
0
;
j
<
faces
.
length
;
j
++
){
this
.
deck
.
push
(
new
Card
(
faces
[
j
],
suits
[
i
]));
}
}
}
shuffle
(){
/* Randomize array in-place using Durstenfeld shuffle algorithm */
for
(
let
i
=
this
.
deck
.
length
-
1
;
i
>
0
;
i
--
)
{
let
j
=
Math
.
floor
(
Math
.
random
()
*
(
i
+
1
));
let
temp
=
this
.
deck
[
i
];
this
.
deck
[
i
]
=
this
.
deck
[
j
];
this
.
deck
[
j
]
=
temp
;
}
}
dealCard
(){
let
newCard
=
this
.
deck
.
pop
();
if
(
this
.
deck
.
length
<=
26
){
this
.
buildDeck
();
this
.
shuffle
();
}
return
newCard
;
}
}
function
collectBet
(){
function
collectBet
(){
let
currentBet
;
let
currentBet
;
let
isValidBet
=
false
;
let
isValidBet
=
false
;
...
@@ -32,8 +104,12 @@ function drawCard(){
...
@@ -32,8 +104,12 @@ function drawCard(){
return
rndNum
;
return
rndNum
;
}
}
function
handValue
(
cards
){
function
handValue
(
cards
Obj
){
let
sum
=
0
;
let
sum
=
0
;
let
cards
=
[];
for
(
let
i
=
0
;
i
<
cardsObj
.
length
;
i
++
){
cards
.
push
(
cardsObj
[
i
].
getValue
());
}
if
(
cards
.
length
>
0
)
{
if
(
cards
.
length
>
0
)
{
sum
=
cards
.
reduce
(
sum
=
cards
.
reduce
(
(
accumulator
,
item
)
=>
accumulator
+
item
(
accumulator
,
item
)
=>
accumulator
+
item
...
@@ -58,6 +134,7 @@ function handValue(cards){
...
@@ -58,6 +134,7 @@ function handValue(cards){
}
}
function
resetGameState
(){
function
resetGameState
(){
gameState
.
deck
=
new
Deck
();
gameState
.
currentBet
=
null
;
gameState
.
currentBet
=
null
;
gameState
.
cards
=
[[],
[]];
gameState
.
cards
=
[[],
[]];
gameState
.
hiddenCard
=
null
;
gameState
.
hiddenCard
=
null
;
...
@@ -77,7 +154,7 @@ function updateGUI(){
...
@@ -77,7 +154,7 @@ function updateGUI(){
}
}
// set values of player's cards and dealer's cards in each cell
// set values of player's cards and dealer's cards in each cell
for
(
let
i
=
0
;
i
<
gameState
.
cards
[
k
].
length
;
i
++
){
for
(
let
i
=
0
;
i
<
gameState
.
cards
[
k
].
length
;
i
++
){
cells
[
i
].
innerHTML
=
gameState
.
cards
[
k
][
i
];
cells
[
i
].
innerHTML
=
gameState
.
cards
[
k
][
i
]
.
draw
()
;
}
}
let
curScore
=
handValue
(
gameState
.
cards
[
k
]);
let
curScore
=
handValue
(
gameState
.
cards
[
k
]);
let
scoreElement
=
document
.
getElementById
(
scoreIds
[
k
]);
let
scoreElement
=
document
.
getElementById
(
scoreIds
[
k
]);
...
@@ -114,11 +191,11 @@ function collectBetAndDealCards(){
...
@@ -114,11 +191,11 @@ function collectBetAndDealCards(){
// one is hidden
// 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
++
){
let
newCard
=
gameState
.
deck
.
dealCard
();
if
(
cardIndex
===
1
&&
playerIndex
===
1
){
if
(
cardIndex
===
1
&&
playerIndex
===
1
){
gameState
.
hiddenCard
=
drawCard
();
newCard
.
setCardVisibility
(
false
);
}
else
{
gameState
.
cards
[
playerIndex
][
cardIndex
]
=
drawCard
();
}
}
gameState
.
cards
[
playerIndex
][
cardIndex
]
=
newCard
;
}
}
}
}
...
@@ -145,7 +222,7 @@ function log(message){
...
@@ -145,7 +222,7 @@ function log(message){
}
}
function
hit
(){
function
hit
(){
let
curCard
=
draw
Card
();
let
curCard
=
gameState
.
deck
.
deal
Card
();
gameState
.
cards
[
0
].
push
(
curCard
);
gameState
.
cards
[
0
].
push
(
curCard
);
let
curPlayerScore
=
handValue
(
gameState
.
cards
[
0
]);
let
curPlayerScore
=
handValue
(
gameState
.
cards
[
0
]);
updateGUI
()
updateGUI
()
...
@@ -161,8 +238,9 @@ function stay(){
...
@@ -161,8 +238,9 @@ function stay(){
let
buttonHit
=
document
.
getElementById
(
'
button-hit
'
);
let
buttonHit
=
document
.
getElementById
(
'
button-hit
'
);
buttonHit
.
disabled
=
true
;
buttonHit
.
disabled
=
true
;
gameState
.
cards
[
1
].
push
(
gameState
.
hiddenCard
);
for
(
let
i
=
0
;
i
<
gameState
.
cards
[
1
].
length
;
i
++
){
gameState
.
hiddenCard
=
null
;
gameState
.
cards
[
1
][
i
].
setCardVisibility
(
true
);
}
let
curPlayerScore
=
handValue
(
gameState
.
cards
[
0
]);
let
curPlayerScore
=
handValue
(
gameState
.
cards
[
0
]);
let
curDealerScore
=
handValue
(
gameState
.
cards
[
1
]);
let
curDealerScore
=
handValue
(
gameState
.
cards
[
1
]);
...
@@ -174,7 +252,7 @@ function stay(){
...
@@ -174,7 +252,7 @@ function stay(){
// dealer draws a card
// dealer draws a card
// --- UNTIL THE DEALER has 17 or more
// --- UNTIL THE DEALER has 17 or more
while
(
curDealerScore
<
17
){
while
(
curDealerScore
<
17
){
let
curCard
=
draw
Card
();
let
curCard
=
gameState
.
deck
.
deal
Card
();
console
.
log
(
`The dealer got a new card with value
${
curCard
}
`
);
console
.
log
(
`The dealer got a new card with value
${
curCard
}
`
);
gameState
.
cards
[
1
].
push
(
curCard
);
gameState
.
cards
[
1
].
push
(
curCard
);
curDealerScore
=
handValue
(
gameState
.
cards
[
1
]);
curDealerScore
=
handValue
(
gameState
.
cards
[
1
]);
...
...
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