JAVA编程实现斗地主小游戏

主程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
public static void main(String[] args) {
//牌
List<String> pokes = new ArrayList<>();
//颜色花色
List<String> colors = new ArrayList<>();
//数量
List<String> nums = new ArrayList<>();

colors.add("♥");
colors.add("♠");
colors.add("♦");
colors.add("♣");
nums.add("A");
for (int i=2; i<=10; i++){
nums.add(i+"");
}
nums.add("J");
nums.add("Q");
nums.add("K");
for (String c:colors){
for (String n:nums){
String pai = c+n;
pokes.add(pai);
}
}
pokes.add("大王");
pokes.add("小王");
// System.out.println(pokes);
// System.out.println(pokes.size());
Collections.shuffle(pokes);
System.out.println(pokes);

//玩家
//发牌,留三张
ArrayList<String> smj = new ArrayList<>();
ArrayList<String> whb = new ArrayList<>();
ArrayList<String> zk = new ArrayList<>();
ArrayList<String> bottom = new ArrayList<>();
for (int i = 0; i < pokes.size(); i++) {
String pai = pokes.get(i);
if(i>=51){
bottom.add(pai);
}
else{
if (i%3==0){
smj.add(pai);
}
else if(i%3==1){
whb.add(pai);
}
else {
zk.add(pai);
}
}
}
System.out.println("底牌"+bottom);
System.out.println("smj"+smj);
System.out.println("whb"+whb);
System.out.println("zk"+zk);

//选择地主:
System.out.println("请输入地主:");
Scanner scanner = new Scanner(System.in);
String dz = scanner.nextLine();
for (int i = 0; i < bottom.size(); i++) {
String pai = bottom.get(i);
if (dz.equals("smj")){
smj.add(pai);
}
else if(dz.equals("whb")){
whb.add(pai);
}
else {
zk.add(pai);
}
}
//清空底牌
bottom = null;
System.out.println("底牌"+bottom);
System.out.println("smj"+smj);
System.out.println("whb"+whb);
System.out.println("zk"+zk);

//排序
//按大王小王2KQJ1098765432A
System.out.println("调整手牌");
smj=paix(smj);
whb=paix(whb);
zk=paix(zk);
System.out.println("smj"+smj);
System.out.println("whb"+whb);
System.out.println("zk"+zk);

排序算法

思想为把该扑克牌数组复制一个为数字类型的数组,对数组进行排序,同时更改扑克牌数组。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
public class sort {

private static <E> void swap(List<E> list, int index1, int index2) { //数组元素交换位置
E e=list.get(index1);
list.set(index1, list.get(index2));
list.set(index2, e);
}
static ArrayList<String> paix(ArrayList<String> x){ //纸牌有序化
ArrayList<Integer> xyz=new ArrayList<>();
int f=-1;
//复制一个数字类型的数组
for (int i = 0; i < x.size(); i++) {
if(x.get(i).equals("大王"))
f=172;
else if(x.get(i).equals("小王"))
f=171;
else {
if (x.get(i).substring(1).equals("A"))
f = 140;
else if (x.get(i).substring(1).equals("2"))
f = 150;
else if (x.get(i).substring(1).equals("J"))
f = 110;
else if (x.get(i).substring(1).equals("Q"))
f = 120;
else if (x.get(i).substring(1).equals("K"))
f = 130;
else f= Integer.parseInt(x.get(i).substring(1)) * 10;

if(x.get(i).substring(0,1).equals("♥")){
f+=4;
}else if(x.get(i).substring(0,1).equals("♦")){
f+=3;
}else if(x.get(i).substring(0,1).equals("♠")){
f+=2;
}else if(x.get(i).substring(0,1).equals("♣")){
f+=1;
}
}
xyz.add(f);
}

// System.out.println(xyz);

for (int i = x.size(); i > 1; i--) {
for (int j = 0; j < i-1; j++) {
if(xyz.get(j) < xyz.get(j+1)){
swap(xyz,j,j+1);
swap(x,j,j+1);
}
}
}

return x;
}

}