码迷,mamicode.com
首页 > 编程语言 > 详细

java 斗地主

时间:2019-12-31 12:40:21      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:sort   ade   shuff   排序   pad   arraylist   图片   dex   rgs   

代码:

package com.oracle.demo02;

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

public class DouDiZhu {
    //斗地主(发牌)
    public static void main(String[] args) {
        //定义扑克牌Map
        HashMap<Integer, String> pooker=new HashMap<Integer,String>();
        //定义装有扑克牌号的集合
        ArrayList<Integer> pookerNumber=new ArrayList<Integer>(); 
        //封装数据
        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);
        //测试
        //System.out.println(pooker);
        //洗牌:打乱集合中元素顺序(随机)--> Collections.shuffle();
        Collections.shuffle(pookerNumber);
        //System.out.println(pookerNumber);
        //创建四个容器
        ArrayList<Integer> player1=new ArrayList<Integer>();
        ArrayList<Integer> player2=new ArrayList<Integer>();
        ArrayList<Integer> player3=new ArrayList<Integer>();
        ArrayList<Integer> bottom=new ArrayList<Integer>();
        //发牌
        for (int i = 0; i < pookerNumber.size(); i++) {
            //将前三张牌作为底牌
            if (i<3) {
                bottom.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();
        Collections.sort(player1);
        Collections.sort(player2);
        Collections.sort(player3);
        Collections.sort(bottom);
        //看牌
        look("渣渣辉",pooker, player1);
        look("梁朝痿",pooker, player2);
        look("赵本仙",pooker, player3);
        look("底牌",pooker, bottom);
    }
    public static void look(String name,HashMap<Integer, String> pooker,ArrayList<Integer> player) {
        System.out.print(name+": ");
        for (int number : player) {
            System.out.print(pooker.get(number)+" ");
        }
        System.out.println();
    }
}

 

构思:

技术图片

java 斗地主

标签:sort   ade   shuff   排序   pad   arraylist   图片   dex   rgs   

原文地址:https://www.cnblogs.com/l1314/p/12123620.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!