Ok, so, I have the method straight(ArrayList<Card> l)
defined below. It works but I really want to improve it because my algorithm is running quite slow.
The idea here is: get all value of the Cards and add them to an arraylist, then check whether the arraylist contain the list of straights or not. If yes, add it to a hashmap, the last array added will be the best straight (in case there are 3 straight: 1 2 3 4 5 6 7)
The Card is define by 2 arrays: value and suit which are:
private String[] name = {"Joker", "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
private String[] suit = {"Club", "Diamond", "Spade", "Heart"};
So I have 2 questions:
1, Is there anyway to improve the code below?
2, I do this project for fun, I want this to be multiple player. So should the server detect the best hand of each player or players do that and then send the result back to server by a pair in form of (String, ArrayList bestHand)?
public ArrayList<Card> straight(ArrayList<Card> l){
ArrayList<Card> straight = new ArrayList<>();
ArrayList<Card> card_value = this.removeDuplicate(l);
ArrayList<Integer> temp_value = this.getCardValue(card_value);
ArrayList<Integer[]> possible_straight = new ArrayList<>();
possible_straight.add(new Integer[]{1, 2, 3, 4, 5});
possible_straight.add(new Integer[]{2, 3, 4, 5, 6});
possible_straight.add(new Integer[]{3, 4, 5, 6, 7});
possible_straight.add(new Integer[]{4, 5, 6, 7, 8});
possible_straight.add(new Integer[]{5, 6, 7, 8, 9});
possible_straight.add(new Integer[]{6, 7, 8, 9, 10});
possible_straight.add(new Integer[]{7, 8, 9, 10, 11});
possible_straight.add(new Integer[]{8, 9, 10, 11, 12});
possible_straight.add(new Integer[]{9, 10, 11, 12, 13});
possible_straight.add(new Integer[]{10, 11, 12, 13, 1});
HashMap<Integer, Integer[]> check_list = new HashMap<>();
int controller = 0;
for (int i = 0; i < possible_straight.size(); i++){
Integer[] dummy = possible_straight.get(i);
if (temp_value.containsAll(Arrays.asList(dummy))){
check_list.put(controller++, dummy);
}
}
if (!check_list.isEmpty()){
for (int k = 0; k < check_list.get((controller - 1)).length; k++){
for (int m = 0; m < card_value.size(); m++){
Card temp_card = card_value.get(m);
if (temp_card.getValue() == check_list.get(controller - 1)[k]){
straight.add(temp_card);
}
}
}
}
return straight;
}