1. 程式人生 > >Java鬥地主--001版本

Java鬥地主--001版本

value min 期待 界面 collect map 玩家 進行 功能

後續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版本