package cn.itcast.day13.demo04;import java.util.ArrayList;import java.util.Collections;/** * @author newcityman * @date 2019/7/18 - 21:15 * 应用背景: * 三个人斗地主,每个人取17张牌,3张牌落底,留给地主使用 */public class DouDiZhu {public static void main(String[] args) {//1、准备牌 //先定义一个集合,用来存放54张牌,泛型使用String ArrayList<String> poker = new ArrayList<>(); //定义两个数组,一个保存牌的花色,一个保存牌的序号 String[] colors = {"♥", "♠", "♣", "♦"}; String[] numbers = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"}; //首先把大小王,存放到集合中去 poker.add("大王"); poker.add("小王"); //循环嵌套遍历两个数组,组合52张牌 for (String color : colors) {for (String number : numbers) { poker.add(color + number);// System.out.println(color+number); } }//2、洗牌 Collections.shuffle(poker); System.out.println(poker); //3、发牌// 先定义4个集合,分别存放三个玩家的牌和一个底牌 ArrayList<String> player1 = new ArrayList<>(); ArrayList<String> player2 = new ArrayList<>(); ArrayList<String> player3 = new ArrayList<>(); ArrayList<String> dipai = new ArrayList<>(); /* * 遍历poker集合,获取每一张牌 * 使用poker集合的索引%3给3个玩家轮流发牌 * 剩余3张牌给底牌 * 注意: * 先判断底牌(i>=51),否则牌就发完了 * */ for (int i = 0; i < poker.size(); i++) { String s = poker.get(i); if (i >= 51) { dipai.add(s); } else if (i % 3 == 0) { player1.add(s); } else if (i % 3 == 1) { player2.add(s); } else { player3.add(s); } }//看牌 System.out.println("newcityboy:"+player1); System.out.println("light:"+player2); System.out.println("zmy:"+player3); System.out.println("Dipai:"+dipai); }}
转载于:https://www.cnblogs.com/newcityboy/p/11210468.html
相关资源:Discuz斗地主多人网页版游戏服务端
转载请注明原文地址: https://mac.8miu.com/read-54698.html