用Java制作斗地主
首先,按照斗地主规则,完成洗牌发牌的动作。如图:
具体规则:
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) { //定义一个map集合:用来将数字与每一张牌进行对应 Map<Integer, String> pooker=new HashMap<Integer, String>(); //一副54张的牌 ArrayList里边为0-53的数的新牌 ArrayList<Integer> pookerNumber=new ArrayList<>(); //准备花色 String[] color={"♠","♣","♦","♥"}; //准备数字 String[] number={"2","A","K","Q","J","10","9","8","7","6","5","4","3"}; int index=2; for(String n:number){ for(String c:color){ pooker.put(index, c+n); pookerNumber.add(index); index++; } } //加入大小王 pooker.put(0, "大王"); pooker.put(1, "小王"); pookerNumber.add(0); pookerNumber.add(1); //洗牌 Collections.shuffle(pookerNumber); System.out.println(pookerNumber); //创建三个玩家和底牌 ArrayList<Integer> player1=new ArrayList<>(); ArrayList<Integer> player2=new ArrayList<>(); ArrayList<Integer> player3=new ArrayList<>(); ArrayList<Integer> dipai=new ArrayList<>(); //遍历这副洗好的牌,遍历过程中,将牌发到三个玩家和底牌中 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); Collections.sort(dipai); //调用方法,看查看牌 look("渣渣辉",pooker, player1); look("古天乐",pooker, player2); look("卢本伟",pooker, player3); look("底牌",pooker, dipai); } //创建一个方法,对应数字形式的每个人手中的牌,定义字符串形式的牌 public static void look(String name,Map<Integer, String> pooker,ArrayList<Integer> player){ System.out.print(name+" :"); for(int num:player){ System.out.print(pooker.get(num)+""); } System.out.println(); } }
输出请看 顶部图片
图解: