java使用list集合和set结合分别实现斗地主操作
时间: 2024-03-31 11:35:32 浏览: 117
好的,我来回答你的问题。
首先,我们需要定义一个扑克牌的类 Poker,如下所示:
```
public class Poker {
private String color; // 花色
private String value; // 数值
public Poker(String color, String value) {
this.color = color;
this.value = value;
}
// getter 和 setter 方法
// ...
}
```
接下来,我们来看一下如何用 List 集合实现斗地主操作。List 集合是有序的,可以按照添加的顺序访问元素。
```
import java.util.*;
public class DouDiZhuWithList {
public static void main(String[] args) {
// 初始化扑克牌
List<Poker> pokers = new ArrayList<>();
String[] colors = {"♠", "♥", "♦", "♣"};
String[] values = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
for (String color : colors) {
for (String value : values) {
pokers.add(new Poker(color, value));
}
}
pokers.add(new Poker("", "小王"));
pokers.add(new Poker("", "大王"));
// 洗牌
Collections.shuffle(pokers);
// 发牌
List<Poker> player1 = new ArrayList<>();
List<Poker> player2 = new ArrayList<>();
List<Poker> player3 = new ArrayList<>();
List<Poker> left = new ArrayList<>();
for (int i = 0; i < pokers.size(); i++) {
if (i >= 51) {
left.add(pokers.get(i));
} else if (i % 3 == 0) {
player1.add(pokers.get(i));
} else if (i % 3 == 1) {
player2.add(pokers.get(i));
} else {
player3.add(pokers.get(i));
}
}
// 排序
Collections.sort(player1, new Comparator<Poker>() {
@Override
public int compare(Poker o1, Poker o2) {
return o1.value.compareTo(o2.value);
}
});
Collections.sort(player2, new Comparator<Poker>() {
@Override
public int compare(Poker o1, Poker o2) {
return o1.value.compareTo(o2.value);
}
});
Collections.sort(player3, new Comparator<Poker>() {
@Override
public int compare(Poker o1, Poker o2) {
return o1.value.compareTo(o2.value);
}
});
Collections.sort(left, new Comparator<Poker>() {
@Override
public int compare(Poker o1, Poker o2) {
return o1.value.compareTo(o2.value);
}
});
// 输出结果
System.out.println("玩家1:" + player1);
System.out.println("玩家2:" + player2);
System.out.println("玩家3:" + player3);
System.out.println("底牌:" + left);
}
}
```
接下来,我们来看一下如何用 Set 集合实现斗地主操作。Set 集合是无序的,不允许重复元素。
```
import java.util.*;
public class DouDiZhuWithSet {
public static void main(String[] args) {
// 初始化扑克牌
Set<Poker> pokers = new HashSet<>();
String[] colors = {"♠", "♥", "♦", "♣"};
String[] values = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
for (String color : colors) {
for (String value : values) {
pokers.add(new Poker(color, value));
}
}
pokers.add(new Poker("", "小王"));
pokers.add(new Poker("", "大王"));
// 洗牌
List<Poker> list = new ArrayList<>(pokers);
Collections.shuffle(list);
// 发牌
List<Poker> player1 = new ArrayList<>();
List<Poker> player2 = new ArrayList<>();
List<Poker> player3 = new ArrayList<>();
List<Poker> left = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
if (i >= 51) {
left.add(list.get(i));
} else if (i % 3 == 0) {
player1.add(list.get(i));
} else if (i % 3 == 1) {
player2.add(list.get(i));
} else {
player3.add(list.get(i));
}
}
// 排序
Collections.sort(player1, new Comparator<Poker>() {
@Override
public int compare(Poker o1, Poker o2) {
return o1.value.compareTo(o2.value);
}
});
Collections.sort(player2, new Comparator<Poker>() {
@Override
public int compare(Poker o1, Poker o2) {
return o1.value.compareTo(o2.value);
}
});
Collections.sort(player3, new Comparator<Poker>() {
@Override
public int compare(Poker o1, Poker o2) {
return o1.value.compareTo(o2.value);
}
});
Collections.sort(left, new Comparator<Poker>() {
@Override
public int compare(Poker o1, Poker o2) {
return o1.value.compareTo(o2.value);
}
});
// 输出结果
System.out.println("玩家1:" + player1);
System.out.println("玩家2:" + player2);
System.out.println("玩家3:" + player3);
System.out.println("底牌:" + left);
}
}
```
以上就是用 List 集合和 Set 集合分别实现斗地主操作的示例代码,希望能够帮到你。
阅读全文