斗地主

只能够实现直接发牌,并不能够真正地玩斗地主。(每次运行结果不同)

直接上代码:

//纯属娱乐

import java.util.ArrayList;
import java.util.Collections;

/*
* 1.准备牌
* 2.洗牌
* 3.发牌
* 4.看牌
*/
public class 斗地主 {
    public static void main(String[] args) {
        //1、准备牌

        String[] huase = {"♥","♣","♠","♦"};
        String[] nums = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
			
        //创建一个集合来存储组装好的牌
        ArrayList<String> pai = new ArrayList<>();

        //将牌装进集合
        for (String i : huase) {
            for (String j : nums) {
                pai.add(i + j);
            }
        }
        //不要忘记大小王
        pai.add("大王");
        pai.add("小王");

        //2、洗牌
        Collections.shuffle(pai);

        //3、发牌

        //创建四个集合分别存储底牌和三个玩家各自的手牌
        ArrayList<String>player1 = new ArrayList<>();
        ArrayList<String>player2 = new ArrayList<>();
        ArrayList<String>player3 = new ArrayList<>();
        ArrayList<String>dipai = new ArrayList<>();

        //把洗好的牌分别发放到各自的集合中
        for (int i = 0; i < pai.size(); i++) {
            if (i >= 51){
                dipai.add(pai.get(i));
            }else if (i % 3 == 0){
                player1.add(pai.get(i));
            }else if (i % 3 == 1){
                player2.add(pai.get(i));
            }else if (i % 3 == 2){
                player3.add(pai.get(i));
            }
        }

        //4、看牌
        System.out.println("周润发:"+player1);
        System.out.println("周星驰:"+player2);
        System.out.println("刘德华:"+player3);
        System.out.println("底牌:"+dipai);
    }
}

看一下运行结果:在这里插入图片描述

你可能感兴趣的:(娱乐)