斗地主案例(使用ArrayList)

代码:

 1 import java.util.ArrayList;
 2 import java.util.Collections;
 3 
 4 public class Poker1 {
 5     public static void main(String[] args) {
 6         /*
 7          * 1: 准备牌操作
 8          */
 9         //1.1 创建牌盒 将来存储牌面的
10         ArrayList<String> pokerBox = new ArrayList<String>();
11         //1.2 创建花色集合
12         ArrayList<String> colors = new ArrayList<String>();
13         //1.3 创建数字集合
14         ArrayList<String> numbers = new ArrayList<String>();
15         //1.4 分别赋予花色
16         colors.add("♥");
17         colors.add("♦");
18         colors.add("♠");
19         colors.add("♣");
20         //1.5 分别赋予数字
21         numbers.add("A");
22         for (int i = 2; i <= 10; i++) {
23             numbers.add("" + i);
24         }
25         numbers.add("J");
26         numbers.add("Q");
27         numbers.add("K");
28         // //1.6 创造牌  拼接牌操作
29         // 拿出每一个花色  然后跟每一个数字 进行结合  存储到牌盒中
30         for (String color : colors) {
31             //color每一个花色
32             //遍历数字集合
33             for (String number : numbers) {
34                 //存储到牌盒中
35                 pokerBox.add(color + number);
36             }
37         }
38         //1.7大王小王
39         pokerBox.add("小☺");
40         pokerBox.add("大☠");
41         //洗牌 是不是就是将  牌盒中 牌的索引打乱
42         // Collections类  工具类  都是 静态方法
43         // shuffer方法
44         /*
45          ** static void shuffle(List<?> list)
46          *     使用默认随机源对指定列表进行置换。
47          */
48         //2:洗牌
49         Collections.shuffle(pokerBox);
50         //3 发牌
51         //3.1 创建 三个 玩家集合  创建一个底牌集合
52         ArrayList<String> player1 = new ArrayList<String>();
53         ArrayList<String> player2 = new ArrayList<String>();
54         ArrayList<String> player3 = new ArrayList<String>();
55         ArrayList<String> dipai = new ArrayList<String>();
56         for (int i = 0; i < pokerBox.size(); i++) {
57             String str = pokerBox.get(i);
58             if(i < 51){
59                 if(i % 3 == 0){
60                     player1.add(str);
61                 }
62                 else if(i % 3 == 1){
63                     player2.add(str);
64                 }
65                 else {
66                     player3.add(str);
67                 }
68             }
69             else {
70                 dipai.add(str);
71             }
72         }
73         System.out.println("令狐冲:"+player1);
74         System.out.println("田伯光:"+player2);
75         System.out.println("绿竹翁:"+player3);
76         System.out.println("底牌  :"+dipai);
77     }
78 }

猜你喜欢

转载自www.cnblogs.com/0error0warning/p/12528433.html
今日推荐