黑马程序员技术交流社区

标题: 集合综合应用,斗地主案例 [打印本页]

作者: _J2EE_LiXiZhen    时间: 2017-11-17 23:08
标题: 集合综合应用,斗地主案例
[Java] 纯文本查看 复制代码
package com.itheima.poker;

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

/*
*  实现斗地主的排序功能
*/
public class Poker {
        public static void main(String[] args) {       
                // 定义Map集合,键是编号,值是牌
                HashMap<Integer, String> pokerMap = new HashMap<Integer, String>();
                // 定义集合List,存储编号
                ArrayList<Integer> pokerList = new ArrayList<Integer>();
                // 定义变量,记录牌的编号
                int index = 0;
                // 定义存储4个花色的数组
                String[] colors = { "♠", "♥", "♣", "♦" };
                // 定义存储13个数字数组
                String[] numbers = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2" };
                // 遍历数组,牌的组合
                for (String number : numbers) {
                        for (String color : colors) {
                                // 组合牌,编号存储到Map集合
                                pokerMap.put(index, color + number);
                                // 编号存储到List集合
                                pokerList.add(index);
                                index++;
                        }
                }
                // 添加王牌
                pokerMap.put(52, "小王");
                pokerMap.put(53, "大王");
                pokerList.add(52);
                pokerList.add(53);

                // 洗牌,洗的是编号
                Collections.shuffle(pokerList);

                // 发牌: 到家手中的集合,是编号
                ArrayList<Integer> player1 = new ArrayList<Integer>();
                ArrayList<Integer> player2 = new ArrayList<Integer>();
                ArrayList<Integer> player3 = new ArrayList<Integer>();
                ArrayList<Integer> dipai = new ArrayList<Integer>();
                // 遍历牌盒容器,对集合的索引%3操作
                for (int i = 0; i < pokerList.size(); i++) {
                        // 先发底牌, 牌盒容器的索引012 到底牌
                        if (i < 3) {
                                dipai.add(pokerList.get(i));
                        }

                        // 索引i%3==0,发到玩家1
                        else if (i % 3 == 0) {
                                player1.add(pokerList.get(i));
                        }
                        // 索引i%3==1,发到玩家2
                        else if (i % 3 == 1) {
                                player2.add(pokerList.get(i));
                        }
                        // 索引i%3==2,发到玩家3
                        else if (i % 3 == 2) {
                                player3.add(pokerList.get(i));
                        }
                }
                //循环发牌完成,每个玩家集合中,存储的是编号
                //Collections方法sort,玩家集合中的编号,升序排列
                Collections.sort(player1);
                Collections.sort(player2);
                Collections.sort(player3);
                Collections.sort(dipai);
               
                //看牌: 玩家集合中的编号,作为键,Map集合找值
                look(player1, pokerMap);
                look(player2, pokerMap);
                look(player3, pokerMap);
                look(dipai, pokerMap);
        }
        /*
         * 定义方法,实现看牌方法
         * 参数: 玩家集合,Map集合
         */
        public static void look(ArrayList<Integer> array, HashMap<Integer, String> map){
                for(Integer key : array){
                        System.out.print(map.get(key)+" ");
                }
                System.out.println();
        }
}





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2