ArrayList模拟斗地主的洗牌,发牌和看牌
ArrayList模拟斗地主的洗牌,发牌和看牌
/*
* ArrayList模拟斗地主的洗牌,发牌和看牌
*
* 分析:
* 1.创建一个牌盒
* 2.装牌
* 3.洗牌
* 4.发牌
* 5.看牌
*/
代码:
import java.util.ArrayList; import java.util.Collections; /** * ArrayList模拟斗地主的洗牌,发牌和看牌 * @author TP * */ public class Doudizhu { /* * ArrayList模拟斗地主的洗牌,发牌和看牌 * * 分析: * 1.创建一个牌盒 * 2.装牌 * 3.洗牌 * 4.发牌 * 5.看牌 */ public static void main(String[] args) { //1.创建一个牌盒 ArrayList<String> array = new ArrayList<String>(); //2.装牌 //牌的花色:红桃 ♥ ,黑桃 ♠ ,梅花 ♣ ,方块 ♦ //定义一个花色数组 String[] colors = {"♥","♠","♣","♦"}; //定义 一个大小数组 String[] num = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"}; //定义字符串来放大小王 String smallBoss = "小王"; String bigBoss = "大王"; //把花色和大小放入集合中 for(String color : colors){ for(String num1 : num){ array.add(color.concat(num1)); } } //装入大小王 array.add(smallBoss); array.add(bigBoss); //3.洗牌 Collections.shuffle(array); //4.发牌 ArrayList<String> zhangsan = new ArrayList<String>(); ArrayList<String> lisi= new ArrayList<String>(); ArrayList<String> wangwu = new ArrayList<String>(); ArrayList<String> dipei = new ArrayList<String>(); for(int i = 0; i < array.size(); i++){ //如索引大于等于array.size()-3,则是底牌 if(i >= array.size()-3){ dipei.add(array.get(i)); }else if(i % 3 == 0){ zhangsan.add(array.get(i)); }else if(i % 3 == 1){ lisi.add(array.get(i)); }else if(i % 3 == 2){ wangwu.add(array.get(i)); } } //5.看牌 System.out.println("array : "+array); System.out.println("张三 : "+zhangsan); System.out.println("李四 : "+lisi); System.out.println("王五 : "+wangwu); System.out.println("底牌 : "+dipei); } }
输出结果:
array : [♦4, ♥K, ♣A, ♣J, ♦3, ♠2, ♦J, ♥7, ♥4, ♦Q, ♣3, ♦6, ♠4, ♠7, 大王, ♦5, ♣K, ♠A, ♠9, ♣5, ♠10, ♥Q, ♥A, ♥10, ♠6, ♣Q, ♦2, ♣7, ♣6, ♥5, ♥J, ♠8, ♥3, ♥8, 小王, ♦8, ♠5, ♦10, ♥6, ♣10, ♦K, ♠J, ♦A, ♣8, ♥2, ♦7, ♠K, ♥9, ♠Q, ♣2, ♣9, ♦9, ♣4, ♠3]
张三 : [♦4, ♣J, ♦J, ♦Q, ♠4, ♦5, ♠9, ♥Q, ♠6, ♣7, ♥J, ♥8, ♠5, ♣10, ♦A, ♦7, ♠Q]
李四 : [♥K, ♦3, ♥7, ♣3, ♠7, ♣K, ♣5, ♥A, ♣Q, ♣6, ♠8, 小王, ♦10, ♦K, ♣8, ♠K, ♣2]
王五 : [♣A, ♠2, ♥4, ♦6, 大王, ♠A, ♠10, ♥10, ♦2, ♥5, ♥3, ♦8, ♥6, ♠J, ♥2, ♥9, ♣9]
底牌 : [♦9, ♣4, ♠3]