java 斗地主

Posted 墨染千城

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 斗地主相关的知识,希望对你有一定的参考价值。

代码:

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 斗地主的主要内容,如果未能解决你的问题,请参考以下文章

java 斗地主

斗地主之洗牌发牌----Java篇

使用Java实现简单的斗地主案例

使用Java实现简单的斗地主案例

Java斗地主案例

Java小白入门200例115之Java模拟斗地主发牌