Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
H
Homework
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor 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
Siyu Lin
Homework
Commits
cff67e8b
Commit
cff67e8b
authored
10 years ago
by
Siyu Lin
Browse files
Options
Downloads
Patches
Plain Diff
createHand writing
parent
cb4aea88
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
homework/src/hw3/OnePairEvaluator.java
+129
-23
129 additions, 23 deletions
homework/src/hw3/OnePairEvaluator.java
with
129 additions
and
23 deletions
homework/src/hw3/OnePairEvaluator.java
+
129
−
23
View file @
cff67e8b
package
hw3
;
import
java.lang.reflect.Array
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
api.Card
;
import
api.Hand
;
...
...
@@ -18,6 +20,7 @@ import util.SubsetFinder;
public
class
OnePairEvaluator
implements
IEvaluator
{
private
int
ranking
;
private
int
handSize
;
private
int
numMainCards
;
/**
* Constructs the evaluator.
...
...
@@ -32,6 +35,7 @@ public class OnePairEvaluator implements IEvaluator {
// perform other initialization
this
.
ranking
=
ranking
;
this
.
handSize
=
handSize
;
this
.
numMainCards
=
2
;
}
...
...
@@ -47,8 +51,7 @@ public class OnePairEvaluator implements IEvaluator {
@Override
public
int
cardsRequired
()
{
// TODO Auto-generated method stub
return
2
;
return
numMainCards
;
}
@Override
...
...
@@ -60,9 +63,11 @@ public class OnePairEvaluator implements IEvaluator {
@Override
public
boolean
canSatisfy
(
Card
[]
mainCards
)
{
// TODO Check the array is sorted
if
(
mainCards
.
length
<
this
.
cardsRequired
())
{
if
(
mainCards
.
length
!=
this
.
cardsRequired
())
{
return
false
;
}
else
{
// Iterate every card of mainCards and check them if they meet the
// criteria
if
(
mainCards
[
0
].
equals
(
mainCards
[
1
]))
{
return
true
;
}
...
...
@@ -73,17 +78,29 @@ public class OnePairEvaluator implements IEvaluator {
@Override
public
boolean
canSubsetSatisfy
(
Card
[]
allCards
)
{
// TODO Auto-generated method stub
// Find all the subsets of allCards
// How many subsets?
if
(
findSubCards
(
allCards
).
size
()
!=
0
)
{
return
true
;
}
else
{
return
false
;
}
}
// Find mainCards that can meet the criteria of the evaluator from allCards
private
ArrayList
<
Card
[]>
findSubCards
(
Card
[]
allCards
)
{
// How many subsets whose length is cardsRequired are in allCards?
ArrayList
<
int
[]>
subsets
=
util
.
SubsetFinder
.
findSubsets
(
allCards
.
length
,
2
);
allCards
.
length
,
this
.
cardsRequired
());
// subsets of mainCards that can satisfy the criteria of the evaluator
ArrayList
<
Card
[]>
subCards
=
new
ArrayList
<
Card
[]>();
// Check all the subsets canSatisfy the criteria
for
(
int
i
=
0
;
i
<
subsets
.
size
();
i
++)
{
int
[]
subset
=
subsets
.
get
(
i
);
// Create an array of cards with just the card chosen by the subset
Card
[]
subsetCards
=
new
Card
[
2
];
Card
[]
subsetCards
=
new
Card
[
this
.
cardsRequired
()
];
int
resultIndex
=
0
;
for
(
int
j
=
0
;
j
<
subset
.
length
;
++
j
)
{
int
index
=
subset
[
j
];
...
...
@@ -92,39 +109,128 @@ public class OnePairEvaluator implements IEvaluator {
subsetCards
[
resultIndex
]
=
card
;
resultIndex
+=
1
;
}
// Check the array can satisfy the evaluator
if
(
this
.
canSatisfy
(
subsetCards
))
{
return
true
;
subCards
.
add
(
subsetCards
)
;
}
}
return
false
;
return
subCards
;
}
@Override
public
Hand
createHand
(
Card
[]
allCards
,
int
[]
subset
)
{
// Can we find given indices in allCards?
boolean
found
=
findIndices
(
allCards
,
subset
);
// Return Null If
// The given cards are less than the required cards OR
// No subset can satisfy the criteria OR
// The given indices can not be found in all cards
if
(
allCards
.
length
<
this
.
cardsRequired
()
||
!
this
.
canSubsetSatisfy
(
allCards
))
{
||
!
this
.
canSubsetSatisfy
(
allCards
)
||
!
found
)
{
return
null
;
}
else
{
// Find if the indices of subset appear in the allCards
// Return null if not found
// Find main cards that contain subset and meet the criteria
// Iterate all the subsets of allCards that can satisfy the criteria
Card
[]
mainCards
=
new
Card
[
this
.
cardsRequired
()];
Card
[]
sideCards
=
new
Card
[
this
.
handSize
()
-
this
.
cardsRequired
()];
// When we find given subset indices in allCards
if
(
found
)
{
// Iterate all the mainCards subsets of allCards that can
// satisfy the
// criteria
ArrayList
<
Card
[]>
satisfiedSubCard
=
findSubCards
(
allCards
);
for
(
Card
[]
cards
:
satisfiedSubCard
)
{
// Sort cards and satisfiedSubCards first
Arrays
.
sort
(
cards
);
Arrays
.
sort
(
mainCards
);
// When we find given indices in a subset of satifiedSubCard
if
(
findIndices
(
cards
,
subset
))
{
// mainCards should be the higher ranking one
mainCards
=
findHigherRankingCards
(
mainCards
,
cards
);
}
}
// If no given subset indices is contained in satisfiedSubCard,
// return
// null
if
(
mainCards
==
null
)
{
return
null
;
}
else
{
// Find side cards of higher ranking according to the
// handSize
// Create an array of cards that don't contain mainCards
// from allCards
int
countMainCards
=
0
;
int
index
=
0
;
ArrayList
<
Card
>
allSideCards
=
new
ArrayList
<
Card
>();
for
(
int
i
=
0
;
i
<
allCards
.
length
;
i
++)
{
allSideCards
.
add
(
allCards
[
i
]);
}
// Find mainCards in allCards and delete it
// Find and remove the mainCards from allCards
}
// If the subset'cards contain a card of ranking of subset[i],
// compare to the previous one and get the higher ranking one
}
}
return
null
;
}
// If no given subset indices is contained in the subsets, return
// null
private
Card
[]
findHigherRankingCards
(
Card
[]
thsCards
,
Card
[]
othCards
)
{
// Find side cards of
higher
r
anking
according to the handSize
Card
[]
higher
R
anking
Cards
=
new
Card
[
thsCards
.
length
];
// Find the subsets, each of which contain handSize-mainCards cards,
// of allCards except the main cards
// Compare them and find higher ranking one
if
(
thsCards
.
length
!=
othCards
.
length
)
{
return
null
;
}
else
{
// Compare the current mainCards with cards
copyOfCards
(
higherRankingCards
,
thsCards
);
for
(
int
i
=
0
;
i
<
othCards
.
length
;
i
++)
{
if
(
othCards
[
i
].
compareTo
(
thsCards
[
i
])
>
0
)
{
// Copy cards that contain indices to mainCards
copyOfCards
(
higherRankingCards
,
othCards
);
}
}
}
return
null
;
return
higherRankingCards
;
}
private
void
copyOfCards
(
Card
[]
mainCards
,
Card
[]
cards
)
{
for
(
int
i
=
0
;
i
<
mainCards
.
length
;
i
++)
{
mainCards
[
i
]
=
cards
[
i
];
}
}
// Find given indices from given cards
private
boolean
findIndices
(
Card
[]
allCards
,
int
[]
subset
)
{
// Find if the indices of subset appear in the allCards
// Return null if not found
boolean
found
=
false
;
int
index
=
0
;
for
(
int
e
:
subset
)
{
while
(!
found
&&
index
<
allCards
.
length
)
{
if
(
allCards
[
index
].
getRank
()
==
e
)
{
found
=
true
;
}
index
++;
}
}
return
found
;
}
@Override
...
...
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