本文共 4059 字,大约阅读时间需要 13 分钟。
(1)无序
import java.util.ArrayList;import java.util.Collections;/*模拟斗地主的洗牌和发牌
*思路:
*/
public class Poker {public static void main(String[] args) {
//1.创建牌盒ArrayListarray = new ArrayList () ;//2.装牌//2.1创建花色数组String[] colors = {"♥","♠","♣","♦"} ;//2.2 创建点数数组String[] numbers = {"A","2","3","4","5","6","7","8","9", "10","J","Q","K"} ;for(String color :colors) { for(String number:numbers) { String poker = color.concat(number) ; array.add(poker) ; }}//添加小王和大王array.add("小王") ;array.add("大王") ;//3. 洗牌Collections.shuffle(array);//4.发牌//现在三个人玩,把每一个看成集合ArrayListArrayList player1 = new ArrayList () ;ArrayList player2 = new ArrayList () ;ArrayList player3 = new ArrayList () ;ArrayList diPai = new ArrayList () ;//有规律的:遍历ArrayList集合 使用普通for循环,获取到集合的元素//通过元素 % 人数 = 0 /1/2 /..for(int x = 0 ; x < array.size() ; x ++) { //获取到每一个元素 if(x >= array.size() -3) { diPai.add(array.get(x)) ; }else if(x % 3 == 0) { //玩家1 player1.add(array.get(x)) ; }else if(x % 3 == 1) { //玩家2 player2.add(array.get(x)) ; }else if(x % 3 == 2) { player3.add(array.get(x)) ; }}//5)看牌
// * 三个人看牌:将看牌封装成一个功能(独立的 代码块)
lookPoker("玩家1", player1);lookPoker("玩家2", player2);lookPoker("玩家3", player3);lookPoker("底牌", diPai);}public static void lookPoker(String name,ArrayList<String> array) {
System.out.print(name+"的牌是:");for(String s : array) { System.out.print(s+" ");}System.out.println();}}(2)有序import java.util.ArrayList;import java.util.Collections;import java.util.HashMap;import java.util.TreeSet;/**
*/
public class Porker2 {public static void main(String[] args) {
// 1. 创建两个集合:HashMap<Integer,String>,ArrayList<Integer>HashMap<Integer, String> hm = new HashMap<Integer, String>();ArrayList<Integer> array = new ArrayList<Integer>();// 2.装牌// 2.1 定义花色数组和点数数组String[] colors = { "♥", "♠", "♣", "♦" };String[] numbers = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2" };// 从0开始编号,将编号和编号对应的牌都存储到HashMap集合中,同时往ArrayList单独存储编号int index = 0;// 拼接for (String number : numbers) { for (String color : colors) { String poker = color.concat(number); hm.put(index, poker); array.add(index); index++; }}// 装小王和大王hm.put(index, "小王");array.add(index);index++;hm.put(index, "大王");array.add(index);// 不能++了,角标越界// 洗牌Collections.shuffle(array);// 4)发牌// 发的也是编号,为了保证牌有序,集合由TreeSet集合接收TreeSetplayer1 = new TreeSet ();TreeSet player2 = new TreeSet ();TreeSet player3 = new TreeSet ();TreeSet diPai = new TreeSet ();// 有规律的:遍历ArrayList集合 使用普通for循环,获取到集合的元素// 通过元素 % 人数 = 0 /1/2 /..for (int x = 0; x < array.size(); x++) { // 获取到每一个元素 if (x >= array.size() - 3) { diPai.add(array.get(x)); } else if (x % 3 == 0) { // 玩家1 player1.add(array.get(x)); } else if (x % 3 == 1) { // 玩家2 player2.add(array.get(x)); } else if (x % 3 == 2) { player3.add(array.get(x)); }}
// 5)看牌 封装功能
lookPoker("玩家1", player1, hm);lookPoker("玩家2", player2, hm);lookPoker("玩家3", player3, hm);lookPoker("底牌", diPai, hm);}
public static void lookPoker(String name,TreeSet<Integer> ts,
HashMap<Integer, String> hm) { System.out.print(name+"的牌是:");//遍历TreeSet集合获取到每一个编号for(Integer key :ts) { //获取到编号,通过编号找牌(在HashMap中找)String value = hm.get(key) ;System.out.print(value+" ");}System.out.println();} }转载于:https://blog.51cto.com/13670525/2117644