Skip to content
Snippets Groups Projects
Commit cff67e8b authored by Siyu Lin's avatar Siyu Lin
Browse files

createHand writing

parent cb4aea88
No related branches found
No related tags found
No related merge requests found
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 ranking according to the handSize
Card[] higherRankingCards = 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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment