Java鬥地主--001版本

來源:互聯網
上載者:User

標籤:get   i++   版本   Map集合   layer   code   儲存   ==   name   

後續swing介面附上!!!敬請期待!

package com.yikuan.cn;import java.util.ArrayList;import java.util.Collections;import java.util.HashMap;/** * 實現類比鬥地主的功能: *         組合牌、洗牌、發牌、看牌 * @author Administrator * */public class DouDiZhu {    //1.組合牌,先排除大王小王2張牌;    /*花色4個固定值,存到數組中(數組效率高)*/    /*點數13個固定值,存到數組中*/    /*遍曆數組*/    public static void main(String[] args) {        /*建立Map集合,鍵是編號,值是牌*/        HashMap<Integer, String> pooker = new HashMap<Integer, String>();        /*建立list集合,儲存編號*/        ArrayList<Integer> pookerNumber = new ArrayList<Integer>();        /*定義出13個點數(從大到小)的數組*/        String[] numbers = {"2","A","K","Q","J","10","9","8","7","6","5","4","3"};        /*定義4個花色數組(點擊儲存UTF-8)*/        String[] colors = {"?","?","?","■"};        /*定義一個整數變數,作為鍵出現*/        int index = 2;/*先去掉兩王0,1*/        /*遍曆數組:花色+點數的組合,存到Map集合*/        for (String number : numbers) {            for (String color : colors) {                pooker.put(index, color + number);                pookerNumber.add(index);                index++;            }        }//        System.out.println(pooker);/*到這裡是個無序集合..*/        /*儲存大王,小王*/        pooker.put(0, "大王");        pookerNumber.add(0);        pooker.put(1, "小王");        pookerNumber.add(1);                //洗牌,將牌的編號打亂        Collections.shuffle(pookerNumber);//        System.out.println(pookerNumber);                //發牌,將牌的編號發給玩家集合,底牌集合        ArrayList<Integer> player1 = new ArrayList<Integer>();        ArrayList<Integer> player2 = new ArrayList<Integer>();        ArrayList<Integer> player3 = new ArrayList<Integer>();        ArrayList<Integer> bottom = new ArrayList<Integer>();        /*發牌採用的是集合的索引%3*/        /*模數得0--->player1*/        /*模數得1--->player2*/        /*模數得2--->player3*/        for(int i=0;i<pookerNumber.size();i++){            /*i從0開始,如果對3模數的話,就得先做底牌,如果i<3,存到底牌去*/            if(i<3){                bottom.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);        //看牌,將玩家手中的編號,到Map集合中尋找,根據鍵找到值        look("王寶強",player1,pooker);        look("馬蓉",player2,pooker);        look("宋喆",player3,pooker);        look("底牌",bottom,pooker);    }    private static void look(String name,ArrayList<Integer> player,            HashMap<Integer, String> pooker) {        System.out.print(name+" ");        /*遍曆ArrayList集合,擷取元素,作為鍵,再到集合Map中找值*/        for (Integer key : player) {            String value = pooker.get(key);            System.out.print(value+" ");        }        System.out.println();    }}

 

Java鬥地主--001版本

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.