Java基礎------鬥地主小案例

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

public class 鬥地主 {
    public static void main(String[] args) {
        //準備牌
        //定義總的一個集合存放poker
        ArrayList<String>poker=new ArrayList<>();
        //定義花色數組
        String []color={"♥","♣","♦","♠"};
        //定義序號數組
        String []numbre={"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
        //定義大王小王
        poker.add("大王");
        poker.add("小王");
        //循環遍歷
        for (String s1 : color) {
            for (String s2 : numbre) {
                //System.out.print(s1+s2+" ");
                //將組合後的存入poker集合
                poker.add(s1+s2);
            }
        }
        //System.out.println(poker); 查看集合中的54張牌
        //洗牌(工具包下的靜態方法)
        Collections.shuffle(poker);

        //發牌
        //定義四個集合存放發的紙牌
        ArrayList<String >player01=new ArrayList<>();
        ArrayList<String >player02=new ArrayList<>();
        ArrayList<String >player03=new ArrayList<>();
        ArrayList<String >dipai=new ArrayList<>();

        for (int i = 0; i < poker.size(); i++) {
            String s = poker.get(i);
            if (i>=51) {
                dipai.add(s);
            }else if (i%3==0){
                player01.add(s);
            }else if (i%3 == 1) {
                player02.add(s);
            }else if (i%3 == 2) {
                player03.add(s);
            }
        }
        //4.看牌
        //玩家一
        System.out.println(player01);
        // 玩家二
        System.out.println(player02);
        //玩家三
        System.out.println(player03);
        //底牌
        System.out.println(dipai);
    }
}
輸出結果
[♦J, ♥A,7, ♠K, ♣A,5, ♦K, ♣K,6,3, 小王,6,7,10,4,3, ♠J]
[6,4,2, ♣J, ♣Q,2,3,9,6,7,9, ♥J, ♥K,10, ♦A,2,8]
[8,10, ♥Q,8,9,8,2, ♠Q,5,5, 大王,3,5,4,9,4, ♠A]
[10,7, ♦Q]

升級版

package Test_04_鬥地主;

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


public class 升級版鬥地主 {
    public static void main(String[] args) {

        /*
        * 準備紙牌
        * */
        //定義一個map集合
        HashMap<Integer, String> poker = new HashMap<>();
        //定義一個索引list集合
        ArrayList<Integer> listindex = new ArrayList<>();
        //定義一個花色數組
        String []color={"♥","♣","♦","♠"};
        //定義一個序號
        String []numbre={"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
        //添加索引
        Integer index=0;
        listindex.add(index);
        poker.put(index,"大王");
        index++;
        listindex.add(index);
        poker.put(index,"小王");
        index++;
        for (String col : color) {
            for (String s : numbre) {
                System.out.print(col+s+"  ");
                poker.put(index,col+s);
                listindex.add(index);
                index++;
            }
            System.out.println();
        }


        /*
         *洗牌
         * */
        //System.out.println(poker);查看poker
        //System.out.println(listindex);查看map 集合單中的key 值
        Collections.shuffle(listindex);



        /*
        * 發牌
        * */
        //定義存儲四個位置
        ArrayList<Integer> player01 = new ArrayList<>();
        ArrayList<Integer> player02 = new ArrayList<>();
        ArrayList<Integer> player03 = new ArrayList<>();
        ArrayList<Integer> dipai = new ArrayList<>();

        //發牌的過程,判斷那張牌給那個人
        for (Integer integer : listindex) {
            Integer integer1 = listindex.get(integer);
            if (integer1>=51){
                dipai.add(integer);
            }else if (integer1%3==0){
                player01.add(integer1);
            }else if (integer1%3 == 1) {
                player02.add(integer1);
            }else if (integer1%3 == 2) {
                player03.add(integer1);
            }
        }
        //分別查看拿了那些牌的key值
        System.out.println(player01);
        System.out.println(player02);
        System.out.println(player03);
        System.out.println(dipai);
        
        /*
        * 排序
        * */
        //對key進行排序
        Collections.sort(player01);
        Collections.sort(player02);
        Collections.sort(player03);
        Collections.sort(dipai);

        /*
        * 看牌
        * */
        //調用自定義方法查看牌
        look("張三",poker,player01);
        look("李四",poker,player02);
        look("王五",poker,player03);
        look("底牌",poker,dipai);

    }
    //自定義方法
    public static void look(String name,HashMap<Integer, String> poker,ArrayList<Integer> list){
        //誰
        System.out.print(name+"  ");
        //遍歷循環拿了牌索引的player01,player02,player03,dipai
        for (Integer key: list) {
            //獲取poker Map集合中的 紙牌
            String value = poker.get(key);
            System.out.print(value+"  ");
        }
        System.out.println( );//獲取完每一位後換行
    }
}

運行結果:
♥2  ♥A  ♥K  ♥Q  ♥J  ♥1098765432  ♣A  ♣K  ♣Q  ♣J  ♣1098765432  ♦A  ♦K  ♦Q  ♦J  ♦1098765432  ♠A  ♠K  ♠Q  ♠J  ♠109876543  
[36, 9, 0, 21, 48, 45, 39, 3, 6, 30, 18, 15, 24, 33, 27, 42, 12]
[34, 7, 37, 1, 49, 25, 4, 46, 31, 10, 40, 16, 22, 28, 43, 13, 19]
[17, 50, 35, 44, 41, 23, 29, 8, 2, 38, 11, 20, 26, 14, 5, 47, 32]
[16, 1, 23]
張三  大王  ♥A  ♥J  ♥852  ♣Q  ♣963  ♦K  ♦1074  ♠A  ♠J  ♠8  
李四  小王  ♥K  ♥1074  ♣A  ♣J  ♣852  ♦Q  ♦963  ♠K  ♠107  
王五  ♥2  ♥Q  ♥963  ♣K  ♣1074  ♦A  ♦J  ♦852  ♠Q  ♠96  
底牌  小王  ♣A  ♣7  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章