Java學習筆記34(集合架構八:綜合案例:類比鬥地主的洗牌發牌),學習筆記洗牌

來源:互聯網
上載者:User

Java學習筆記34(集合架構八:綜合案例:類比鬥地主的洗牌發牌),學習筆記洗牌

規則:

1.54張撲克牌,有花色

2.順序打亂,一人一張依次發牌,一人17張,留三張作為底牌

3.看牌:按大小王2A....43的序排列列印

樣本:

package demo;import java.util.ArrayList;import java.util.Collections;import java.util.HashMap;public class DouDiZhu {    public static void main(String[] args) {        // 建立Map集合,鍵是編號,值是牌        HashMap<Integer, String> pooker = new HashMap<Integer, String>();        // List集合儲存編號        // 用List集合原因:可以調用排序方法        ArrayList<Integer> pookerNumber = new ArrayList<Integer>();        // 由於13個點數恒定,定義數組        String[] numbers = { "2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3" };        // 花色恒定,定義數組        String[] colors = { "", "", "", "" };        // 定義整數變數,作為鍵,0和1留給大小王        int index = 2;        // 遍曆數組,存入Map集合        for (String number : numbers) {            for (String color : colors) {                pooker.put(index, color + number);                pookerNumber.add(index);                index++;            }        }        // 單獨儲存大小王        pooker.put(0, "大王");        pookerNumber.add(0);        pooker.put(1, "小王");        pookerNumber.add(1);        // 洗牌,將牌的編號打亂        Collections.shuffle(pookerNumber);        // 發牌        // 三個玩家和底牌        ArrayList<Integer> player1 = new ArrayList<Integer>();        ArrayList<Integer> player2 = new ArrayList<Integer>();        ArrayList<Integer> player3 = new ArrayList<Integer>();        ArrayList<Integer> dipai = new ArrayList<Integer>();        // 每張依次發到三個玩家        for (int i = 0; i < pookerNumber.size(); i++) {            // 先將底牌做好            if (i < 3) {                dipai.add(pookerNumber.get(i));            }            // 依次給每個玩家發牌            else if (i % 3 == 0) {                player1.add(pookerNumber.get(i));            } else if (i % 3 == 1) {                player2.add(pookerNumber.get(i));            } else if (i % 3 == 2) {                player3.add(pookerNumber.get(i));            }        }        // 對玩家手中的牌排序        Collections.sort(player1);        Collections.sort(player2);        Collections.sort(player3);        // 看牌,根據鍵找值        look("玩家1", player1, pooker);        look("玩家2", player2, pooker);        look("玩家3", player3, pooker);        look("底牌", dipai, pooker);    }    public static void look(String name, ArrayList<Integer> player, HashMap<Integer, String> pooker) {        System.out.print(name + ":");        for (Integer key : player) {            String value = pooker.get(key);            System.out.print("    " + value);        }        System.out.println();    }}

 

效果:

每次的結果都不一致:

 

有興趣的朋友可以根據鬥地主的規則繼續開發下去,做成一個簡易的鬥地主遊戲

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.