Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
DeckOfCards
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
Vetle Solheim Hodne
DeckOfCards
Merge requests
!3
Dev/deal hand
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Dev/deal hand
Dev/dealHand
into
main
Overview
0
Commits
2
Pipelines
1
Changes
5
Merged
Vetle Solheim Hodne
requested to merge
Dev/dealHand
into
main
1 year ago
Overview
0
Commits
2
Pipelines
1
Changes
5
Expand
0
0
Merge request reports
Compare
main
main (base)
and
latest version
latest version
55ec32c4
2 commits,
1 year ago
5 files
+
134
−
30
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
5
Search (e.g. *.vue) (Ctrl+P)
src/main/java/edu/ntnu/idatt2003/deckofcards/DeckOfCards.java
+
42
−
17
Options
package
edu.ntnu.idatt2003.deckofcards
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.List
;
import
java.util.Random
;
public
class
DeckOfCards
{
private
final
char
[]
suit
=
{
'S'
,
'H'
,
'D'
,
'C'
};
private
final
char
[]
rank
=
{
'A'
,
'2'
,
'3'
,
'4'
,
'5'
,
'6'
,
'7'
,
'8'
,
'9'
,
'T'
,
'J'
,
'Q'
,
'K'
};
private
final
String
[]
deck
;
private
final
String
[]
rank
=
{
"2"
,
"3"
,
"4"
,
"5"
,
"6"
,
"7"
,
"8"
,
"9"
,
"10"
,
"J"
,
"Q"
,
"K"
,
"A"
};
private
final
ArrayList
<
String
>
deck
;
public
DeckOfCards
()
{
deck
=
new
String
[
52
]
;
deck
=
new
ArrayList
<>()
;
initializeDeck
();
}
//Initializes the deck with all 52 cards
private
void
initializeDeck
()
{
int
index
=
0
;
for
(
char
suit
:
suit
)
{
for
(
char
rank
:
rank
)
{
deck
[
index
++]
=
""
+
suit
+
rank
;
for
(
String
rank
:
rank
)
{
deck
.
add
(
suit
+
rank
)
;
}
}
}
public
void
shuffle
()
{
Random
random
=
new
Random
();
for
(
int
i
=
0
;
i
<
deck
.
size
();
i
++)
{
int
randomIndex
=
random
.
nextInt
(
deck
.
size
());
String
temp
=
deck
.
get
(
i
);
deck
.
set
(
i
,
deck
.
get
(
randomIndex
));
deck
.
set
(
randomIndex
,
temp
);
}
}
public
void
dealHand
(
int
numberOfCards
,
Hand
hand
)
{
Random
random
=
new
Random
();
for
(
int
i
=
0
;
i
<
numberOfCards
;
i
++)
{
//Find a random card in the deck
int
cardIndex
=
random
.
nextInt
(
deck
.
size
());
//Add the card to the hand
hand
.
add
(
deck
.
get
(
cardIndex
));
deck
.
remove
(
cardIndex
);
}
}
public
void
collectHand
(
Hand
hand
)
{
deck
.
addAll
(
hand
.
getHand
());
hand
.
clearHand
();
}
public
char
[]
getSuit
()
{
return
suit
;
}
public
char
[]
getRank
()
{
public
String
[]
getRank
()
{
return
rank
;
}
public
String
[]
getDeck
()
{
public
ArrayList
<
String
>
getDeck
()
{
return
deck
;
}
@Override
public
String
toString
()
{
StringBuilder
deckString
=
new
StringBuilder
();
for
(
char
s
:
suit
)
{
for
(
char
r
:
rank
)
{
deckString
.
append
(
s
).
append
(
r
).
append
(
" "
);
}
deckString
.
append
(
"\n"
);
}
return
deckString
.
toString
();
return
deck
+
" "
;
}
}
Loading