0%

Java(26)案例:斗地主综合案例

39 案例:斗地主综合案例

  • 斗地主综合案例:有序版本

    1. 准备牌
    1. 洗牌
    1. 发牌
    1. 排序
    1. 看牌

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
package com.tipdm.Demo03;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

/**
* 斗地主综合案例:有序版本
* 1. 准备牌
* 2. 洗牌
* 3. 发牌
* 4. 排序
* 5. 看牌
*/
public class demo1 {
public static void main(String[] args) {
// 定义一个Map集合用来存储牌的索引和牌型
HashMap<Integer, String> poker = new HashMap<>();
// 首先定义一个扑克牌
ArrayList<Integer> pokerIndex = new ArrayList<>();
// 牌索引起点
int index = 0;
// 将大王和小王放入牌堆
pokerIndex.add(index);
poker.put(index, "大王");
index++;
pokerIndex.add(index);
poker.put(index, "小王");
index++;
// 定义存储花色的数组
String[] hs = {"♠", "♥", "♣", "♦"};
// 定义存储牌大小的数组
String[] dx = {"2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"};
// 循环嵌套将牌花色和大小进行组装
for (int i = 0; i < dx.length; i++) {
for (int i1 = 0; i1 < hs.length; i1++) {
pokerIndex.add(index);
poker.put(index, hs[i1]+dx[i]);
index++;
}
}
// System.out.println(poker);
// System.out.println(pokerIndex);
// 洗牌
Collections.shuffle(pokerIndex);
// 定义玩家和底牌集合
ArrayList<Integer> poker1 = new ArrayList<>();
ArrayList<Integer> poker2 = new ArrayList<>();
ArrayList<Integer> poker3 = new ArrayList<>();
ArrayList<Integer> dp = new ArrayList<>();
// 发牌
System.out.println(pokerIndex);
for (int i = 0; i < pokerIndex.size(); i++) {
if(i >= 51){
dp.add(pokerIndex.get(i));
}else if(i % 3 == 0){
poker1.add(pokerIndex.get(i));
}else if(i % 3 == 1){
poker2.add(pokerIndex.get(i));
}else if(i % 3 == 2){
poker3.add(pokerIndex.get(i));
}
}
// 排序
Collections.sort(poker1);
Collections.sort(poker2);
Collections.sort(poker3);

System.out.println(poker1);
System.out.println(poker2);
System.out.println(poker3);
System.out.println(dp);
// 将索引对应回牌型
index2color("刘德华", poker, poker1);
index2color("周润发", poker, poker2);
index2color("陈小刀", poker, poker3);
index2color("底牌", poker, dp);
}

private static void index2color(String name, HashMap<Integer, String> map, ArrayList<Integer> list) {
System.out.print(name+":[");
for (Integer key : list) {
String value = map.get(key);
System.out.print(value + ",");
}
System.out.println("]");
}

}
1
2
3
4
5
6
7
8
9
10
11
[10, 53, 32, 15, 29, 33, 21, 36, 42, 39, 44, 14, 48, 1, 18, 30, 24, 43, 49, 22, 52, 20, 7, 8, 40, 17, 19, 28, 16, 37, 35, 46, 11, 47, 5, 51, 0, 13, 41, 2, 4, 23, 27, 45, 6, 38, 50, 26, 12, 25, 34, 9, 31, 3]
[0, 2, 10, 12, 15, 20, 21, 27, 28, 30, 35, 38, 39, 40, 47, 48, 49]
[1, 4, 5, 7, 13, 16, 17, 22, 24, 25, 29, 36, 44, 45, 46, 50, 53]
[6, 8, 11, 14, 18, 19, 23, 26, 32, 33, 34, 37, 41, 42, 43, 51, 52]
[9, 31, 3]
刘德华:[大王,♠2,♠K,♣K,♥Q,♣J,♦J,♥9,♣9,♠8,♥7,♠6,♥6,♣6,♥4,♣4,♦4,]
周润发:[小王,♣2,♦2,♥A,♦K,♣Q,♦Q,♠10,♣10,♦10,♦9,♣7,♣5,♦5,♠4,♠3,♦3,]
陈小刀:[♠A,♣A,♥K,♠Q,♠J,♥J,♥10,♠9,♣8,♦8,♠7,♦7,♦6,♠5,♥5,♥3,♣3,]
底牌:[♦A,♥8,♥2,]

进程已结束,退出代码0
-------------本文结束感谢您的阅读-------------