【发布时间】:2014-01-26 00:05:57
【问题描述】:
好的,我有下面定义的方法straight(ArrayList<Card> l)。它可以工作,但我真的很想改进它,因为我的算法运行速度很慢。
这里的想法是:获取卡片的所有值并将它们添加到数组列表中,然后检查数组列表是否包含顺子列表。如果是,将其添加到 hashmap,最后添加的数组将是最好的直线(如果有 3 个直线:1 2 3 4 5 6 7)
卡片由 2 个数组定义:价值和花色:
private String[] name = {"Joker", "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
private String[] suit = {"Club", "Diamond", "Spade", "Heart"};
所以我有两个问题:
1、下面的代码有什么改进的地方吗?
2,我做这个项目是为了好玩,我希望这是多人游戏。那么服务器是否应该检测每个玩家的最佳手牌或玩家这样做,然后将结果以 (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;
}
【问题讨论】:
-
我不太明白。我知道如何更有效地判断一手牌是否是顺子,但你到底想用这种方法做什么?你只是想看看一手牌是否是顺子吗?
-
是的,这种方法用于检测 7 张牌中的顺子。如果找到顺子,则返回顺子,否则返回大小为 0 的数组列表
-
它还可以检测任意数量的牌中的顺子
-
允许多少个通配符(Jokers?)?
-
小丑是不允许的。所有循环都以 1 开头,一包有 52 张卡片。