理解很好理解,关键是思路
按照斗地主的规则,完成洗牌发牌的动作:
具体规则:
1. 组装54张扑克牌
2. 将54张牌顺序打乱
3. 三个玩家参与游戏,三人交替摸牌,每人17张牌,最后三张留作底牌。
4. 查看三人各自手中的牌(按照牌的大小排序)、底牌
手中扑克牌从大到小的摆放顺序:大王,小王,2,A,K,Q,J,10,9,8,7,6,5,4,3
package com.oracle.demo01;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class Doudizhu {
public static void main(String[] args) {
//1.创建扑克牌Map
Map<Integer,String> pooker=new HashMap<Integer,String>();
//创建所有key所在的容器
ArrayList<Integer> pookerNum=new ArrayList<Integer>();
//创建花色数组
String[] color={"♠","♣","♥","♦"};
//创建牌号数组
String[] number={"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
//造牌并存进map集合
int index=2;
for(String n:number){
for(String c:color){
//向map中存数据
pooker.put(index,c+n);
//向所有key所在的容器存数据
pookerNum.add(index);
index++;
}
}
//存大小王
pooker.put(0, "大王");
pookerNum.add(0);
pooker.put(1, "小王");
pookerNum.add(1);
//洗牌
Collections.shuffle(pookerNum);
//System.out.println(pookerNum);
//发牌
//创建四个容器
ArrayList<Integer> bottom=new ArrayList<>();
ArrayList<Integer> player1=new ArrayList<>();
ArrayList<Integer> player2=new ArrayList<>();
ArrayList<Integer> player3=new ArrayList<>();
//开始发牌
for(int i=0;i<pookerNum.size();i++){
//将前三张给底牌
if(i<3){
bottom.add(pookerNum.get(i));
}else if(i%3==0){
player1.add(pookerNum.get(i));
}else if(i%3==1){
player2.add(pookerNum.get(i));
}else if(i%3==2){
player3.add(pookerNum.get(i));
}
}
//排序(key升序 牌从大到小)
Collections.sort(bottom);
Collections.sort(player1);
Collections.sort(player2);
Collections.sort(player3);
//看牌(调用方法)
look("刘德华",player1,pooker);
look("张家辉",player2,pooker);
look("周润发",player3,pooker);
look("底牌",bottom,pooker);
}
//看牌的方法(传参为 玩家姓名,玩家的牌即键,所有牌的键值对)
public static void look( String name,ArrayList<Integer> player,Map<Integer,String> pooker){
//打印玩家姓名
System.out.print(name+":");
//遍历所有牌号
for(int num:player){
System.out.print(pooker.get(num)+" ");
}
System.out.println();
}
}
来源:oschina
链接:https://my.oschina.net/u/4268222/blog/3516016